Php WeChat public platform interface development-smart reply development tutorial, php public

Source: Internet
Author: User

Php public platform interface development-smart reply development tutorial, php public

This article describes how to implement the smart reply function of php public platform interface development. We will share this with you for your reference. The details are as follows:

Intelligent reply is based on the user input conditions to feedback the results of the user, this small series has previously been a letter to sort out some examples for your reference, relatively complete is mainly introduced in the development end.

Since its launch, it has been a hit, and the launch of the payment function has pushed it to an incomparable height. Then, people who apply for a subscription number or service number have begun to compete. Below I will give you a brief introduction to the public platform development interface.

Go to the public platform to apply for an account, and then follow the prompts step by step. When selecting a subscription number and a service number, an individual can only apply for a subscription number, which is limited to basic functions. enterprises can apply for both. The difference between a subscription number and a service number is that a message can be sent to a group every day, and a service number can be sent to a group only once every month. A subscription number must be authenticated to customize the menu (for enterprises to authenticate, the service number has a custom menu at the beginning, but it can also be authenticated. After authentication, the service number directly upgrades the advanced functions. For more differences, please Baidu...

I applied for a subscription number because it is an individual. Just upload a head photo with your ID card in your hand. It's silly. Wait for the Information Registration review (about one day ). Go to the public platform directly, click the function to enter the advanced function, disable the editing mode, enable the development mode, download the provided demo, decompress the package, and access the file wx_sample.php. The Code is as follows:

<?php/** * wechat php test *///define your tokendefine("TOKEN", "weixin");$wechatObj = new wechatCallbackapiTest();$wechatObj->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 be due to the different environments $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];    //extract post data if (!emptyempty($postStr)){        $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);        $fromUsername = $postObj->FromUserName;        $toUsername = $postObj->ToUserName;        $keyword = trim($postObj->Content);        $time = time();        $textTpl = "<xml>    <ToUserName><![CDATA[%s]]></ToUserName>    <FromUserName><![CDATA[%s]]></FromUserName>    <CreateTime>%s</CreateTime>    <MsgType><![CDATA[%s]]></MsgType>    <Content><![CDATA[%s]]></Content>    <FuncFlag>0</FuncFlag>    </xml>";  if(!emptyempty( $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() {    $signature = $_GET["signature"];    $timestamp = $_GET["timestamp"];    $nonce = $_GET["nonce"]; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr, SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){  return true; }else{  return false; } }}?>

In fact, authentication is used to send messages. Upload the file to your server, put it in the root directory, and modify the url and token value in development mode. Assume that the url used here is. Click Submit and you will be prompted that you have succeeded. Then, scan the number you applied for and send a message, and you will find that there is no response. In this case, we need to make a small adjustment to close the interface document to call the authentication method, enable the call to process the reply information:

//$wechatObj->valid();$wechatObj->responseMsg();

At this time, you will receive a message: Welcome to wechat world!

Is there a message immediately after you have followed some subscription numbers or service numbers. What is Reply 1, how is Reply 2, how is it, and so on.

Take my own blog as an example. My comments are:

Thank you for your interest in AndyYang's personal blog assistant.
Reply [1] and return two latest articles
Reply [2] Return two popular articles
Reply [3] Return two hot comments
Reply [4] Back to two latest technical articles
Reply [5] Back to two newly written articles
Reply to two other articles that return search keywords
For more highlights, visit www.jb51.net. Thank you for your support ~

How can this be implemented? Directly run the Code:

<? Php/*** wechat php test * // define your tokendefine ("TOKEN", "weixin"); $ wechatObj = new wechatCallbackapiTest (); // $ wechatObj-> valid (); $ wechatObj-> responseMsg (); class wechatCallbackapiTest {public function valid () {$ echoStr = $ _ GET ["echostr"]; // valid signature, option if ($ this-> checkSignature () {echo $ echoStr; exit ;}} public function responseMsg () {// get post data, may be due to the differen T environments $ postStr = $ GLOBALS ["HTTP_RAW_POST_DATA"]; // extract post data if (! Empty ($ postStr) {$ postObj = simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA); $ fromUsername = $ postObj-> FromUserName; $ toUsername = $ postObj-> ToUserName; $ keyword = trim ($ postObj-> Content); $ time = time (); $ MsgType = $ postObj-> MsgType; // add $ textTpl = "<xml> <ToUserName> <! [CDATA [% s]> </ToUserName> <FromUserName> <! [CDATA [% s]> </FromUserName> <CreateTime> % s </CreateTime> <MsgType> <! [CDATA [% s]> </MsgType> <Content> <! [CDATA [% s]> </Content> <FuncFlag> 0 </FuncFlag> </xml> "; if ($ MsgType! = 'Event') {if (! Empty ($ keyword) {$ msgType = "text"; $ contentStr = "Welcome to wechat world! ";}Else {echo" Input something... ";}} else {$ msgType =" text "; $ contentStr =" Thank you for your interest in AndyYang personal blog assistant. \ R \ n ". "Reply [1] Back to two latest articles \ r \ n ". "Reply [2] Return two popular articles \ r \ n ". "Reply [3] Return two hot comments \ r \ n ". "Reply [4] Back to two latest technical articles \ r \ n ". "Reply [5] Back to two latest writing articles \ r \ n ". "reply to two other articles that return the search keyword \ r \ n ". "For more information, see <a href = 'HTTP: // www.bkjia.com '> www.jb51.net </a>. Thank you for your support ~ ";}$ ResultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr); echo $ resultStr;} else {echo ""; exit ;}} private function checkSignature () {$ signature = $ _ GET ["signature"]; $ timestamp =$ _ GET ["timestamp"]; $ nonce = $ _ GET ["nonce"]; $ token = TOKEN; $ tmpArr = array ($ token, $ timestamp, $ nonce); sort ($ tmpArr, SORT_STRING ); // Add the second parameter (compare items as strings) $ tmpStr = implode ($ tmpArr); $ tmpStr = sha1 ($ tmpStr) in the new sdk ); if ($ tmpStr ==$ signature) {return true;} else {return false ;}}}

Of course, this is just a simple implementation. In fact, there are many msgtype types, even if the message type is event, it also contains subscribe, LOCATION, and so on. If it is refined, the Event is used as the subscribe to handle the first-time attention Event. The Code is as follows:

<? Phpdefine ("TOKEN", "weixin"); $ wechatObj = new wechatCallbackapiTest (); $ wechatObj-> weixin_run (); class wechatCallbackapiTest {private $ fromUsername; private $ toUsername; private $ times; private $ keyword; private $ MsgType; public function responseMsg () {$ postStr = $ GLOBALS ["HTTP_RAW_POST_DATA"]; if (! Emptyempty ($ postStr) {$ postObj = simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA); $ this-> fromUsername = $ postObj-> FromUserName; $ this-> toUsername = $ postObj-> ToUserName; $ this-> keyword = trim ($ postObj-> Content); $ this-> time = time (); $ this-> MsgType = $ postObj-> MsgType;} else {echo "Pay attention to <a href = 'HTTP: // {$ _ SERVER ['HTTP _ host']} '> HTTP: // {$ _ SERVER ['HTTP _ host']} </a>, t Hanks! "; Exit ;}} public function weixin_run () {$ this-> responseMsg (); if ($ this-> MsgType! = 'Event') {// attention $ data = $ this-> getData (); $ this-> fun_xml ("news", $ data, count ($ data);} else {$ data = $ this-> getWelData (); $ this-> fun_xml ("text", $ data, 1 );}} // type: text type, news text type // text, array (content), array (ID) // news, array (title, Introduction, image, hyperlinks ),... smaller than 10), number of private function fun_xml ($ type, $ value_arr, $ count) {$ con = "<xml> <ToUserName> <! [CDATA [{$ this-> fromUsername}]> </ToUserName> <FromUserName> <! [CDATA [{$ this-> toUsername}]> </FromUserName> <CreateTime >{$ this-> times} </CreateTime> <MsgType> <! [CDATA [{$ type}]> </MsgType> "; switch ($ type) {case" text ": $ con. =" <Content> <! [CDATA [$ value_arr]> </Content> "; break; case" news ": $ con. = "<ArticleCount >{$ count }</ArticleCount> <Articles>"; foreach ($ value_arr as $ key =>$ v) {$ con. = "<item> <Title> <! [CDATA [{$ v [0]}]> </Title> <Description> <! [CDATA [{$ v [1]}]> </Description> <PicUrl> <! [CDATA [{$ v [2]}]> </PicUrl> <Url> <! [CDATA [{$ v [3]}]> </Url> </item> ";}$ con. = "</Articles>"; break;} echo $ con. "</xml>" ;}private function getData () {// The database queries an article by keyword //............ //............ // Return $ data;} private function getWelData () {$ data = "thank you for choosing AndyYang personal blog assistant. \ R \ n ". "Reply [1] Back to two latest articles \ r \ n ". "Reply [2] Return two popular articles \ r \ n ". "Reply [3] Return two hot comments \ r \ n ". "Reply [4] Back to two latest technical articles \ r \ n ". "Reply [5] Back to two latest writing articles \ r \ n ". "reply to two other articles that return the search keyword \ r \ n ". "For more information, see <a href = 'HTTP: // www.bkjia.com/'> www.jb51.net </a>. Thank you for your support ~ "; Return $ data ;}}

To be honest, I really want to get a service number for fun. The custom menu has no technical content, but the only service of the service number, such as payment, is worth trying.

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.