PHP對接微信公眾平臺消息接口開發流程教程剖析_第1頁
PHP對接微信公眾平臺消息接口開發流程教程剖析_第2頁
PHP對接微信公眾平臺消息接口開發流程教程剖析_第3頁
PHP對接微信公眾平臺消息接口開發流程教程剖析_第4頁
PHP對接微信公眾平臺消息接口開發流程教程剖析_第5頁
已閱讀5頁,還剩2頁未讀, 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

PHP對接微信公眾平臺消息接口開發流程教程這篇文章主要介紹了PHP對接微信公眾平臺消息接口開發流程,如何使用PHP版接口操作公眾平臺消息,需要的朋友可以參考下一、寫好接口程序在你的服務器上上傳好一個接口程序文件,

/weixin.php

內容如下:復制代碼 代碼如下:<?phpdefine("TOKEN","weixin");//自己定義的token就是個通信的私鑰$wechatObj=newwechatCallbackapiTest();$wechatObj->valid();//$wechatObj->responseMsg();classwechatCallbackapiTest{publicfunctionvalid(){$echoStr=$_GET["echostr"];if($this->checkSignature()){echo$echoStr;exit;}}publicfunctionresponseMsg(){$postStr=$GLOBALS["HTTP_RAW_POST_DATA"];if(!empty($postStr)){$postObj = simplexml_load_string($postStr, 'SimpleXMLElement',LIBXML_NOCDATA);$fromUsername=$postObj->FromUserName;$toUsername=$postObj->ToUserName;$keyword=trim($postObj->Content);$time=time();$textTpl="<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[%s]]></MsgType><Content><![CDATA[%s]]></Content><FuncFlag>0<FuncFlag></xml>";if(!empty($keyword)){$msgType="text";$contentStr='你好啊,屌絲 ';$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time,$msgType,$contentStr);echo$resultStr;}else{echo'咋不說哈呢';}}else{echo'咋不說哈呢';exit;}}privatefunctioncheckSignature(){$signature=$_GET["signature"];$timestamp=$_GET["timestamp"];$nonce=$_GET["nonce"];$token=TOKEN;$tmpArr=array($token,$timestamp,$nonce);sort($tmpArr);$tmpStr=implode($tmpArr);$tmpStr=sha1($tmpStr);if($tmpStr==$signature){returntrue;}else{returnfalse;}}}?>二、配置微信公眾平臺回復接口設置回復接口,填好URL和

Token(url填上面的

/weixin.php

,token必須跟上面程序里面定義的

Token一致)三、驗證接口用自己的個人微信關注下你的公眾賬號,給這個賬號發一條消息過去,收到原樣的消息返回,即驗證成功了。四、開始自定義回復注釋掉$wechatObj->valid(); 這行,同時去掉 //$wechatObj->responseMsg();這行的注釋。你可以修改 responseMsg函數里面的代碼, 根據用戶的消息類型 ('text','image','location'消息內容來回復用戶不同的內容。消息接口就可以使用了,發個消息試試看吧?

)和1.封裝

weixin.class.php由于微信公眾平臺的通信使用的是特定格式的 XML數據,每次接受和回復都要去做一大堆的數據處理。我們就考慮在這個基礎上做一次封裝, weixin.class.php,代碼如下:復制代碼 代碼如下:<?phpclassWeixin{public$token='';//tokenpublic$debug= false;//是否debug的狀態標示,方便我們在調試的時候記錄一些中間數據public$setFlag=false;public$msgtype='text'; //('text','image','location')public$msg=array();publicfunction__construct($token,$debug){$this->token=$token;$this->debug=$debug;}//獲得用戶發過來的消息(消息內容和消息類型 )publicfunctiongetMsg(){$postStr=$GLOBALS["HTTP_RAW_POST_DATA"];if($this->debug){$this->write_log($postStr);}if(!empty($postStr)){$this->msg = (array)simplexml_load_string($postStr, 'SimpleXMLElement',LIBXML_NOCDATA);$this->msgtype=strtolower($this->msg['MsgType']);}}回復文本消息publicfunctionmakeText($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><![CDATA1]></MsgType><Content><![CDATA[%s]]></Content><FuncFlag>%s</FuncFlag></xml>";returnsprintf($textTpl,$text,$FuncFlag);}根據數組參數回復圖文消息publicfunctionmakeNews($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;//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;}publicfunctionreply($data){if($this->debug){$this->write_log($data);}echo$data;}publicfunctionvalid(){if($this->checkSignature()){if($_SERVER['REQUEST_METHOD']=='GET'){echo$_GET['echostr'];exit;}}else{write_log('認證失敗');exit;}}privatefunctioncheckSignature(){$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){returntrue;}else{returnfalse;}}privatefunctionwrite_log($log){//這里是你記錄調試信息的地方

請自行完善

以便中間調試}}?>2.調用weixin.class.php把你的微信公眾平臺主接口文件(如前面定義的 )中,修改代碼為:復制代碼 代碼如下:<?phpinclude_once('weixin.class.php');// 引用剛定義的微信消息處理類define("TOKEN","mmhelper");define('DEBUG',true);$weixin=newWeixin(TOKEN,DEBUG);//實例化$weixin->getMsg();$type=$weixin->msgtype;//消息類型$username=$weixin->msg['FromUserName'];// 哪個用戶給你發的消息

,這個$username是微信加密之后的,但是每個用戶都是一一對應的if($type==='text'){if($weixin->msg['Content']=='Hello2BizUser'){//微信用戶第一次關注你的賬號的時候,你的公眾賬號就會受到一條內容為'Hello2BizUser'的消息$reply=$weixin->makeText('歡迎你關注哦,屌絲

');}else{//

這里就是用戶輸入了文本信息$keyword=$weixin->msg['Content'];include_once("chaxun.php");//

//用戶的文本消息內容文本消息 調用查詢程序$chaxun=newchaxun(DEBUG,$keyword,$username);$results['items']=$chaxun->search();//查詢的代碼$reply=$weixin->makeNews($results);}}elseif($type==='location'){//用戶發送的是位置信息 稍后的文章中會處理}elseif($type==='image'){//用戶發送的是圖片 稍后的文章中會處理}elseif($type==='voice'){//用戶發送的是聲音 稍后的文章中會處理}$weixin->reply($reply);?>3.查詢代碼還需要將數據庫里面的查詢結果格式化為特定的形式復制代碼 代碼如下:<?phppublicfunctionsearch(){$record=array(); //定義返回結果的數組$list=$this->search($this->keyword);// 普通的根據關鍵詞查詢數據庫的操

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論