Php public platform configuration interface development program, php public
Before reading this article, make the following preparations:
1. Register a subscription number (via public platform https://mp.weixin.qq.com /);
2. Register Sina cloud and real-name authentication http://sae.sina.com.cn/
3. Create a cloud application after real-name authentication of Sina cloud.
4. You Need To Know basic php knowledge.
I. Verification Interface
1. log on to our public account. Then click to enter the developer tool, in the lower left corner of the page after logon.
2. Click to enter the developer documentation. Then click Start development, and click access guide. We will see it as shown in.
As shown in the figure, developers must follow the steps below to access the public platform for development:
1. Fill in the server configuration
2. verify the validity of the server address
3. Implement business logic based on the Interface documentation.
Step 1:Enter Server Configuration
After logging on to the public account, click basic development configuration in the lower right corner,
The URL is the interface URL used by developers to receive messages and events. For details, refer to the following section.
You can enter the Token at Will (but everyone may not. If the submission fails, you can try to modify the Token). The Token will be compared with the Token contained in the interface to verify the security, the EncodingAESKey is manually entered or randomly generated by the developer and will be used as the accesskey for message body encryption and decryption.
The entered URL requires that we have the address of our own server. Now we have to go to the Sina cloud application we have registered.
1. log on to Sina cloud, click cloud application, and click to enter the console. Create
2. Level Domain Name and Application name. Just start your own name and click the place marked in red.
3. Click the arrow to edit the code.
4. The SAE interface will pop up later. We need to create a PHP file in it and click access through URL. Then we will copy the URL and place it on the URL we previously configured.
Step 2:The verification message is indeed from the server.
The specific php code is as follows:
<? Php //// the simplest verification method // echo $ _ GET ["echostr"]; // verify whether the function checkWeixin () {// four parameters will be sent to the random string $ signature =$ _ GET ["signature"] of the timestamp of the backend signature of our server; $ timestamp = $ _ GET ["timestamp"]; $ nonce = $ _ GET ["nonce"]; $ echostr = $ _ GET ["echostr"]; $ token = "qilipingmgl"; // 1) sort the tokens, timestamp, and nonce in lexicographically ordered order. $ tmpArr = array ($ nonce, $ token, $ timestamp ); sort ($ tmpArr, SORT_STRING); // 2) concatenates three parameter strings into one string for sha1 encryption $ str = Implode ($ tmpArr); $ sign = sha1 ($ str); // 3) the encrypted string obtained by the developer can be compared with signature, identify the request from if ($ sign ==$ signature) {echo $ echostr ;}} checkWeixin () ;?>
Note:In the code, $ token is different from each other. We need to fill in our own code. Click the developer tool, and there is a public platform test account on the right. Click to enter. Then we will see the interface configuration, copy the Token to the code.
Copy the code to the PHP file of the SAE we just created, save it (remember to save it), and then click access through URL,
If an error is reported (the error message is a number), please click in the developer documentation and read it before clicking it. There is an interface return code description. Let's take a look at the error, note: 0 indicates that the request is successful.
2. Get access_token
Tip: access_token is the globally unique interface call credential of the public account. access_token is required when the Public Account calls each interface. as developers, We need to properly store the access_token. The validity period of the access_token is currently 2 hours and needs to be refreshed regularly. Repeated access_token acquisition will invalidate the access_token obtained last time.
Step 1,Click the developer documentation, click Start development, and click GET access_token to see the http Request Method: GET, and then a URL. We need to use this URL, as shown in,
E0204D74-2EA6-4943-B93F-7E7C1E2FA88A.png
Step 2:We write the GET request function to GET the access_token.
<? Php class Weixin {private $ appID = "wxe62f370c4e2cade2"; private $ appsecret = "58807091ae5a4c59ee3e47417184bdb7"; function getAccessToken () {$ url = "https://api.weixin.qq.com/cgi-bin/token? Grant_type = client_credential & appid ={$ this-> appID} & secret ={$ this-> appsecret} "; // return $ this-> httpGet ($ url ); // json string $ json = $ this-> httpGet ($ url); // parse json $ obj = json_decode ($ json); return $ obj-> access_token ;} function httpGet ($ url) {// 1. initialize $ curl = curl_init (); // configure curl curl_setopt ($ curl, CURLOPT_URL, $ url); curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, true ); // execute curl $ res = curl_exec ($ curl );/ /Disable curl curl_close ($ curl); return $ res ;}}$ wx = new Weixin (); echo $ wx-> getAccessToken (); /* get access_token Method */?>
Note:In the code, $ appID and $ appsecret are different from each other. We need to fill in our own code. Click the developer tool, and then there is a public platform test account on the right. Click to enter, then we will see the test number information. then copy it to the code.
Step 3: copy the code to the SAEphp file and right-click to access it through URL. If {"access_token": "ACCESS_TOKEN", "expires_in": 7200} is returned, the token is successfully obtained. if an error is reported unfortunately, it doesn't matter if we look for an error. Click the development document, and you must read it before clicking start. Click the interface return code description. Let's take a look at it and find the error as prompted.
Here, our configuration interface is complete. In the next section, we will continue to develop the custom menu creation interface.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.