您好,登錄后才能下訂單哦!
這篇文章主要介紹php怎樣實現(xiàn)簡單微信文本通訊,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
微信開發(fā)前,需要設(shè)置token,這個是微信設(shè)置的,可以任意設(shè)置,用來實現(xiàn)微信通訊。這里有一個別人寫的微信類,功能還比較不錯。weixin.class.php代碼如下
<?php class Weixin { public $token = '';//token public $debug = false;//是否debug的狀態(tài)標示,方便我們在調(diào)試的時候記錄一些中間數(shù)據(jù) public $setFlag = false; public $msgtype = 'text'; //('text','image','location') public $msg = array(); public function __construct($token,$debug) { $this->token = $token; $this->debug = $debug; } //獲得用戶發(fā)過來的消息(消息內(nèi)容和消息類型 ) public function getMsg() { $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; if (!empty($postStr)) { $this->msg = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $this->msgtype = strtolower($this->msg['MsgType']); } } //回復(fù)文本消息 public function makeText($text='') { $CreateTime = time(); $FuncFlag = $this->setFlag ? 1 : 0; $textTpl = "<xml> <ToUserName><![CDATA[{$this->msg['FromUserName']}]]></ToUserName> <FromUserName><![CDATA[{$this->msg['ToUserName']}]]></FromUserName> <CreateTime>{$CreateTime}</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>%s</FuncFlag> </xml>"; return sprintf($textTpl,$text,$FuncFlag); } //根據(jù)數(shù)組參數(shù)回復(fù)圖文消息 public function makeNews($newsData=array()) { $CreateTime = time(); $FuncFlag = $this->setFlag ? 1 : 0; $newTplHeader = "<xml> <ToUserName><![CDATA[{$this->msg['FromUserName']}]]></ToUserName> <FromUserName><![CDATA[{$this->msg['ToUserName']}]]></FromUserName> <CreateTime>{$CreateTime}</CreateTime> <MsgType><![CDATA[news]]></MsgType> <Content><![CDATA[%s]]></Content> <ArticleCount>%s</ArticleCount><Articles>"; $newTplItem = "<item> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <PicUrl><![CDATA[%s]]></PicUrl> <Url><![CDATA[%s]]></Url> </item>"; $newTplFoot = "</Articles> <FuncFlag>%s</FuncFlag> </xml>"; $Content = ''; $itemsCount = count($newsData['items']); $itemsCount = $itemsCount < 10 ? $itemsCount : 10;//微信公眾平臺圖文回復(fù)的消息一次最多10條 if ($itemsCount) { foreach ($newsData['items'] as $key => $item) { if ($key<=9) { $Content .= sprintf($newTplItem,$item['title'],$item['description'],$item['picurl'],$item['url']); } } } $header = sprintf($newTplHeader,$newsData['content'],$itemsCount); $footer = sprintf($newTplFoot,$FuncFlag); return $header . $Content . $footer; } public function reply($data) { echo $data; } public function valid() { if ($this->checkSignature()) { if( $_SERVER['REQUEST_METHOD']=='GET' ) { echo $_GET['echostr']; exit; } }else{ exit; } } private function checkSignature() { $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $tmpArr = array($this->token, $timestamp, $nonce); sort($tmpArr); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } } } ?>
接著正式開發(fā),使用百度SVN地址,創(chuàng)建weixinapi.php文件,這個根據(jù)你后臺設(shè)置名稱。
<?php define("TOKEN", ""); define('DEBUG', false); include_once('weixin.class.php'); require_once("db.php"); $weixin = new Weixin(TOKEN,DEBUG);//實例化 $weixin->getMsg(); $type = $weixin->msgtype;//消息類型 $keyword = $weixin->msg['Content'];//獲取的文本 if ($type==='text') { $reply = $weixin->makeText($key); }elseif($type==='event'){//第一次關(guān)注推送事件 $reply = $weixin->makeText("歡迎關(guān)注"); }else{//其他類型 $reply = $weixin->makeText("暫時沒有圖片,聲音,地理位置等功能,后續(xù)開發(fā)會增加,感謝你關(guān)注"); } $weixin->reply($reply);
這樣就實現(xiàn)了一個例子,第一次關(guān)注事件回復(fù),非文本回復(fù),以及文本回復(fù),這里文本回復(fù)是你輸入什么就返回什么。
以上是“php怎樣實現(xiàn)簡單微信文本通訊”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。