Public platform-send passive response message-PHP example & lt ;? Php $ testObj = newTest (); if (! Empty ($ _ GET ['echostr']) {$ testObj-& gt; valid () ;}else {$ testObj-& gt; responseMsg public platform-send passive response message-PHP example
Valid () ;}else {$ testObj-> responseMsg () ;}exit; class Test {/*** bind url, token information */public function valid () {$ echoStr = $ _ GET ["echostr"]; if ($ this-> checkSignature () {echo $ echoStr;} exit ();} /*** check the signature to ensure that the request is sent from */private function checkSignature () {$ signature = $ _ GET ["signature"]; $ timestamp = $ _ GET ["timestamp"]; $ nonce = $ _ GET ["nonce"]; $ token = "test123"; // consistent with the configured token, do not disclose $ tmpArr = array ($ token, $ Timestamp, $ nonce); sort ($ tmpArr); $ tmpStr = implode ($ tmpArr); $ tmpStr = sha1 ($ tmpStr); if ($ tmpStr = $ signature) {return true;} else {return false;}/*** receives the message and automatically sends the response information */public function responseMsg () {// verify the signature if ($ this-> checkSignature () {$ postStr = $ GLOBALS ["HTTP_RAW_POST_DATA"]; $ this-> log_request_info (); // Extract post data if (! Empty ($ postStr) {$ postObj = simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA); $ fromUsername = $ postObj-> FromUserName; // sender $ toUsername = $ postObj-> ToUserName; // receiver $ MsgType = $ postObj-> MsgType; // Message Type $ MsgId = $ postObj-> MsgId; // message id $ time = time (); // The current time is used as the reply time // if it is a text message (the expression is a text message) if ($ MsgType = 'text ') {$ content = trim ($ postObj-> Content); // if (! Empty ($ content) {// if the text content is text, the text information is returned; otherwise, the text information is returned if ($ content = "text ") {// reply to text message, ArticleCount text message count, multiple text message information, the default first item is a large image $ ArticleCount = 2; $ newsTpl ="
%s
%s
% S
%s
% S
<! [CDATA [% s]>
%s
%s
%s
<! [CDATA [% s]>
%s
%s
%s
"; $ ResultStr = sprintf ($ newsTpl, $ fromUsername, $ toUsername, $ time, 'news', $ ArticleCount, 'I am a graphic info',' I am a descriptive info ', 'http: // www.test.com/DocCenterService/image? Photo_id = 236 ', 'http: // www.test.com', 'Love City Network officially activated online', 'Description 2', 'http: // success, 'http: // www.test.com '); echo $ resultStr; $ this-> log ($ resultStr);} else {// reply to text information $ textTpl ="
%s
%s
% S
%s
%s
0
"; $ ContentStr = 'Your message is: recipient :'. $ toUsername. ', Sender :'. $ fromUsername. ', message type :'. $ MsgType. ', message content :'. $ content. 'www.icity365.com '; $ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, 'text', $ contentStr); echo $ resultStr; $ this-> log ($ resultStr) ;}} else {echo "Input something... "; $ this-> log ($ resultStr);} // if it is an image message} elseif ($ MsgType = 'image') {$ MediaId = $ postObj-> MediaId; // Image Message Media id, which can call multiple media Object download interface to pull data. $ ImageTpl ="
%s
%s
% S
%s
%s
"; $ ResultStr = sprintf ($ imageTpl, $ fromUsername, $ toUsername, $ time, $ MsgType, $ MediaId); echo $ resultStr; $ this-> log ("automatically responding to image information"); $ this-> log ($ resultStr ); // if it is a video} else if ($ MsgType = 'video') {$ MediaId = $ postObj-> MediaId; // The id of the video message Media, you can call the multimedia file download interface to pull data. $ ThumbMediaId = $ postObj-> ThumbMediaId; // Media id of the video message thumbnail. you can call the multimedia file download interface to pull data. $ VideoTpl ="
%s
%s
% S
%s
%s
%s
<! [CDATA [% s]>
%s
"; $ ResultStr = sprintf ($ videoTpl, $ fromUsername, $ toUsername, $ time, $ MsgType, $ MediaId, $ ThumbMediaId, 'I am title ', 'I am description'); echo $ resultStr; $ this-> log ("automatically responding to video information ". $ ThumbMediaId); $ this-> log ($ resultStr); // if it is a geographical location} else if ($ MsgType = 'location ') {$ Location_X = $ postObj-> Location_X; // dimension $ Location_Y = $ postObj-> Location_Y; // longitude $ Scale = $ postObj-> Scale; // Map zoom size $ Label = $ postObj-> Label; // location information in the ground // reply to text information $ textTpl ="
%s
%s
% S
%s
%s
0
"; $ MsgType =" text "; $ contentStr = 'longitude :'. $ Location_Y. ', dimension :'. $ Location_X. ', map zoom size '. $ Scale. ', geographic location information :'. $ Label; $ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr); echo $ resultStr; $ this-> log ($ resultStr); // if it is an event} else if ($ MsgType = 'event') {$ Event = $ postObj-> Event; // subscribe (follow, also called subscription) if ($ Event = 'subscribe') {$ EventKey = $ postObj-> EventKey; // Event KEY value, qrscene _ Is Prefix, followed by the parameter value of the QR code // if (! Empty ($ EventKey) {$ Ticket = $ postObj-> Ticket; // the ticket of the QR code, which can be exchanged for the QR code image $ this-> log ($ fromUsername. 'scan the QR code to follow the instructions! EventKey = '. $ EventKey.', Ticket = '. $ Ticket);} else {$ this-> log ($ fromUsername.' follow me! ');} // Unsubscribe (unsubscribe)} elseif ($ Event = 'unsubscribe') {$ this-> log ($ fromUsername.' You have canceled following me! '); // SCAN the QR code Event} elseif ($ Event = 'scan' | $ Event = 'scan ') {$ EventKey = $ postObj-> EventKey; // event KEY value, which is a 32-bit unsigned integer, that is, the two-dimensional code scene_id $ Ticket = $ postObj-> Ticket; // the ticket of the QR code, which can be used in exchange for the QR code image $ this-> log ($ fromUsername. 'Follow me! EventKey = '. $ EventKey. ', Ticket = '. $ Ticket); // menu CLICK Event} elseif ($ Event = 'click') {$ EventKey = $ postObj-> EventKey; // Event KEY value, corresponds to the KEY value in the custom menu interface // replies to text information $ textTpl ="
%s
%s
% S
%s
%s
0
"; $ ContentStr = 'you clicked the menu and the menu item key = '. $ EventKey; $ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, 'text', $ contentStr); echo $ resultStr; $ this-> log ($ resultStr); // other event types} else {$ this-> log ('event type :'. $ Event);} // other message types, links, voice, etc.} else {// reply text information $ textTpl ="
%s
%s
% S
%s
%s
0
"; $ ContentStr = 'message type: '. $ MsgType.' We haven't processed it yet .... [City network] '; $ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, 'text', $ contentStr); echo $ resultStr; $ this-> log ($ resultStr) ;}} else {echo "; exit ;}} else {$ this-> log (" signature verification failed! ") ;}}/*** Record request information */function log_request_info () {$ post =''; foreach ($ _ POST as $ key => $ value) {$ post = $ post. $ key. ':'. $ value. ',';} $ get = ''; foreach ($ _ GET as $ key => $ value) {$ get = $ get. $ key. ':'. $ value. ',';} $ this-> log ("get information :". $ get); $ this-> log ("post information :". $ post);}/*** record log * @ param $ str * @ param $ mode */function log ($ str) {$ mode = 'a '; // Append $ file = "log.txt"; $ oldmask = @ umas K (0); $ fp = @ fopen ($ file, $ mode); @ flock ($ fp, 3); if (! $ Fp) {Return false;} else {@ fwrite ($ fp, $ str); @ fclose ($ fp); @ umask ($ oldmask ); return true ;}}?>
?
View more information: http://mp.weixin.qq.com/wiki/index.php? Title = send passive response message