PHP WeChat public Platform Configuration Interface Development Program method examples

Source: Internet
Author: User
Tags php class sha1 encryption
This article mainly for you to introduce the PHP public Platform Configuration Interface Development program, with a certain reference value, interested in small partners can refer to

Before reading this article, you should prepare the following:

1. Register a subscription number (via public platform https://mp.weixin.qq.com/);
2. Registered Sina Cloud and real-name authentication http://sae.sina.com.cn/
3. After the real-name certification Sina Cloud, create a cloud app.
4. You need to know the basics of PHP.

One: Authentication interface

1. Log in to our public number. Then click on the Developer tool to enter the bottom left corner of the page after login.

2. Click to enter the developer documentation. Then click on Start Development, click on the Access guide and we'll see like.

As the picture says, access to public platform development, developers need to follow the following steps to complete:

1. Fill in the server configuration
2. Verifying the validity of the server address
3. Implement the business logic according to the interface document.

First step: fill out the server configuration

After we log in to the public number, the bottom right corner of the basic development configuration point in,

URLs are the interface URLs that developers use to receive messages and events. How do we fill in the details?

Tokens can be filled in at will (but everyone is different, if the subsequent submission fails to try to change token), this token is compared to the tokens contained in the interface to verify security, Encodingaeskey is manually filled out by the developer or randomly generated, will be used as the message body plus decryption key.

Fill in the URL, we need to have their own server address, and now we are going to our previous registered Sina cloud application.

One. Log in to the Sina Cloud, click on the Cloud app and click to enter the console. If you create

Two. Domain name and app name, just make a name for yourself. Then click to enter the red mark of the place.

Three. Click to enter the edit code as the arrow indicates

Four. Then we will pop up the SAE interface, we need to build a PHP file inside, and then click through the URL to access, then we copy the URL, and put it on the basic configuration of our previous URL.

Step Two: Verify that the message does come from the server

The specific PHP code is as follows:

<?php/////The simplest authentication method//Echo $_get["Echostr"]; Verify that the function Checkweixin () {  //will send 4 parameters to our server background signature timestamp random string random number  $signature = $_get["signature"];  $timestamp = $_get["timestamp"];  $nonce = $_get["nonce"];  $echostr = $_get["Echostr"];  $token = "QILIPINGMGL";  1) The token, timestamp, nonce three parameters of the dictionary order  $TMPARR = Array ($nonce, $token, $timestamp);  Sort ($TMPARR, sort_string);  2) concatenation of three parameter strings into a string for SHA1 encryption  $STR = implode ($TMPARR);  $sign = SHA1 ($STR);  3) The developer obtains the encrypted string can be compared with signature, identifies the request from  if ($sign = = $signature) {   echo $echostr;  }} checkweixin ();? >

Note: The code $token is different for everyone, we need to fill out their own, click on the Developer tool, then the right side of the public platform test Account, click Enter, then we will see the interface configuration, the token copied over to put in the code.

We'll copy this code into the SAE PHP file we just created, save it (remember to save it), and click on the URL to access it,

If the error (the message is the number of error), please click on the developer documentation, click on the beginning must read, there is an interface return code description, we can control, where the error, note: 0 indicates that the request was successful.

Two. Get Access_token

Tip: Access_token is the global unique interface invocation credential for the public number, and the public number needs to use Access_token when calling each interface. We as developers to properly save, Access_token is currently valid for 2 hours, need to refresh regularly, A duplicate fetch causes the last acquired Access_token to fail.

The first step, we click on the developer documentation, click Start Development, click Get Access_token will see, HTTP request way: Get, then a URL, we need to use this URL, such as,

E0204d74-2ea6-4943-b93f-7e7c1e2fa88a.png

Step Two: we write the GET Request function, get Access_token

<?php class weixin{  private $appID = "Wxe62f370c4e2cade2";  Private $appsecret = "58807091ae5a4c59ee3e47108184bdb7";  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. Initialization   $curl = Curl_init ();   Configure Curl   curl_setopt ($curl, Curlopt_url, $url);   curl_setopt ($curl, Curlopt_returntransfer, true);   Perform curl   $res = curl_exec ($curl);   Turn off Curl   curl_close ($curl);   return $res;  } } $wx = new Weixin (); echo $wx->getaccesstoken (); /*  Get Access_token method Get method */?>

Note: the code $appID and $appsecret is different for everyone, we need to fill out their own, click on the Developer tool, then the right side of the public platform test Account, click Enter, then we will see the test number information. Then copy it and put it in the code.

The third step: we copy the code into the saephp file, right-click through the URL to access, if return {"Access_token": "Access_token", "expires_in": 7200} means we get token success. If the unfortunate error, It doesn't matter we find the error, click on the development document, click on the beginning must read, click on the interface return code description, we contrast, according to the hint to find the wrong.

Say here our configuration interface is complete, the next section we continue to develop the custom menu creation interface.

Summary: The above is the entire content of this article, I hope to be able to help you learn.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.