Complete php code development and php code development
This article provides examples of php code development for your reference. The specific content is as follows:
<? Php // encapsulate it into an interface class WeixinApi {private $ appid; private $ appsecret; // the value of public function _ construct ($ appid = "", $ appsecret = "") {$ this-> appid = $ appid; $ this-> appsecret = $ appsecret;} // verify the server address validity. public function valid () {if ($ this-> checkSignature () {$ echostr = $ _ GET ['echostr']; // random string return $ echostr ;} else {return "Error" ;}}// check the signature private function checkSignature () {// 1. Receiving Server GET party $ Signature =$ _ GET ['signature']; // encrypt the signature $ timestamp =$ _ GET ['timestamp']; // timestamp $ nonce = $ _ GET ['nonce ']; // random number // 2. Encryption/verification process // 1. sort the token, timestamp, and nonce parameters in Lexicographic Order. // bool sort (array & $ array [, int $ sort_flags = SORT_REGULAR]) sort the array $ tmpArr = array (TOKEN, $ timestamp, $ nonce); // put the above 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 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 "); // returns $ this-> replyNews ($ obj, $ newsArr); break; // cancel the follow event case 'unsubscribe': // The account unbinding operation, and so on. // customize the menu to push the CLICK event case 'click ': $ eventKey = $ obj-> EventKey; // obtain the event KEY value, which corresponds to the KEY value in the custom menu interface. switch ($ eventKey) {case 'old ': $ weixinArr = $ this-> history ("da675ebc6a0d72920dca3f676122a693 "); $ 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 text messages. The public function extends etext ($ obj) {$ co Ntent = trim ($ obj-> Content); // return $ content of the text message;} // receives the 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 = "<xml> <ToUserName> <! [CDATA [% s]> </ToUserName> <FromUserName> <! [CDATA [% s]> </FromUserName> <CreateTime> % s </CreateTime> <MsgType> <! [CDATA [image]> </MsgType> <Image> <MediaId> <! [CDATA [% s]> </MediaId> </Image> </xml> "; return sprintf ($ replyXml, $ obj-> FromUserName, $ obj-> ToUserName, time (), $ mediaId) ;}// the public function replyText ($ obj, $ content) {$ replyXml = "<xml> <ToUserName> <! [CDATA [% s]> </ToUserName> <FromUserName> <! [CDATA [% s]> </FromUserName> <CreateTime> % s </CreateTime> <MsgType> <! [CDATA [text]> </MsgType> <Content> <! [CDATA [% s]> </Content> </xml> "; 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 = "<item> <Title> <! [CDATA [% s]> </Title> <Description> <! [CDATA [% s]> </Description> <PicUrl> <! [CDATA [% s]> </PicUrl> <Url> <! [CDATA [% s]> </Url> </item> "; foreach ($ newsArr as $ item) {$ itemStr. = sprintf ($ itemXml, $ item ['title'], $ item ['description'], $ item ['picurl'], $ item ['url']);} $ replyXml = "<xml> <ToUserName> <! [CDATA [% s]> </ToUserName> <FromUserName> <! [CDATA [% s]> </FromUserName> <CreateTime> % s </CreateTime> <MsgType> <! [CDATA [news]> </MsgType> <ArticleCount> ". count ($ newsArr ). "</ArticleCount> <Articles> ". $ itemStr. "</Articles> </xml>"; return sprintf ($ replyXml, $ obj-> FromUserName, $ obj-> ToUserName, time ());} // encapsulate https request (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); // close the request Returns 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 fun image 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 ['U Rl '], "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 $ newsA Rr;} 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 $ news Arr;}/*** get 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) {// if the call fails, obtain the interface credential to get 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 );} // custom menu create public function menu_create ($ data) {$ access_token = $ this-> getAccessToken (); // custom menu create interface address $ 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) ;}}?>
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.