日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

微信公众平台消息接口开发(2)-封装weixin.class.php

發(fā)布時間:2023/12/9 php 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 微信公众平台消息接口开发(2)-封装weixin.class.php 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

微信公眾平臺消息接口開發(fā)(2)-封裝weixin.class.php

一、封裝weixin.class.php

由于微信公眾平臺的通信使用的是特定格式的XML數(shù)據(jù),每次接受和回復都要去做一大堆的數(shù)據(jù)處理。

我們就考慮在這個基礎上做一次封裝,weixin.class.php,代碼如下:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 <?php class Weixin { ??? public $token = '';//token ??? public $debug =? false;//是否debug的狀態(tài)標示,方便我們在調試的時候記錄一些中間數(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; ??? }<br>     //獲得用戶發(fā)過來的消息(消息內容和消息類型? ) ??? public function getMsg() ??? { ??????? $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']); ??????? } ??? }<br>     //回復文本消息 ??? 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); ??? }<br>     //根據(jù)數(shù)組參數(shù)回復圖文消息 ??? 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;//微信公眾平臺圖文回復的消息一次最多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) ??? { ??????? if ($this->debug) { ??????????????????? $this->write_log($data); ??????? } ??????? echo $data; ??? } ??? public function valid() ??? { ??????? if ($this->checkSignature()) { ??????????? if( $_SERVER['REQUEST_METHOD']=='GET' ) ??????????? { ??????????????? echo $_GET['echostr']; ??????????????? exit; ??????????? } ??????? }else{ ??????????? write_log('認證失敗'); ??????????? 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; ??????? } ??? }<br>    private function write_log($log){<br>       //這里是你記錄調試信息的地方? 請自行完善?? 以便中間調試<br>    } } ?>

二、調用weixin.class.php

把你的微信公眾平臺主接口文件(如前面定義的http://www.yourdomain.com/weixin.php)中,修改代碼為:

?

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 include_once('weixin.class.php');//引用剛定義的微信消息處理類<br>define("TOKEN", "mmhelper");<br>define('DEBUG', true); $weixin = new Weixin(TOKEN,DEBUG);//實例化 $weixin->getMsg(); $type = $weixin->msgtype;//消息類型 $username = $weixin->msg['FromUserName'];//哪個用戶給你發(fā)的消息,這個$username是微信加密之后的,但是每個用戶都是一一對應的 if ($type==='text') { ??? if ($weixin->msg['Content']=='Hello2BizUser') {//微信用戶第一次關注你的賬號的時候,你的公眾賬號就會受到一條內容為'Hello2BizUser'的消息 ??????? $reply = $weixin->makeText('歡迎你關注媽媽助手哦,屌絲'); ??? }else{//這里就是用戶輸入了文本信息 ??????? $keyword = $weixin->msg['Content'];?? //用戶的文本消息內容 ??????????????? include_once("chaxun.php");//文本消息 調用查詢程序? ??????????????? $chaxun= new chaxun(DEBUG,$keyword,$username); ??????????????? $results['items'] =$chaxun->search();//查詢的代碼 ??????????? ??????????????? $reply = $weixin->makeNews($results); ??? } }elseif ($type==='location') { ????? //用戶發(fā)送的是位置信息? 稍后的文章中會處理????????????????? }elseif ($type==='image') { ????? //用戶發(fā)送的是圖片 稍后的文章中會處理 }elseif ($type==='voice') {???? ????? //用戶發(fā)送的是聲音 稍后的文章中會處理 } $weixin->reply($reply);

?

三、查詢代碼

還需要將數(shù)據(jù)庫里面的查詢結果格式化為特定的形式

?

?
1 2 3 4 5 6 7 8 9 10 11 public function search(){ ?????? $record=array();? //定義返回結果的數(shù)組 ?????? $list = $this->search($this->keyword);//普通的根據(jù)關鍵詞查詢數(shù)據(jù)庫的操作? 代碼就不用分享了     if(is_array($list)&&!empty($list)){???????????????? ?????????????? foreach($list as $msg){                 $record[]=array(//以下代碼,將數(shù)據(jù)庫中查詢返回的數(shù)組格式化為微信返回消息能接收的數(shù)組形式,即title、description、picurl、url 詳見微信官方的文檔描述 ??????????????????? 'title' =>$msg['title'], ??????????????????? 'description' =>$msg['discription'], ??????????????????? 'picurl' => $msg['pic_url'], ??????????????????? 'url' =>$msg['url'] ??????????????? );<br>        }<br>    }<br>    return $record;<br>}<br>

?

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------

承接微信公眾平臺消息接口開發(fā)、PHP、.NET、javascript、html5程序開發(fā);新浪微博應用、騰訊微博應用等各大開放平臺應用開發(fā)業(yè)務

聯(lián)系QQ:1433273389

關注媽媽助手(賬號mmhelper)方法:

1. 依次進入以下路徑:朋友們—>添加朋友—>搜號碼,輸入mmhelper,不區(qū)分大小寫,點擊查找,然后點擊關注。

2. 掃描二維碼:

轉載于:https://www.cnblogs.com/fx2008/archive/2013/03/22/2974853.html

總結

以上是生活随笔為你收集整理的微信公众平台消息接口开发(2)-封装weixin.class.php的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。