您好,登錄后才能下訂單哦!
小編給大家分享一下php如何實(shí)現(xiàn)驗(yàn)證token、回復(fù)圖文\文本、推送消息的實(shí)用微信類,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
具體代碼如下
<?php class Wechat{ private $data = array(); public function __construct($token){ $this -> auth($token, $wxuser) || exit; if(IS_GET){ echo($_GET['echostr']); exit; }else{ $xml = file_get_contents("php://input"); $xml = new SimpleXMLElement($xml); //file_put_contents('/var/log/test.txt', $xml,FILE_APPEND); $xml || exit; foreach ($xml as $key => $value){ $this -> data[$key] = strval($value); } } } public function request(){ return $this -> data; } public function response($content, $type = 'text', $flag = 0){ $this -> data = array('ToUserName' => $this -> data['FromUserName'], 'FromUserName' => $this -> data['ToUserName'], 'CreateTime' => NOW_TIME, 'MsgType' => $type); $this -> $type($content); $this -> data['FuncFlag'] = $flag; $xml = new SimpleXMLElement('<xml></xml>'); $this -> data2xml($xml, $this -> data); exit($xml -> asXML()); } private function text($content){ $this -> data['Content'] = $content; } private function music($music){ list($music['Title'], $music['Description'], $music['MusicUrl'], $music['HQMusicUrl']) = $music; $this -> data['Music'] = $music; } private function news($news){ $articles = array(); foreach ($news as $key => $value){ list($articles[$key]['Title'], $articles[$key]['Description'], $articles[$key]['PicUrl'], $articles[$key]['Url']) = $value; if($key >= 9){ break; } } $this -> data['ArticleCount'] = count($articles); $this -> data['Articles'] = $articles; } private function transfer_customer_service($content){ $this -> data['Content'] = ''; } private function data2xml($xml, $data, $item = 'item'){ foreach ($data as $key => $value){ is_numeric($key) && $key = $item; if(is_array($value) || is_object($value)){ $child = $xml -> addChild($key); $this -> data2xml($child, $value, $item); }else{ if(is_numeric($value)){ $child = $xml -> addChild($key, $value); }else{ $child = $xml -> addChild($key); $node = dom_import_simplexml($child); $node -> appendChild($node -> ownerDocument -> createCDATASection($value)); } } } } private function auth($token){ $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr, SORT_STRING); $tmpStr = implode($tmpArr); $tmpStr = sha1($tmpStr); if(trim($tmpStr) == trim($signature)){ return true; }else{ return false; } return true; } } ?>
看完了這篇文章,相信你對(duì)“php如何實(shí)現(xiàn)驗(yàn)證token、回復(fù)圖文\文本、推送消息的實(shí)用微信類”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。