Php code full version of the public platform development interface PHPSDK
ResponseMsg ();} else {$ wechatObj-> valid ();} class wechatCallbackapiTest {// verify the signature public function valid () {$ echoStr = $ _ GET ["echostr"]; $ 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) {echo $ ec HoStr; exit ;}/// response message public function responseMsg () {$ postStr = $ GLOBALS ["HTTP_RAW_POST_DATA"]; if (! Empty ($ postStr) {$ this-> logger ("R \ r \ n ". $ postStr); $ postObj = simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA); $ RX_TYPE = trim ($ postObj-> MsgType ); if ($ postObj-> MsgType = "event ") & ($ postObj-> Event = "subscribe" | $ postObj-> Event = "unsubscribe ")) {// filter follow and cancel follow events} else {} // message type separation switch ($ RX_TYPE) {case "event ": $ result = $ this-> receiveEvent ($ postObj); break; case "text": if (strstr ($ postObj-> Content, "third party ")) {$ result = $ this-> relayPart3 (" http://www.fangbei.org/test.php ".'? '. $ _ SERVER ['query _ string'], $ postStr);} else {$ result = $ this-> receiveText ($ postObj);} break; case "image ": $ result = $ this-> receiveImage ($ postObj); break; case "location": $ result = $ this-> cancelocation ($ postObj); break; case "voice ": $ result = $ this-> incluevoice ($ postObj); break; case "video": $ result = $ this-> receiveVideo ($ postObj); break; case "link ": $ result = $ this-> cancelink ($ postObj ); Break; default: $ result = "unknown msg type :". $ RX_TYPE; break;} $ this-> logger ("T \ r \ n ". $ result); echo $ result;} else {echo ""; exit ;}// receives the event message private function receiveEvent ($ object) {$ content = ""; switch ($ object-> Event) {case "subscribe": $ content = "Welcome to fanwe studio"; $ content. = (! Empty ($ object-> EventKey ))? ("\ N from the QR code scenario ". str_replace ("qrscene _", "", $ object-> EventKey): ""; break; case "unsubscribe": $ content = "cancel follow"; break; case "CLICK": switch ($ object-> EventKey) {case "COMPANY": $ content = array (); $ content [] = array ("Title" => "Fang times Studio", "Description" => "", "PicUrl" =>" http://discuz.comli.com/weixin/weather/icon/cartoon.jpg "," Url "=>" http://m.cnblogs.com/?u=txw1958 "); Break; default: $ content =" click menu :". $ object-> EventKey; break;} break; case "VIEW": $ content = "redirect link ". $ object-> EventKey; break; case "SCAN": $ content = "SCAN scenario ". $ object-> EventKey; break; case "LOCATION": $ content = "upload LOCATION: latitude ". $ object-> Latitude. "; Longitude ". $ object-> longpolling; break; case "scancode_waitmsg": if ($ object-> ScanCodeInfo-> ScanType = "qrcode") {$ content = "scan with prompt: type QR code result :". $ object-> ScanCodeInfo-> ScanResult;} else if ($ object-> ScanCodeInfo-> ScanType = "barcode") {$ codeinfo = explode (",", strval ($ object-> ScanCodeInfo-> ScanResult); $ codeValue = $ codeinfo [1]; $ content = "scan the QR code with the prompt: type bar code result :". $ codeValue;} else {$ content = "scan with prompt: type ". $ object-> ScanCodeInfo-> ScanType. "Result :". $ object-> ScanCodeInfo-> ScanResult;} break; case "scancode_push": $ content = "Code scanning event"; break; case "pic_sysphoto ": $ content = "system photos"; break; case "pic_weixin": $ content = "album photos: Quantity ". $ object-> SendPicsInfo-> Count; break; case "pic_photo_or_album": $ content = "photo or album: Quantity ". $ object-> SendPicsInfo-> Count; break; case "location_select": $ content = "sending location: Tag ". $ object-> SendLocationInfo-> Label; break; default: $ content = "receive a new event :". $ object-> Event; break;} if (is_array ($ content) {if (isset ($ content [0] ['picurl']) {$ result = $ this-> transmitNews ($ object, $ content);} else if (isset ($ content ['musicurl']) {$ result = $ this-> transmitMusic ($ object, $ content) ;}} else {$ result = $ this-> transmitText ($ object, $ content );} return $ result;} // private function for receiving text messages eText ($ object) {$ keyword = trim ($ object-> Content ); // multi-Customer Service Manual Reply mode if (strstr ($ keyword, "Are you there?") | strstr ($ keyword, "online customer service ")) {$ result = $ this-> transmitService ($ object); return $ result;} // if (strstr ($ keyword, "text ")) {$ content = "this is a text message";} else if (strstr ($ keyword, "emoticons") {$ content = "China :". $ this-> bytes_to_emoji (0x1F1E8 ). $ this-> bytes_to_emoji (0x1F1F3 ). "\ n cactus :". $ this-> bytes_to_emoji (0x1F335);} else if (strstr ($ keyword, "Single Image") {$ content = array (); $ content [] = array ("Title" => "single text Title", "Description" => "single text content", "PicUrl" =>" http://discuz.comli.com/weixin/weather/icon/cartoon.jpg "," Url "=>" http://m.cnblogs.com/?u=txw1958 ");} Else if (strstr ($ keyword," 文" ") | strstr ($ keyword," 文" ") {$ content = array (); $ content [] = array ("Title" => "multi-text 1 Title", "Description" => "", "PicUrl" =>" http://discuz.comli.com/weixin/weather/icon/cartoon.jpg "," Url "=>" http://m.cnblogs.com/?u=txw1958 "); $ Content [] = array (" Title "=>" multi-Text 2 Title "," Description "=>" "," PicUrl "=>" http://d.hiphotos.bdimg.com/wisegame/pic/item/f3529822720e0cf3ac9f1ada0846f21fbe09aaa3.jpg "," Url "=>" http://m.cnblogs.com/?u=txw1958 "); $ Content [] = array (" Title "=>" multi-image 3 Title "," Description "=>" "," PicUrl "=>" http://g.hiphotos.bdimg.com/wisegame/pic/item/18cb0a46f21fbe090d338acc6a600c338644adfd.jpg "," Url "=>" http://m.cnblogs.com/?u=txw1958 ");} Else if (strstr ($ keyword," music ") {$ content = array (); $ content = array ("Title" => "the most dazzling national style", "Description" => "singer: Phoenix Legend", "MusicUrl" =>" http://121.199.4.61/music/zxmzf.mp3 "," HQMusicUrl "=>" http://121.199.4.61/music/zxmzf.mp3 ");} Else {$ content = date (" Y-m-d H: I: s ", time ()). "\ nOpenID :". $ object-> FromUserName. "\ n technical support Fang times studio";} if (is_array ($ content) {if (isset ($ content [0]) {$ result = $ this-> transmitNews ($ object, $ content);} else if (isset ($ content ['musicurl']) {$ result = $ this-> transmitMusic ($ object, $ content) ;}} else {$ result = $ this-> transmitText ($ object, $ content );} return $ result;} // receives the image message private function recei VeImage ($ object) {$ content = array ("MediaId" => $ object-> MediaId); $ result = $ this-> transmitImage ($ object, $ content ); return $ result;} // receive location message private function cancelocation ($ object) {$ content = "You are sending location, Longitude :". $ object-> Location_Y. "; Latitude :". $ object-> Location_X. "; zoom level :". $ object-> Scale. "; Location :". $ object-> Label; $ result = $ this-> transmitText ($ object, $ content); return $ result;} // receives the voice message private func Tion into eVoice ($ object) {if (isset ($ object-> Recognition )&&! Empty ($ object-> Recognition) {$ content = "What you just said :". $ object-> Recognition; $ result = $ this-> transmitText ($ object, $ content );} else {$ content = array ("MediaId" => $ object-> MediaId); $ result = $ this-> transmitVoice ($ object, $ content );} return $ result;} // receives the video message. private function receiveVideo ($ object) {$ content = array ("MediaId" => $ object-> MediaId, "ThumbMediaId" => $ object-> ThumbMediaId, "Title" => "", "Desc Ription "=>" "); $ result = $ this-> transmitVideo ($ object, $ content); return $ result ;} // receive the link message private function cancelink ($ object) {$ content = "You sent a link with the title :". $ object-> Title. "; content :". $ object-> Description. "; link address :". $ object-> Url; $ result = $ this-> transmitText ($ object, $ content); return $ result;} // reply to the private function transmitText ($ object, $ content) {if (! Isset ($ content) | empty ($ content) {return "" ;}$ xmlTpl ="
%s
%s
% S
text
%s
"; $ Result = sprintf ($ xmlTpl, $ object-> FromUserName, $ object-> ToUserName, time (), $ content); return $ result ;} // reply to the text message private function transmitNews ($ object, $ newsArray) {if (! Is_array ($ newsArray) {return "" ;}$ itemTpl ="
<! [CDATA [% s]>
%s
%s
%s
"; $ Item_str =" "; foreach ($ newsArray as $ item) {$ item_str. = sprintf ($ itemTpl, $ item ['title'], $ item ['description'], $ item ['picurl'], $ item ['URL']);} $ xmlTpl ="
%s
%s
% S
news
% S$ Item_str
"; $ Result = sprintf ($ xmlTpl, $ object-> FromUserName, $ object-> ToUserName, time (), count ($ newsArray); return $ result ;} // Reply to music message private function transmitMusic ($ object, $ musicArray) {if (! Is_array ($ musicArray) {return "" ;}$ itemTpl ="
<! [CDATA [% s]>
%s
%s
%s
"; $ Item_str = sprintf ($ itemTpl, $ musicArray ['title'], $ musicArray ['description'], $ musicArray ['musicurl'], $ musicArray ['hqmusicurl']); $ xmlTpl ="
%s
%s
% S
music
$ Item_str
"; $ Result = sprintf ($ xmlTpl, $ object-> FromUserName, $ object-> ToUserName, time (); return $ result ;} // reply to the image message private function transmitImage ($ object, $ imageArray) {$ itemTpl ="
%s
"; $ Item_str = sprintf ($ itemTpl, $ imageArray ['mediaid ']); $ xmlTpl ="
%s
%s
% S
image
$ Item_str
"; $ Result = sprintf ($ xmlTpl, $ object-> FromUserName, $ object-> ToUserName, time (); return $ result ;} // reply to the voice message private function transmitVoice ($ object, $ voiceArray) {$ itemTpl ="
%s
"; $ Item_str = sprintf ($ itemTpl, $ voiceArray ['mediaid ']); $ xmlTpl ="
%s
%s
% S
voice
$ Item_str
"; $ Result = sprintf ($ xmlTpl, $ object-> FromUserName, $ object-> ToUserName, time (); return $ result ;} // reply to the video message private function transmitVideo ($ object, $ videoArray) {$ itemTpl ="
%s
%s
<! [CDATA [% s]>
%s
"; $ Item_str = sprintf ($ itemTpl, $ videoArray ['mediaid '], $ videoArray ['thumbmediaid'], $ videoArray ['title'], $ videoArray ['description']); $ xmlTpl ="
%s
%s
% S
video
$ Item_str
"; $ Result = sprintf ($ xmlTpl, $ object-> FromUserName, $ object-> ToUserName, time (); return $ result ;} // reply to multiple customer service messages private function transmitService ($ object) {$ xmlTpl ="
%s
%s
% S
transfer_customer_service
"; $ Result = sprintf ($ xmlTpl, $ object-> FromUserName, $ object-> ToUserName, time (); return $ result ;} // reply to the message private function relayPart3 ($ url, $ rawData) {$ headers = array ("Content-Type: text/xml; charset = utf-8") from a third-party interface "); $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, expires, 1); curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ headers ); curl_setopt ($ ch, CURLOPT_POST, 1 ); Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ rawData); $ output = curl_exec ($ ch); curl_close ($ ch); return $ output ;} // function bytes_to_emoji ($ cp) {if ($ cp> 0x10000) {#4 bytes return chr (0xF0 | ($ cp & 0x1C0000)> 18 )). chr (0x80 | ($ cp & 0x3F000)> 12 )). chr (0x80 | ($ cp & 0xFC0)> 6 )). chr (0x80 | ($ cp & 0x3F);} else if ($ cp> 0x800) {#3 bytes return chr (0xE0 | ($ cp & 0xF000)> 12 )). c Hr (0x80 | ($ cp & 0xFC0)> 6 )). chr (0x80 | ($ cp & 0x3F);} else if ($ cp> 0x80) {#2 bytes return chr (0xC0 | ($ cp & 0x7C0)> 6 )). chr (0x80 | ($ cp & 0x3F);} else {#1 byte return chr ($ cp) ;}// log record private function logger ($ log_content) {if (isset ($ _ SERVER ['http _ appname']) {// SAE sae_set_display_errors (false); sae_debug ($ log_content); sae_set_display_errors (true );} else if ($ _ SERVER ['remote _ ADDR ']! = "127.0.0.1") {// LOCAL $ max_size = 1000000; $ log_filename = "log. xml "; if (file_exists ($ log_filename) and (abs (filesize ($ log_filename) >$ max_size) {unlink ($ log_filename);} file_put_contents ($ log_filename, date ('Y-m-d H: I: s '). "". $ log_content. "\ r \ n", FILE_APPEND) ;}}?>