PHP Micro-trust public platform interaction and interface _php Example

Source: Internet
Author: User
Tags cdata php code sprintf trim xml parser

This article is divided into three parts for everyone to introduce, the specific contents are as follows

1, micro-credit users, micro-trust server and background server interaction

Example: A micro-credit user sends a text message to the public number, this message will be first passed to the micro-trust server, the micro-trust server processing this information and the XML data format passed to the backend server, the background server to accept the data will be processed, and then the response data in XML data format passed to the micro-trust server, The micro-trust server is then responsive to the user's micro-mail interface.
The interactive process between the micro-trust user and the micro-trust backend server is the data transfer process, but it needs to pass through the micro-trust server.

So what is the use of micro-trust server?
The XML data is processed and packaged and displayed on the phone screen. We accept the text message as follows:

Single text:

Multi-text

You will find that almost all the text in the micro-letter is this format, plate, size is the same, this is after the micro-mail server packaging results.

2, the data type of interaction

Types of data that a micro-trust user can send
1. Text type

<xml>
<tousername><![ Cdata[%s]]></tousername>
<fromusername><![ cdata[%s]]></fromusername>
<CreateTime>%s</CreateTime>
<msgtype><![ Cdata[text]]></msgtype>
<content><![ cdata[%s]]></content>
</xml> ";

2, Voice (voice)

<xml>
<tousername><![ Cdata[%s]]></tousername>
<fromusername><![ cdata[%s]]></fromusername>
<CreateTime>%s</CreateTime>
<msgtype><![ Cdata[voice]]></msgtype>
<format><![ cdata[amr]]></format>
<MsgId>5836982871638042400</MsgId>
<mediaid><![ Cdata[pgkso3lagbvtsfyo7fgu51kuya07d0c_nozz2fn1z6vythosf59ptfl0vaggxkvh]]></mediaid>
< recognition><! [Cdata[]]></recognition>//recognition represents the results of speech recognition
</xml>

3, Picture (IMG)

