PHP Micro-trust public Platform Configuration Interface Development Program _php skills

Source: Internet
Author: User
Tags curl documentation php class php code sha1 sha1 encryption

Before reading this article, you should make the following preparations:

1. Register a subscription number (via the micro-credit 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 authentication Sina Cloud, creates a cloud application.
4. You need to know the basics of PHP.

One: Verify the interface

1. Log in to our public number. Then click into the Developer tool, in the lower left corner of the page after login.

2. Click to enter the developer documentation. Then click Start Development, click on the Access Guide, we will see the following figure.

As the picture says, the development of the public platform, the developer needs to follow the following steps to complete:

1. Fill in the server configuration
2. Verify the validity of the server address
3. Implement business logic based on interface document.

First step: fill in the server configuration

After we login to the micro-credit public number, the bottom right corner of the basic development configuration point in,

The URL is the interface URL that the developer uses to receive the message and the event. How to fill in the details.

Token can fill in (but everyone is not the same, if the subsequent commit failure can try to modify the token), this token and the interface included in the token to be compared to verify security, Encodingaeskey by the developer manually fill out or randomly generated, will be used as the message body encryption key.

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

I. Login to the Sina cloud, click on the cloud application, click to enter the console. The following figure creates

Two. class domain name and application name, it's good to have a name. Then click to go to the red mark in the picture below.

Three. The following figure arrows indicate clicking into the edit code

Four. After the SAE will pop up the interface, we need to create a PHP file inside, and then click through the URL to access, and then we copy the Web site down to our basic configuration before the URL.

Step Two: Verify that the message really comes from the micro-trust server

The specific PHP code is as follows:

<?php/////
 The simplest way of validating//
 echo $_get["Echostr"];

 Verify that the micro-letter
 function Checkweixin () {
  //micro-letter 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) 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 to compare with the signature, identifies the request from the micro-letter
  if ($sign = = $signature) {
   echo $echostr
  }
 } Checkweixin ();
? >

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

We 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 information is a number of errors), please click on the developer's documentation, click on the start required to read, there is an interface return code description, we can compare, where the error, note: 0 of the time to indicate a successful request.

Two. Get Access_token

Hint: Access_token is the public number's global unique interface invocation credentials, the public number calls each interface to use the Access_token. We as a developer to properly save, the Access_token is currently 2 hours, to be timed to refresh, Repeated fetching will result in the last acquired Access_token invalidation.

The first step, we click on the developer document, click to Start development, click to get Access_token will see, HTTP request way: Got, then a URL, we need to use this URL, the following figure,

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

Step Two: we write the function of Get request, obtain 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);
   Executive Curl $res = curl_exec ($curl);
   Closure of Curl Curl_close ($curl);
  return $res;
 }} $wx = new Weixin ();

 echo $wx->getaccesstoken (); /* Fetch Access_token Method Get method/*?> 

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

The third step: we copy the code to the saephp file, right-click through the URL access, if the return of {"Access_token": "Access_token", "expires_in": 7200} means we get token success. If the error is unfortunate, We find mistakes, click on the development of documents, click to start before you read, click the interface return code description, we contrast, according to the prompts to find errors.

Now that our configuration interface is complete, in the next section we will continue to develop the-> custom menu to create an interface.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.