PHP Technology development WeChat public platform _php Example

Source: Internet
Author: User
Tags cdata
The following is an illustrated way to introduce the public platform development process, the specific content is as follows:

There are two modes of public platform: edit mode and development mode.

Normal functions can be done in edit mode. The development mode has more features. Let's use development mode to develop Helloword.

The steps are as follows:

First step: register a public number first (https://mp.weixin.qq.com)

Step two: register for the SAE (http://sae.sina.com.cn/) as your server.

Step three: log in to the public platform (https://mp.weixin.qq.com) to view the development documentation and download the official demo. Make the appropriate changes.

Fourth Step: compress the code into a zip format and upload it to the SAE platform.

Fifth Step: Login to the public platform and enter the developer center. Turn on "Service person configuration".

The Sixth step: success.

Let's go:

1. Register a public number first (https://mp.weixin.qq.com)

2. Registration of SAE (http://sae.sina.com.cn/)

After registering to remember to do real-name authentication, or binding to the public platform, there will always be "unable to obtain token" hint. (real name certification takes 3 business days to succeed)

You can then click Create App. You can see it below when you create it.

Go to the app you created. Then click Code Management.

3. Login to Public platform (https://mp.weixin.qq.com)

Review the development documentation and download the official demo.

After opening is the following code:

<?php/** * WeChat PHP test *///define your Tokendefine ("TOKEN", "Weixin"); $wechatObj = new Wechatcallbackapitest (); $we Chatobj->valid (); Class wechatcallbackapitest{Public Function valid () {$echoStr = $_get["Echostr"];//valid  Signature, option if ($this->checksignature ()) {echo $echoStr; Exit }} Public Function responsemsg () {//get-post data, May is due to the different environments $postStr = $GLOBALS ["Http_ra  W_post_data "];   Extract post Data if (!empty ($POSTSTR)) {/* Libxml_disable_entity_loader is to prevent XML eXternal entity injection,   The best-on-the-is-check-validity of XML by yourself */Libxml_disable_entity_loader (TRUE);  $POSTOBJ = simplexml_load_string ($postStr, ' simplexmlelement ', libxml_nocdata);  $fromUsername = $POSTOBJ->fromusername;  $toUsername = $POSTOBJ->tousername;  $keyword = Trim ($postObj->content);  $time = time (); $TEXTTPL = "
 
   
    %s 
    
    %s 
    
   
    
 %s 
    
    %s 
    
    %s 
    
   
     0 
       

  
 ";   if (!empty ($keyword)) {$msgType = "text";   $CONTENTSTR = "Welcome to WeChat world!";   $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);  Echo $resultStr;  }else{echo "Input something ...";  }}else {echo ""; Exit }} Private Function Checksignature () {//must define token by yourself if (!defined ("token")) {throw new Exception (' TOKEN is not defined! '); } $signature = $_get["signature"]; $timestamp = $_get["timestamp"]; $nonce = $_get["nonce"]; $token = token; $TMPARR = Array ($token, $timestamp, $nonce); Use sort_string rule SORT ($TMPARR, sort_string); $TMPSTR = implode ($TMPARR); $TMPSTR = SHA1 ($TMPSTR); if ($tmpStr = = $signature) {return true;} else{return false;}}? >

I have tried, as the code above, seems unable to execute to response that piece. So make a change

<?php/** * WeChat PHP test *///define your Tokendefine ("TOKEN", "Weixin"); $wechatObj = new Wechatcallbackapitest ();//This Change if ($_get["Echostr"]) {$WECHATOBJ->valid ();} else{$wechatObj->responsemsg ();} Class wechatcallbackapitest{Public Function valid () {$echoStr = $_get["Echostr"];//valid signature, option if ($this-&G  T;checksignature ()) {echo $echoStr; Exit }} Public Function responsemsg () {//get-post data, May is due to the different environments $postStr = $GLOBALS ["Http_ra  W_post_data "];   Extract post Data if (!empty ($POSTSTR)) {/* Libxml_disable_entity_loader is to prevent XML eXternal entity injection,   The best-on-the-is-check-validity of XML by yourself */Libxml_disable_entity_loader (TRUE);  $POSTOBJ = simplexml_load_string ($postStr, ' simplexmlelement ', libxml_nocdata);  $fromUsername = $POSTOBJ->fromusername;  $toUsername = $POSTOBJ->tousername;  $keyword = Trim ($postObj->content);  $time = time (); $TEXTTPL = "
 
   
    %s 
    
    %s 
    
   
    
 %s 
    
    %s 
    
    %s 
    
   
     0 
       

  
 ";   if (!empty ($keyword)) {$msgType = "text";   $CONTENTSTR = "Welcome to WeChat world!";   $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);  Echo $resultStr;  }else{echo "Input something ...";  }}else {echo ""; Exit }} Private Function Checksignature () {//must define token by yourself if (!defined ("token")) {throw new Exception (' TOKEN is not defined! '); } $signature = $_get["signature"]; $timestamp = $_get["timestamp"]; $nonce = $_get["nonce"]; $token = token; $TMPARR = Array ($token, $timestamp, $nonce); Use sort_string rule SORT ($TMPARR, sort_string); $TMPSTR = implode ($TMPARR); $TMPSTR = SHA1 ($TMPSTR); if ($tmpStr = = $signature) {return true;} else{return false;}}? >

You can change welcome to WeChat world! to Hello word!

4. Compress the code into a zip format and upload it to the SAE platform.

Click "Edit Code" to see the php file you uploaded. Then right-click, url to view. Copy the URL (http://1.carlzhang.sinaapp.com/wx_carlzhang819.php). Here you have to remember the tokens defined in this PHP file. Here is "Weixin", which can be seen in the.

5. Login to the public platform and enter the developer center. Turn on "Service person configuration". URL fill in the URL of the previous copy (I deleted the previous 1. Because my SAE defaults to the first version. You can try, delete 1, if URL access, do not report 404, that is no problem). Token fills in the token in the code ("Weixin" above).

If you enable success, you can focus on your platform, enter content, and see if the hint is welcome to WeChat world! or Hello word!

All of the above is for the public platform to do the explanation, hope can help everyone.

  • 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.