This example for everyone to share the development of PHP code for your reference, the specific content as follows
AppID = $appid; $this->appsecret = $appsecret; }//Verify server address validity public function valid () {if ($this->checksignature ()) {$echostr = $_get[' Ech Ostr '];//random string return $echostr; } else {return "Error"; }}//Check the signature private function Checksignature () {///One, receive server GET method submitted 4 parameter Data $signature = $_get[' Signa Ture '];//cryptographic signature $timestamp = $_get[' timestamp '];//timestamp $nonce = $_get[' nonce '];//random number//two, encryption/verification process//1. Three parameters of token, timestamp and nonce are sorted in dictionary order;//bool Sort (array & $array [, int $sort _flags = sort_regular]) Sort by array $TMPARR = Array (TOKEN, $timestamp, $nonce);//Put the above three parameters into an array of sort ($TMPARR, sort_string); 2. Concatenation of three parameter strings into a string for SHA1 encryption; $tmpStr = implode ($TMPARR); Converts an array into a string $signatureStr = SHA1 ($TMPSTR); 3. The developer obtains the encrypted string in contrast to the signature. if ($signatureStr = = $signature) {return true; } else {return FALse }}//Response message Public Function responsemsg () {///Receive Server send POST request to developer server, carry XML packet $postData = $GLOBALS [' H Ttp_raw_post_data ']; Processing XML Packets $XMLOBJ = Simplexml_load_string ($postData, "SimpleXMLElement", libxml_nocdata); if (! $xmlObj) {echo ""; Exit }//Gets the contents of the parameter in the receive message $toUserName = $XMLOBJ->tousername;//developer Number $fromUserName = $XMLOBJ->fromusername;//Sent Party number (OpenID) $msgType = $XMLOBJ->msgtype;//message type switch ($msgType) {//Receive text message case ' text ': Gets the keyword of the text message $keyword = $this->receivetext ($XMLOBJ); Make a keyword reply to switch ($keyword) {case "w001": Case "W001": Return $this-& Gt;replytext ($XMLOBJ, "hi~ hello"); Break Case "w002": Case "W002": Return $this->replytext ($XMLOBJ, "hi~ embarrassed"); 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 picture message case ' image ': Return $this->receiveimage ($XMLOBJ); Break Receive event push case ' event ': Return $this->receiveevent ($XMLOBJ); Break }}//Receive event push Public function receiveevent ($obj) {//Receive event Type $event = $obj->event; Switch ($event) {//Event case ' subscribe '://message Welcome $NEWSARR = Array ( Array ("Title" and "=" to make valuable headlines!) "," Description "=" grasp the value of the headline information, daily more talk about it! "," picurl "=" Http://jober.applinzi.com/news/img/news.png "," Url "=" Http://job " Er.applInzi.com/news/index.php ")); Reply text message return $this->replynews ($obj, $NEWSARR); Break Cancel Attention Event case ' unsubscribe '://account's Unbind operation and so on; Custom menu Push Click event Case ' click ': $eventKey = $obj->eventkey;//Gets the event key value corresponding to the key value in the custom menu interface, switch ($ Eventkey) {case ' old ': $weixinArr = $this->history ("da675ebc6a0d72920dca3f676122a 693 "); $WEIXINARR = Array_slice ($WEIXINARR, 0,5); $NEWSARR = Array (); foreach ($weixinArr as $item) {$NEWSARR = array (Array ("Title" => ; $item [' Description '], "Description" +-$item [' Title '], "picurl" and "=" "Http://1.jober.applinzi.com/news/img/2.jpg", "Url" = "http://www.todayonhistory.com/" )); } return $this->replynews ($obj, $NEWSARR); Break } break; }}//Receive text message public function Receivetext ($obj) {$content = Trim ($obj->content);//The contents of the text message return $content; }//Receive Picture message public function Receiveimage ($obj) {$picUrl = link to $obj->picurl;//picture $mediaId = $obj mediaid;//picture message Media ID return $this->replyimage ($obj, $mediaId); }//Reply picture message public function Replyimage ($obj, $mediaId) {$replyXml = "%s"; Return sprintf ($replyXml, $obj->fromusername, $obj->tousername,time (), $mediaId); }//Reply text message public function Replytext ($obj, $content) {$replyXml = "%s"; Return sprintf ($replyXml, $obj->fromusername, $obj->tousername,time (), $content); }//Reply text message public function replynews ($obj, $NEWSARR) {//Determine if the array type if (!is_array ($NEWSARR)) { Return }//Judgment array isNo is an empty array if (! $newsArr) {return; } $itemStr = ""; Define the item Template $itemXml = ""; foreach ($newsArr as $item) {$itemStr. = sprintf ($itemXml, $item [' Title '], $item [' Description '], $item [' Picurl '], $item [' URL ']); } $replyXml = "%s". Count ($NEWSARR). "". $itemStr. ""; 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);//URL address of the request curl_setopt ($ch, curlopt_returntransfer,1);//The result of the request as a file stream The form returns if (!empty ($data)) {curl_setopt ($ch, curlopt_post,1);//Request POST mode curl_setopt ($ch, Curlop T_postfields, $data);//post submissions}//3, executing the request and processing the result $outopt = Curl_exec ($ch); Convert JSON data to an array $OUTOPTARR = Json_decode ($outopt, TRUE); 4. Close Curl Curl_close ($ch); If the returned result $outoptIs 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 (); }}//Aggregated data-get latest fun public function Joke_text ($key, $pagesize =10) {$url = "http://japi.juhe.cn/joke/img/te xt.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 if the joke interface gets the data if ($jokeArr [' error_code '] = = 0) {foreach ($resultArr as $item) {$NEWSARR [ ] = Array ("TitlE "= $item [' content ']," Description "and" UpdateTime "and" Picurl "and" $item [' url '], "url" = = $item [' url ']); }} return $NEWSARR; }//Aggregated data-gets the history of today's public function ($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 (); Determines 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 (); Determines whether the interface obtains data if ($JOKEARR [' error_code '] = = 0) {foreach ($resultArr as $item) {$NEWSARR [] = Array ("title" = = $item [' Day '], "Description" and "= $item [' title ']," Picurl " = "", "Url" = "http://www.baidu.com"); }} return $NEWSARR; /** * Get basic support inside the interface call credential Access_token and Cache Access_token * @return Access_token String interface credentials **/public functio n Getaccesstoken () {//Get Memcache cache access_token $access _token = $this->_memcache_get ("Access_token"); If the cached Access_token fails if (! $access _token{//If the invalidation call gets the interface credentials to get access_token $url = "Https://api.weixin.qq.com/cgi-bin/token?grant_type=clie Nt_credential &appid={$this->appid}&secret={$this->appsecret} "; $OUTOPTARR = $this->https_request ($url); if (!isset ($outoptArr [' Errcode '])) {//memcache cache access_token $this->_memcache_set ("Access_toke n ", $outoptArr [' Access_token '],7000); return $OUTOPTARR [' Access_token ']; }} return $access _token; }//Initialize memcache private Function _memcache_init () {$mmc = new Memcache; $ret = Connect () $mmc. 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 Creation Interface address $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token={$access _token}"; return $this->https_request ($url, $data); }//Customize menu Delete public Function Menu_delete () {$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 a relatively complete development of PHP code _php instance of the content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!