This example for you to share the micro-letter development of PHP code for your reference, the specific content as follows
<?php//encapsulated into a micro-letter interface class Weixinapi {private $appid;
Private $appsecret;
The constructor initializes the assignment public function __construct ($appid = "", $appsecret = "") {$this->appid = $appid;
$this->appsecret = $appsecret; }//Verify server address validity public function valid () {if ($this->checksignature ()) {$echostr = $_g
et[' echostr '];//a random string return $echostr;
else {return "Error"; Check the signature private function Checksignature () {//One, receive the micro-server get way submitted over 4 parameter data $signature = $_ get[' signature '];//micro-letter Encryption Signature $timestamp = $_get[' timestamp '];//timestamp $nonce = $_get[' nonce '];//random number//II. Encryption/validation process//1.
Sort token, timestamp, nonce three parameters in dictionary order;//bool Sort (array & $array [, int $sort _flags = sort_regular]) array sorting
$TMPARR = Array (TOKEN, $timestamp, $nonce);//Put the above three arguments into an array sort ($TMPARR, sort_string); 2. Concatenation of three parameter strings into a stringSHA1 encryption; $tmpStr = implode ($TMPARR);
Converts an array to a string $signatureStr = SHA1 ($TMPSTR); 3.
The developer gets the encrypted string in contrast to the signature.
if ($signatureStr = = $signature) {return true;
else {return false; }//Response message Public Function responsemsg () {//Receive micro-trust server send POST request to developer server, carry XML packet $postData = $G
lobals[' Http_raw_post_data '];
Processing XML Packets $XMLOBJ = Simplexml_load_string ($postData, "SimpleXMLElement", libxml_nocdata);
if (! $xmlObj) {echo "";
Exit //Gets the parameter content in the Receive message $toUserName = $XMLOBJ->tousername;//Developer Micro-signal $fromUserName = $XMLOBJ->fromusernam e;//the sender's micro-signal (OpenID) $msgType = $XMLOBJ->msgtype;//message type switch ($msgType) {//Receive text message case ' t
Ext '://Get the text message keyword $keyword = $this->receivetext ($XMLOBJ); Keyword reply switch ($keyword) {case "w001": CASE "W001": Return $this->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 ' 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) {//Attention events Case ' subscribe '://Issued welcome message $NEWSARR = Array ("Title" => ;" Make a valuable headline! "," Description "=>" grasp the value of the headline information, daily more talk about it! "," Picurl "=>" Http://jober.applinzi.com/news/img/news.png "," Url "=>" http://j
Ober.applinzi.com/news/index.php "));
Reply to text message return $this->replynews ($obj, $NEWSARR);
Break
Cancellation of Attention event case ' unsubscribe '://Account of the Untied operation and so on break; Custom menu Push Click event Case ' click ': $eventKey = $obj->eventkey;//Get event key value, corresponding to key value in 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 (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;
//Receive text message public function Receivetext ($obj) {$content = Trim ($obj->content);//content of text message
return $content; //Receive Picture message public function Receiveimage ($obj) {$picUrl = $obj link to->picurl;//picture $mediaId = $ob
j->mediaid;//picture message Media ID return $this->replyimage ($obj, $mediaId); //reply to Picture message public function Replyimage ($obj, $mediaId) {$replyXml = "<xml> <tous ername><! [cdata[%s]]>≪/tousername> <fromusername><! [cdata[%s]]></fromusername> <CreateTime>%s</CreateTime> <MSGTYPE>&L t;! [cdata[image]]></msgtype> <Image> <mediaid><!
[cdata[%s]]></mediaid> </Image> </xml> ";
Return sprintf ($replyXml, $obj->fromusername, $obj->tousername,time (), $mediaId); }//Reply text message public function Replytext ($obj, $content) {$replyXml = "<xml> <touse rname><! [cdata[%s]]></tousername> <fromusername><! [cdata[%s]]></fromusername> <CreateTime>%s</CreateTime> <MSGTYPE>&L t;! [cdata[text]]></msgtype> <content><!
[cdata[%s]]></content> </xml> "; Return sprintf ($replyXml, $obj->fromusername, $obj;
Tousername,time (), $content);
//Reply to Text message public function replynews ($obj, $NEWSARR) {//To determine whether an array type if (!is_array ($NEWSARR))
{return;
///To determine if the array is an empty array if (! $newsArr) {return;
} $itemStr = ""; Define 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 transmission options curl_setopt ($ch, Curlopt_url, $url);//Request URL address curl_setopt ($ch, curlopt_returntransfer,1);//The result of the request will be The form of the stream returns if (!empty ($data)) {curl_setopt ($ch, curlopt_post,1);//Request POST method curl_setopt ($ch
, Curlopt_postfields, $data);//post content submitted}//3, execute request and process result $outopt = curl_exec ($ch);
Converts JSON data to an array $OUTOPTARR = Json_decode ($outopt, TRUE);
4, Close 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; The 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 (); }//Aggregation data-Get the latest interesting graph 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 (); Judge whether the joke interface gets the data if ($jokeArr [' error_code '] = = 0) {foreach ($resultArr as $item) {$new Sarr[] = Array ("Title" => $item[' content '], "Description" => $item [' UpdateTime '], "Picurl" => $item [' url '],
"url" => $item [' url ']);
} return $NEWSARR;
}//Aggregation data-Gets the history of today's 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 (); Determines whether the interface obtains data if ($JOKEARR [' error_code '] = = 0) {foreach ($resultArr as $item) {$newsA
Rr[] = Array ("title" => $item [' title '], "Description" => $item [' Date '],
"Picurl" => "", "Url" => ""); } return $NEWSARR;
The 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) {$newsA
Rr[] = Array ("title" => $item [' Day '], "Description" => $item [' title '],
"Picurl" => "", "Url" => "http://www.baidu.com");
} return $NEWSARR; /** * Get basic support inside the interface call voucher Access_token and cache Access_token * @return Access_token String Interface voucher **/public F Unction Getaccesstoken () {//Get Memcache cache access_token $access _token = $this->_memcache_get ("acCess_token "); If the cached Access_token fails if (! $access _token) {//If the failed call obtains the interface credentials to obtain the 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_t
Oken ", $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 Creation Interface address $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token={$access _token}";
return $this->https_request ($url, $data);
///custom 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 the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.