Getting started with WeChat public platform development-PHP, which enables automatic reply to text, text, and click events

Source: Internet
Author: User
One page of code to implement the basic reply and click event function, deploy it to sae or bae, the appropriate basic free server doesn't understand the code, basically everyone can make their own public account MsgType ); switch ($ RX_TYPE ){

One page of code to implement the basic reply and click event function, deploy it to sae or bae, and properly implement the basic free server

Almost everyone can create their own public accounts without understanding the code.

 

 Valid ();} else {// reply message $ wechatObj-> responseMsg ();} class wechatCallbackapiTest {public function valid () {$ echoStr = $ _ GET ["echostr"]; if ($ this-> checkSignature () {echo $ echoStr; exit ;}} private function checkSignature () {$ signature = $ _ GET ["signature"]; $ timestamp = $ _ GET ["timestamp"]; $ nonce = $ _ GET ["nonce"]; $ token = TOKEN; $ tmpArr = array ($ token, $ timestamp, $ nonce); sort ($ tmpArr); $ tmpStr = I Mplode ($ tmpArr); $ tmpStr = sha1 ($ tmpStr); if ($ tmpStr ==$ signature) {return true;} else {return false ;}} // reply to the message public function responseMsg () {$ postStr = $ GLOBALS ["HTTP_RAW_POST_DATA"]; if (! Empty ($ postStr) {$ postObj = simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA); $ RX_TYPE = trim ($ postObj-> MsgType); switch ($ RX_TYPE) {case "text": $ resultStr = $ this-> receiveText ($ postObj); break; case "image": $ resultStr = $ this-> receiveImage ($ postObj ); break; case "location": $ resultStr = $ this-> cancelocation ($ postObj); break; case "voice": $ resultStr = $ this-> cancevoice ($ postObj ); break; case "video": $ resultStr = $ this-> receiveVideo ($ postObj); break; case "link": $ resultStr = $ this-> cancelink ($ postObj ); break; case "event": $ resultStr = $ this-> receiveEvent ($ postObj); break; default: $ resultStr = "unknow msg type :". $ RX_TYPE; break;} echo $ resultStr;} else {echo ""; exit ;}// receives the text message private function receiveText ($ object) {$ keyword = trim ($ object-> Content); $ url =" http://api100.duapp.com/movie/?appkey=DIY_miaomiao&name= ". $ Keyword; $ output = file_get_contents ($ url, $ keyword); $ contentStr = json_decode ($ output, true); if (is_array ($ contentStr )) {$ resultStr = $ this-> transmitNews ($ object, $ contentStr);} else {$ resultStr = $ this-> transmitText ($ object, $ contentStr );} return $ resultStr;} // receives events, followed by other private functions receiveEvent ($ object) {$ contentStr = ""; switch ($ object-> Event) {case "subscribe ": $ contentStr = "you followed me"; // The Reply content break; case "unsubscribe": $ contentStr = ""; break; case "CLICK ": $ contentStr = $ this-> export eclick ($ object); // Click the event break; default: $ contentStr = "receive a new event :". $ object-> Event; break;} return $ contentStr;} // receive Image private function receiveImage ($ object) {$ contentStr = "You sent an image: ". $ object-> PicUrl; $ resultStr = $ this-> transmitText ($ object, $ contentStr); return $ resultStr;} // receives the voice private function audio eVoice ($ object) {$ contentStr = "You sent a voice and the media ID is :". $ object-> MediaId; $ resultStr = $ this-> transmitText ($ object, $ contentStr); return $ resultStr;} // receives the video private function receiveVideo ($ object) {$ contentStr = "You sent a video with the media ID :". $ object-> MediaId; $ resultStr = $ this-> transmitText ($ object, $ contentStr); return $ resultStr;} // location message private function cancelocation ($ object) {$ contentStr = "You sent the location with the latitude :". $ object-> Location_X. "; Longitude :". $ object-> Location_Y. "; zoom level :". $ object-> Scale. "; Location :". $ object-> Label; $ resultStr = $ this-> transmitText ($ object, $ contentStr); return $ resultStr;} // link message private function cancelink ($ object) {$ contentStr = "You sent a link with the title :". $ object-> Title. "; content :". $ object-> Description. "; link address :". $ object-> Url; $ resultStr = $ this-> transmitText ($ object, $ contentStr); return $ resultStr ;}

// Click the menu message private function javaseclick ($ object) {switch ($ object-> EventKey) {case "1": $ contentStr = "cat sauce personalized DIY clothing, our professional customization personality [class clothes, couple clothes, parent-child clothes, etc., long and short T-shirts, sweaters, long shorts] can be printed on a picture, to give you a warm and lovely TA, something can be left a message "; break; case "2": $ contentStr = "you clicked the menu :". $ object-> EventKey; break; case "3": $ contentStr = "silly"; break; default: $ contentStr = "you clicked the menu :". $ object-> EventKey; break;} // Two types of reply if (is_array ($ contentStr) {$ resultStr = $ this-> transmitNews ($ object, $ contentStr );} else {$ resultStr = $ this-> transmitText ($ object, $ contentStr);} return $ resultStr;} // reply to the text message private function transmitText ($ object, $ content) {$ textTpl =" % S "; $ ResultStr = sprintf ($ textTpl, $ object-> FromUserName, $ object-> ToUserName, time (), $ content); return $ resultStr ;}

// Reply to the private function transmitNews ($ object, $ arr_item) {if (! Is_array ($ arr_item) return;

$ ItemTpl =" "; $ Item_str =" "; foreach ($ arr_item as $ item) $ item_str. = sprintf ($ itemTpl, $ item ['title'], $ item ['description'], $ item ['picurl'], $ item ['URL']);

$ NewsTpl =" % S % S$ Item_str ";

$ ResultStr = sprintf ($ newsTpl, $ object-> FromUserName, $ object-> ToUserName, time (), count ($ arr_item); return $ resultStr ;} // music message private function transmitMusic ($ object, $ musicArray, $ flag = 0) {$ itemTpl =" ";

$ Item_str = sprintf ($ itemTpl, $ musicArray ['title'], $ musicArray ['description'], $ musicArray ['musicurl'], $ musicArray ['hqmusicurl']);

$ TextTpl =" % S $ Item_str % D ";

$ ResultStr = sprintf ($ textTpl, $ object-> FromUserName, $ object-> ToUserName, time (), $ flag); return $ resultStr ;}}?>

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.