<xml>
<tousername><![ Cdata[%s]]></tousername>
<fromusername><![ cdata[%s]]></fromusername>
<CreateTime>%s</CreateTime>
<msgtype><![ Cdata[image]]></msgtype>
<picurl><![ Cdata[http://mmbiz.qpic.cn/mmbiz/l4qjytoibummhn90t1mnaibyiar8ljyicf3mw7xx3blp1qzgub7ctz0d]]></picurl>
<MsgId>5836982871638042400</MsgId>
<mediaid><![ Cdata[pgkso3lagbvtsfyo7fgu51kuya07d0c_nozz2fn1z6vythosf59ptfl0vaggxkvh]]></mediaid>
</xml>

Each message to the micro-trust server will be marked with a msgid, uploaded pictures, videos, voice, etc. will also be labeled a mediaid.

4. Videos (VIDEO)

<xml>
<tousername><![ Cdata[%s]]></tousername>
<fromusername><![ cdata[%s]]></fromusername>
<CreateTime>%s</CreateTime>
<msgtype><![ cdata[vedio]]></msgtype>
<MsgId>5836982871638042400</MsgId>
<mediaid><![ Cdata[pgkso3lagbvtsfyo7fgu51kuya07d0c_nozz2fn1z6vythosf59ptfl0vaggxkvh]]></mediaid>
< thumbmediaid><! [Cdata[mxuj5gcceesjwx2t9qsk62yzi
clcp_hnrdftqcojlpet2g9q3d22ukslybflz01j]]></thumbmediald>;// Display the image when the video is still in the address
</xml>

5. Location message (location)

<xml>
<tousername><![ Cdata[%s]]></tousername>
<fromusername><![ cdata[%s]]></fromusername>
<CreateTime>%s</CreateTime>
<msgtype><![ cdata[location]]></msgtype>
<MsgId>5836982871638042400</MsgId>
<Location_X> 22.539968</location_x>
<Location_Y>113.954980</Location_Y>
<scale>16</scale >
<label><![ cdata[No. 9001 Shennan Avenue, Shenzhen Nanshan District, Guangdong Province, China 
: 518053]]></label>
</xml>

6. Link message

<xml>
<tousername><![ Cdata[%s]]></tousername>
<fromusername><![ cdata[%s]]></fromusername>
<CreateTime>%s</CreateTime>
<msgtype><![ cdata[link]]></msgtype>
<MsgId>5836982871638042400</MsgId>
<title><![ Cdata[Public platform developers of the lake]]></title>
<description><![ cdata[Chen Kun public number This time the fire, everyone ...] ></Description>
<url><![ Cdata[http://www.cnblogs.com/txw1958/]]></url>
<MsgId>5839907284805129867</MsgId>
</xml>

Message type for background server response
1. Text type
2, Voice (voice)

<xml>
<tousername><![ Cdata[%s]]></tousername>
<fromusername><![ cdata[%s]]></fromusername>
<CreateTime>%s</CreateTime>
<msgtype><![ cdata[voice]]></msgtype>
<MsgId>5836982871638042400</MsgId>
<Voice>
<mediaid><! [cdata[pgkso3lagbvtsfyo7fgu51kuya07d0c_nozz2fn1z6vythosf59ptfl0vaggxkvh]]></mediaid>
</Voice >
</xml>

3, Picture (IMG)

<xml>
<tousername><![ Cdata[%s]]></tousername>
<fromusername><![ cdata[%s]]></fromusername>
<CreateTime>%s</CreateTime>
<msgtype><![ cdata[image]]></msgtype>
<MsgId>5836982871638042400</MsgId>
<Image>
<mediaid><! [cdata[pgkso3lagbvtsfyo7fgu51kuya07d0c_nozz2fn1z6vythosf59ptfl0vaggxkvh]]></mediaid>
</Image >
</xml>

4. Videos (VIDEO)

<xml>
<tousername><![ Cdata[%s]]></tousername>
<fromusername><![ cdata[%s]]></fromusername>
<CreateTime>%s</CreateTime>
<msgtype><![ cdata[vedio]]></msgtype>
<MsgId>5836982871638042400</MsgId>
<video>
<mediaid><! [cdata[pgkso3lagbvtsfyo7fgu51kuya07d0c_nozz2fn1z6vythosf59ptfl0vaggxkvh]]></mediaid>
< thumbmediaid><! [Cdata[mxuj5gcceesjwx2t9qsk62yzi
clcp_hnrdftqcojlpet2g9q3d22ukslybflz01j]]></thumbmediald>;// show that picture when the video is still in the address
</video>
</xml>

5. Music (musical)

<xml>
<tousername><![ Cdata[%s]]></tousername>
<fromusername><![ cdata[%s]]></fromusername>
<CreateTime>%s</CreateTime>
<msgtype><![ cdata[voice]]></msgtype>
<MsgId>5836982871638042400</MsgId>
<Music>
<title><! [cdata[the most dazzling national wind]]></title>
<description><![ cdata[Phoenix legend]]></description>
<musicurl><![ Cdata[http://zj189.cn/zj/download/music/zxmzf.mp3]]></musicurl>
 
 

6, TEXT (news)

<xml> <tousername><! [cdata[%s]]></tousername> <fromusername><! [cdata[%s]]></fromusername> <CreateTime>%s</CreateTime> <msgtype><! [cdata[news]]></msgtype> <MsgId>5836982871638042400</MsgId> <content><! [cdata[]]></content> <ArticleCount>%s</ArticleCount> <Articles> <item> <title ><! [cdata["Shenzhen" Live temperature: 6 ℃ Humidity: 62﹪ wind Speed: Northeast 2]]></title> <description><! [cdata[]]></description> <picurl><! [cdata[http://www.doucube.com/weixin/weather/icon/banner.jpg]]></picurl> <Url><! [cdata[]]></url> </item> <item> <title><! [cdata["Shenzhen" Live temperature: 6 ℃ Humidity: 62﹪ wind Speed: Northeast 2]]></title> <description><! [cdata[]]></description> <picurl><! [cdata[http://www.doucube.com/weixin/weather/icon/banner.jpg]]></picurl> <Url><! [cdata[]]></url> </item> </artiCles> </xml>
 

The above code only makes reference to the data filling. The above code can be called when needed, just to show you the following data format.
CDATA is a tag that is not parsed by the XML parser in the text data that is tagged. A CDATA part to "

Tousername Receiver Account Number
Fromusername Sender Account
Createtime Send Event
Msgtype Data type
Content Text Contents
Articlecount number of pictures and texts
MsgId Data ID
MediaId Media ID
Title titles
Description description
Musicurl Music Connection Address
Hqmusicurl High quality Music connection address

2, the specific interaction steps that code

In Figure 2 of the previous chapter, we defined the URL and token for the test number. A URL is a backend server address that communicates with a micro-trust server, and a token is equivalent to a token. This token is produced when the micro-mail server communicates with the background server, and if the background server discovers that the micro-server is the same as the token it carries, it will not communicate. This process is called token validation (this token is not a token value).
Above the more image of the talk, I explained by the code below
For example: URL is http://weixinceshi111111.applinzi.com/index2.php
Token:weixin
index2.php Code

<?php////Response to User message//micro-credit public account response to the user's different message type//micro-trust server to communicate with the background server first to token authentication, micro-mail will be sent through the Get way signature (micro-letter encryption signature), Nonce (random number), Timestamp (timestamp), ECHOSTR (random string). After the background server acquisition will be timestamp, nonce and the definition of token in a certain sequence of stitching into strings, through the shal encrypted after the results and signature to compare, if the same echostr back to the micro-trust server.
Indicates a successful validation.
Header ("Content-type:text;charset=utf8");
Define ("TOKEN", "Weixin"); Token authentication is the transmission of data via get, and the data sent by the micro-trust user is sent by post.
A GET request is made before a POST request is made.
$WECHATOBJ = new Wechatcallbackapitest (); Determines whether a GET request or a POST request. $_get[' Echostr ', if present, represents a GET request for token authentication.
The reverse is the POST request that transmits the data. if (!isset ($_get[' echostr ')) {$WECHATOBJ->responsemsg ();//Response Data}else{$WECHATOBJ->valid ();/response} class WECHATC
 allbackapitest {public Function valid () {$echoStr = $_get["Echostr"];
 if ($this->checksignature ()) {echo $echoStr;
 Exit
 The Private Function checksignature () {$signature = $_get["signature"];
 $timestamp = $_get["timestamp"];
 $nonce = $_get["nonce"];
 $token = token;
 $TMPARR = Array ($token, $timestamp, $nonce); Sort ($TMPARR);//The elements in the array are sorted $tmpStr = IMPlode ($TMPARR);//concatenate the elements in the array into a string $tmpStr = SHA1 ($TMPSTR);//Encrypt the string.
 if ($tmpStr = = $signature) {return true;
 }else{return false;
 The Public Function responsemsg () {$postStr = $GLOBALS ["Http_raw_post_data"];//get the data sent. if (!empty ($POSTSTR)) {$POSTOBJ = simplexml_load_string ($postStr, ' simplexmlelement ',);// Loads an XML string into a SimpleXMLElement object.
 Simplexml_load_string () is an XML parser.

 $RX _type = Trim ($postObj->msgtype);//trim remove Kongge from both ends of the string.
  The message type that the user sends determines the switch ($RX _type) {case "text": $result = $this->receivetext ($POSTOBJ);
 Break
  Case "image": $result = $this->receiveimage ($POSTOBJ);
 Break
  Case "Voice": $result = $this->receivevoice ($POSTOBJ);
 Break
  Case "Video": $result = $this->receivevideo ($POSTOBJ);
 Break
  Default: $result = "Unknow msg type:". $RX _type;
 Break
 echo $result;
 }else {echo "";
 Exit

 The Private Function Receivetext ($object) {$keyword = Trim ($object->content); if ($keyword = = "text") {//Reply text message $coNtent = "This is a text message";
 $result = $this->transmittext ($object, $content);
 else if ($keyword = = "Graphic" | | $keyword = = "Single text") {//reply to Single message $content = Array (); $content [] = Array ("title" => "single caption", "Description" => "single Graphic Content", "Picurl =>" http://discuz.comli.com/weixin/
 Weather/icon/cartoon.jpg "," Url "=>" http://m.cnblogs.com/?u=txw1958 ");
 $result = $this->transmitnews ($object, $content);
 else if ($keyword = = "multiple graphics") {//reply to multiple text messages $content = array (); $content [] = Array ("title" => "multiple graphics and Text 1 titles", "Description" => "", "Picurl =>" http://discuz.comli.com/weixin/
 Weather/icon/cartoon.jpg "," Url "=>" http://m.cnblogs.com/?u=txw1958 "); $content [] = Array ("title" => "multiple graphics and Text 2 titles", "Description" => "", "Picurl =>" http://d.hiphotos.bdimg.com/wisegame/
 Pic/item/f3529822720e0cf3ac9f1ada0846f21fbe09aaa3.jpg "," Url "=>" http://m.cnblogs.com/?u=txw1958 "); $content [] = Array ("title" => "multiple graphics and Text 3 titles", "Description" => "", "Picurl =>" http://g.hiphotos.bdimg.com/wisegame/ Pic/item/18cb0a46f21fbe090d338acc6a600c338644adfd.jpg "," Url "=>" http://m.cnblogs.com/?u=txw1958 ");

 $result = $this->transmitnews ($object, $content); else if ($keyword = = "Music") {//Reply to music message $content = Array ("Title" => "the most dazzling national wind", "Description" => "singer: Phoenix Legend", "Musicurl"
 => "Http://121.199.4.61/music/zxmzf.mp3", "Hqmusicurl" => "Http://121.199.4.61/music/zxmzf.mp3");
 $result = $this->transmitmusic ($object, $content);
 return $result;
 Private Function Receiveimage ($object) {//reply picture message $content = Array ("MediaId" => $object->mediaid);
 $result = $this->transmitimage ($object, $content);
 return $result;
 Private Function Receivevoice ($object) {//reply voice message $content = Array ("MediaId" => $object->mediaid);
 $result = $this->transmitvoice ($object, $content);
 return $result; Private Function Receivevideo ($object) {//reply video message $content = Array ("MediaId" => $object->mediaid, "Thumbmediai D "=> $object->thumbmediaid," Title "=>" "," Description"=>");
 $result = $this->transmitvideo ($object, $content);
 return $result;
 * * Reply to text message, wrapping the XML message that will be returned. * * Private Function Transmittext ($object, $content) {$textTpl = "<xml> <tousername><![ Cdata[%s]]></tousername> <fromusername><! [cdata[%s]]></fromusername> <CreateTime>%s</CreateTime> <msgtype><! [cdata[text]]></msgtype> <content><!
 [cdata[%s]]></content> </xml> "; $result = sprintf ($TEXTTPL, $object->fromusername, $object->tousername, Time (), $content);
 sprintf () This function is still more interesting, you can search for a look.
 return $result; * * * reply to Picture message/Private Function transmitimage ($object, $imageArray) {$itemTpl = "<Image> <mediaid>& lt;!

 [cdata[%s]]></mediaid> </Image> ";

 $item _str = sprintf ($ITEMTPL, $imageArray [' MediaId ']); $TEXTTPL = "<xml> <tousername><! [cdata[%s]]></tousername> <fromusername><! [Cdata[%s]]></fromuseRname> <CreateTime>%s</CreateTime> <msgtype><!

 [cdata[image]]></msgtype> $item _str </xml> ";
 $result = sprintf ($TEXTTPL, $object->fromusername, $object->tousername, Time ());
 return $result; * * * reply voice message/Private Function Transmitvoice ($object, $voiceArray) {$itemTpl = "<Voice> <mediaid>& lt;!

 [cdata[%s]]></mediaid> </Voice> ";

 $item _str = sprintf ($ITEMTPL, $voiceArray [' MediaId ']); $TEXTTPL = "<xml> <tousername><! [cdata[%s]]></tousername> <fromusername><! [cdata[%s]]></fromusername> <CreateTime>%s</CreateTime> <msgtype><!

 [cdata[voice]]></msgtype> $item _str </xml> ";
 $result = sprintf ($TEXTTPL, $object->fromusername, $object->tousername, Time ());
 return $result; * * * Reply video message/Private function Transmitvideo ($object, $videoArray) {$itemTpl = "<Video> <mediaid>& lt;!
[cdata[%s]]></mediaid> <thumbmediaid><! [cdata[%s]]></thumbmediaid> <title><! [cdata[%s]]></title> <description><!

 [cdata[%s]]></description> </Video> "; $item _str = sprintf ($ITEMTPL, $videoArray [' MediaId '], $videoArray [' Thumbmediaid '], $videoArray [' Title '], $videoArray

 [' Description ']); $TEXTTPL = "<xml> <tousername><! [cdata[%s]]></tousername> <fromusername><! [cdata[%s]]></fromusername> <CreateTime>%s</CreateTime> <msgtype><!

 [cdata[video]]></msgtype> $item _str </xml> ";
 $result = sprintf ($TEXTTPL, $object->fromusername, $object->tousername, Time ());
 return $result;

 * * * Reply message/private function transmitnews ($object, $arr _item) {if (!is_array ($arr _item)) return; $ITEMTPL = "<item> <title><! [cdata[%s]]></title> <description><! [cdata[%s]]></description> <picurl><! [cdata[%s]]></picurl> &Lt url><!
 [cdata[%s]]></url> </item> ";
 $item _str = ""; foreach ($arr _item as $item) $item _str. = sprintf ($ITEMTPL, $item [' Title '], $item [' Description '], $item [' Picurl '], $item

 [' URL ']]; $NEWSTPL = "<xml> <tousername><! [cdata[%s]]></tousername> <fromusername><! [cdata[%s]]></fromusername> <CreateTime>%s</CreateTime> <msgtype><! [cdata[news]]></msgtype> <content><! [cdata[]]></content> <ArticleCount>%s</ArticleCount> <Articles> $item _str</articles

 > </xml> ";
 $result = sprintf ($NEWSTPL, $object->fromusername, $object->tousername, Time (), COUNT ($arr _item));
 return $result; * * Reply to music message/Private function Transmitmusic ($object, $musicArray) {$itemTpl = "<Music> <title>&lt ;! [cdata[%s]]></title> <description><! [cdata[%s]]></description> <musicurl><! [cdata[%s]]></musicurl> &Lt hqmusicurl><!

 [cdata[%s]]> 

3. Interface

3.1 What is the interface

The interface is equivalent to a tool that has a specific function. For example, when you are building a house, you need to drill holes in the wall, and you will use rig tools to drill holes. From the transfer tool to the completion of the drilling, you have to complete the plug-in, calibration, drilling and so on a series of steps, and ultimately achieve your goal. The rig is our interface, the plug-in, calibration, drilling is the tool we call to complete the purpose of the step.

Example of a micro-letter creation menu interface.

To invoke an interface:
1, access to the micro-letter menu Interface Connection address, through the curl function to establish a dialogue with this interface.
2, the creation of the menu data sent to this interface.
Interface calls are complete, and this interface automatically processes the data and generates menus on the micro-public good page.

How to call the micro-communication interface look at the next chapter: Micro-trust public Platform Development (III): Micro-credit advanced interface calls.

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.