Development code php code
Appid = $ appid; $ this-> appsecret = $ appsecret;} // verify the server address validity. public function valid () {if ($ this-> checkSignature ()) {$ echostr = $ _ GET ['chostr']; // random string return $ echostr;} else {return "Error ";}} // Check the signature private function checkSignature () {// 1. four parameter data submitted by the receiving server in GET Mode $ signature =$ _ GET ['signature']; // encrypted signature $ timestamp =$ _ GET ['timestamp']; // timestamp $ nonce =$ _ GET ['nonce ']; // random number // 2. Encryption/verification process // 1. token Time stamp and nonce parameters are sorted alphabetically. // bool sort (array & $ array [, int $ sort_flags = SORT_REGULAR]) orders arrays $ tmpArr = array (TOKEN, $ timestamp, $ nonce); // put the preceding three parameters in an array, sort ($ tmpArr, SORT_STRING); // 2. splice the three parameter strings into one string for sha1 encryption; $ tmpStr = implode ($ tmpArr); // Convert the array into a string $ signatureStr = sha1 ($ tmpStr ); // 3. the developer obtains the encrypted string and compares it with signature. If ($ signatureStr ==$ signature) {return true;} else {return false ;}// response message public function responseMsg () {// The receiving server sends a POST request to the developer server, carrying the XML data packet $ postData = $ GLOBALS ['http _ RAW_POST_DATA ']; // process xml data packets $ xmlObj = simplexml_load_string ($ postData, "SimpleXMLElement", LIBXML_NOCDATA); if (! $ XmlObj) {echo ""; exit;} // Obtain the parameter content in the received message $ toUserName = $ xmlObj-> ToUserName; // developer id $ fromUserName = $ xmlObj-> FromUserName; // sender's ID (openid) $ msgType = $ xmlObj-> MsgType; // message type switch ($ msgType) {// receive text message case 'text': // Get the keyword $ keyword = $ this-> receiveText ($ xmlObj ); // perform keyword reply switch ($ keyword) {case "w001": case "W001": return $ this-> replyText ($ xmlObj, "Hi ~ Hello "); break; case" w002 ": case" W002 ": return $ this-> replyText ($ xmlObj," Hi ~ Awkward "); break; case" joke ": $ key =" dee9ebc68fd5a61f67286063932afe56 "; return $ this-> replyNews ($ xmlObj, $ this-> joke_text ($ key )); break; default: $ key = "dee9ebc68fd5a61f67286063932afe56"; return $ this-> replyNews ($ xmlObj, $ this-> joke_text ($ key); break;} break; // receive image message case 'image': return $ this-> receiveImage ($ xmlObj); break; // receive event push case 'event ': return $ this-> receiveEvent ($ xmlObj); break ;}// receives the event Push public function receiveEvent ($ obj) {// receive event type $ Event = $ obj-> event; switch ($ event) {// follow Event case 'subscribe ': // send a welcome message $ newsArr = array ("Title" => "to make valuable headlines! "," Description "=>" grasp the value Toutiao news, and have more resources to talk about on a daily basis! "," PicUrl "=>" http://jober.applinzi.com/news/img/news.png "," Url "=>" http://jober.applinzi.com/news/index.php "); // Return $ this-> replyNews ($ obj, $ newsArr); break; // cancel the follow event case 'unsubscribe ': // account unbinding and other break; // custom menu push CLICK event case 'Click': $ eventKey = $ obj-> EventKey; // Obtain the event KEY value, switch ($ eventKey) {case 'old': $ weixinArr = $ this-> history ("da675ebc6a0d72920dca3f676122a693") corresponding to the KEY value in the custom menu interface "); $ weixinArr = array_slice ($ weixinArr, 0, 5); $ newsArr = array (); foreach ($ weixinArr as $ item) {$ newsArr = array ("Title" => $ item ['description'], "Description" => $ item ['title'], "PicUrl" =>" http://1.jober.applinzi.com/news/img/2.jpg "," Url "=>" http://www.todayonhistory.com/ ");} Return $ this-> replyNews ($ obj, $ newsArr); break;} break ;}// receives the text message public function receiveText ($ obj) {$ content = trim ($ obj-> Content); // return $ content;} // receives Image message public function receiveImage ($ obj) {$ picUrl = $ obj-> PicUrl; // image link $ mediaId = $ obj-> MediaId; // Image Message Media id return $ this-> replyImage ($ obj, $ mediaId);} // reply to the image message public function replyImage ($ obj, $ mediaId) {$ replyXml ="
%s
%s
% S
image
%s
"; Return sprintf ($ replyXml, $ obj-> FromUserName, $ obj-> ToUserName, time (), $ mediaId );} // reply to the text message public function replyText ($ obj, $ content) {$ replyXml ="
%s
%s
% S
text
%s
"; Return sprintf ($ replyXml, $ obj-> FromUserName, $ obj-> ToUserName, time (), $ content );} // reply to the text message public function replyNews ($ obj, $ newsArr) {// determine whether it is an array type if (! Is_array ($ newsArr) {return;} // checks whether the array is empty. if (! $ NewsArr) {return ;}$ itemStr = ""; // defines the item template $ itemXml ="
<! [CDATA [% s]>
%s
%s
%s
"; Foreach ($ newsArr as $ item) {$ itemStr. = sprintf ($ itemXml, $ item ['title'], $ item ['description'], $ item ['picurl'], $ item ['URL']);} $ replyXml ="
%s
%s
% S
news
". Count ($ newsArr )."". $ ItemStr ."
"; Return sprintf ($ replyXml, $ obj-> FromUserName, $ obj-> ToUserName, time ();} // encapsulate https requests (GET and POST) protected function https_request ($ url, $ data = null) {// 1. initialize curl $ ch = curl_init (); // 2. set the transmission option curl_setopt ($ ch, CURLOPT_URL, $ url); // request url curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // return the request result as a file stream if (! Empty ($ data) {curl_setopt ($ ch, CURLOPT_POST, 1); // request the POST method curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data ); // POST submitted content} // 3. execute the request and process the result $ outopt = curl_exec ($ ch ); // Convert json data into an array $ outoptArr = json_decode ($ outopt, TRUE); // 4. disable curl curl_close ($ ch ); // if the returned result $ outopt is json data, you need to determine if (is_array ($ outoptArr) {return $ outoptArr;} else {return $ outopt ;}} public function juhe_weixin ($ key, $ type) {$ url =" http://v.juhe.cn/toutiao/index?type= {$ Type} & key = {$ key} "; $ result = $ this-> https_request ($ url ); if ($ result ['error _ Code'] = 0) {return $ result ['result'] ['data'];} else {return array ();}} // aggregate data-get the latest interesting figure public function joke_text ($ key, $ pagesize = 10) {$ url =" http://japi.juhe.cn/joke/img/text.from?key= {$ Key} & pagesize = {$ pagesize} "; $ jokeArr = $ this-> https_request ($ url ); $ resultArr = $ jokeArr ['result'] ['data']; // $ content = $ resultArr [0] ['content']; // return $ this-> replyText ($ xmlObj, $ content); $ newsArr = array (); // Determine whether the joke interface obtains data if ($ jokeArr ['error _ Code'] = 0) {foreach ($ resultArr as $ item) {$ newsArr [] = array ("Title" => $ item ['content'], "Description" => $ item ['updatetime'], "PicUrl" => $ item ['URL'], "url" => $ item ['URL']) ;}} return $ newsArr ;} // aggregate data-obtain the historical public function history ($ key) {$ m = idate ('M'); $ d = idate ('D '); $ day = "{$ m}/{$ d}"; $ url =" http://v.juhe.cn/todayOnhistory/queryEvent.php?key= {$ Key} & date = {$ day} "; $ historyArr = $ this-> https_request ($ url); $ resultArr = $ historyArr ['result']; // $ content = $ resultArr ['title']; // return $ this-> replyText ($ xmlObj, $ content); $ newsArr = array (); // Determine whether the interface obtains data if ($ jokeArr ['error _ Code'] = 0) {foreach ($ resultArr as $ item) {$ newsArr [] = array ("Title" => $ item ['title'], "Description" => $ item ['Date'], "PicUrl" => "", "Url" => "") ;}return $ newsArr;} public function fund ($ key) {$ url =" http://japi.juhe.cn/jingzhi/query.from?key= {$ Key} "; $ fundArr = $ this-> https_request ($ url); $ resultArr = $ fundArr ['result']; // $ content = $ resultArr ['title']; // return $ this-> replyText ($ xmlObj, $ content); $ newsArr = array (); // Determine whether the interface obtains data if ($ jokeArr ['error _ Code'] = 0) {foreach ($ resultArr as $ item) {$ newsArr [] = array ("Title" => $ item ['day'], "Description" => $ item ['title'], "PicUrl" => "", "Url" =>" http://www.baidu.com ") ;}} Return $ newsArr ;} /*** obtain the interface call credential access_token in the basic support and cache the access_token * @ return access_token string interface credential ***/public function getAccessToken () {// Get the access_token $ access_token = $ this-> _ memcache_get ("access_token") of the memcache cache; // if the cached access_token is invalid if (! $ Access_token) {// Obtain the interface credential to obtain access_token $ url =" https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid= {$ This-> appid} & secret = {$ this-> appsecret} "; $ outoptArr = $ this-> https_request ($ url); if (! Isset ($ outoptArr ['errcode']) {// memcache cache access_token $ this-> _ memcache_set ("access_token", $ outoptArr ['Access _ token'], 7000); return $ outoptArr ['Access _ token'];} return $ access_token;} // initialize memcache private function _ memcache_init () {$ mmc = new Memcache; $ ret = $ mmc-> connect (); if ($ ret = false) {return;} return $ mmc;} // Set memcache private function _ memcache_set ($ key, $ value, $ time = 0) {$ mmc = $ this-> _ memcache_init (); $ mmc-> set ($ key, $ value, 0, $ time );} // get memcahce private function _ memcache_get ($ key) {$ mmc = $ this-> _ memcache_init (); return $ mmc-> get ($ key );} // create public function menu_create ($ data) {$ access_token = $ this-> getAccessToken (); // address of the interface for creating a custom menu $ url =" https://api.weixin.qq.com/cgi-bin/menu/create?access_token= {$ Access_token} "; return $ this-> https_request ($ url, $ data);} // delete public function menu_delete () from the custom menu () {$ access_token = $ this-> getAccessToken (); $ url =" https://api.weixin.qq.com/cgi-bin/menu/delete?access_token= {$ Access_token} "; return $ this-> https_request ($ url) ;}}?>