Home > Article > Backend Development > How to access the public account in php
How to access WeChat public account with PHP?
Prerequisite
·Server resources: You need to have a public network server resource (Alibaba Cloud, Tencent Cloud Or other cloud resources)
·Server environment: It is recommended to use LNMP environment, which is perfect for PHP development. (Please make sure your server has successfully run PHP. If you have any questions, you can privately chat with the poster)
·Public account resources: If you do not have a public account, first go to the public account platform to apply for WeChat public platform. Generally, individuals can only apply for a subscription number.
·Technical requirements: Master a certain amount of coding ability.
Related recommendations: "php tutorial"
Development configuration
·After logging in to the public platform Click Development->Basic Configuration.
·Get the developer password and save it (please save it where only you can see it)
·Add IP whitelist (add Go to your server’s public IP)
·Fill in the server configuration. The server address can be IP or domain name, fill in your script address (for WeChat access), customize the token, AESKEY can be randomly generated, and choose safe mode for the message method.
· Of course, please save the above information to the project to call it. Don’t click submit here yet!
Script Development
· Next, just develop the interface in the link address you filled in in the previous step.
·You can directly copy the code into it. Document description WeChat development document (the demo of the document is not connected)
<?php namespace api\controllers; use yii\web\Request; class WeiXinController { public function actionIndex() { $params = (new Request())->get();//不用yii2的同学替换为 $params=$_GET; $tmpArray = array(WX_TOKEN, $params['timestamp'], $params['nonce']);//WX_TOKEN就是上一步填写的token值 sort($tmpArray, SORT_STRING);//别漏了第二个参数 $tmpStr = implode($tmpArray); $tmpStr = sha1($tmpStr); if ($params['signature'] == $tmpStr) { echo $params['echostr']; exit(); } echo 'failed'; } }
Done
·After completing the above steps, click Submit. You will see that the configuration is successful!
·Problems that may arise.
·Token parsing failed: It means your interface did not return.
·Service error: There may be a problem with your server. Try to see if you can access the interface.
The above is the detailed content of How to access the public account in php. For more information, please follow other related articles on the PHP Chinese website!