第二章 啟用開發模式
微信公眾平台開發模式
高級功能
微信公眾平台地址:https://mp.weixin.qq.com
登錄微信公眾平台后台,選擇高級功能,進入后就看到兩種模式
我們需要先關閉編輯模式。點擊編輯模式的進入
滑動關閉
開發模式
進入開發模式里面
點擊成為開發者
彈出URL和Token填寫框
此處的URL為上篇中介紹的雲應用的域名,而Token在index.php中定義為weixin。提交后提示你已成為開發者。
再滑動右上角啟用按鈕。
恭喜,你成功啟用開發模式。
自動回復
在上面的例子中,實現了一個發送“?”就能回復當前時間的功能。
效果如下:
至此,你的微信公眾平台賬號已經實現自動回復了。
第三章 消息類型講解
接收消息類型
目前普通用戶能向公眾賬號推送五種格式的消息:文本(包括表情)、語音、圖片、視頻、位置、鏈接。
下面就這五種分別詳解如下:
1. 文本(包括表情)
發送文本及表情
文字后台格式:
<xml> <ToUserName><![CDATA[gh_680bdefc8c5d]]></ToUserName> <FromUserName><![CDATA[oIDrpjqASyTPnxRmpS9O_ruZGsfk]]></FromUserName> <CreateTime>1359028446</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[測試文字]]></Content> <MsgId>5836982729904121631</MsgId> </xml>
表情后台格式
<xml><ToUserName><![CDATA[gh_680bdefc8c5d]]></ToUserName> <FromUserName><![CDATA[oIDrpjqASyTPnxRmpS9O_ruZGsfk]]></FromUserName> <CreateTime>1359044526</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[/::)/::~/::B/::|/:8-)]]></Content> <MsgId>5837051792978241864</MsgId> </xml>
XML格式講解
ToUserName 消息接收方微信號,一般為公眾平台賬號微信號
FromUserName 消息發送方微信號
CreateTime 消息創建時間
MsgType 消息類型;文本消息為text
Content 消息內容
MsgId 消息ID號
可以看出,文本和表情的消息類型均為文本
2. 圖片
發送圖片
后台格式:
<xml><ToUserName><![CDATA[gh_680bdefc8c5d]]></ToUserName> <FromUserName><![CDATA[oIDrpjqASyTPnxRmpS9O_ruZGsfk]]></FromUserName> <CreateTime>1359028479</CreateTime> <MsgType><![CDATA[image]]></MsgType> <PicUrl><![CDATA[http://mmbiz.qpic.cn/mmbiz/L4qjYtOibummHn90t1mnaibYiaR8ljyicF3MW7XX3BLp1qZgUb7CtZ0DxqYFI4uAQH1FWs3hUicpibjF0pOqLEQyDMlg/0]]></PicUrl> <MsgId>5836982871638042400</MsgId> <MediaId><![CDATA[PGKsO3LAgbVTsFYO7FGu51KUYa07D0C_Nozz2fn1z6VYtHOsF59PTFl0vagGxkVH]]></MediaId> </xml>
XML格式講解
ToUserName 消息接收方微信號,一般為公眾平台賬號微信號
FromUserName 消息發送方微信號
CreateTime 消息創建時間
MsgType 消息類型;圖片消息為image
PicUrl 圖片鏈接地址,可以用HTTP GET獲取
MsgId 消息ID號
3. 語音
發送語音
后台格式:
<xml><ToUserName><![CDATA[gh_680bdefc8c5d]]></ToUserName> <FromUserName><![CDATA[oIDrpjqASyTPnxRmpS9O_ruZGsfk]]></FromUserName> <CreateTime>1359028479</CreateTime> <MsgType><![CDATA[image]]></MsgType> <PicUrl><![CDATA[http://mmbiz.qpic.cn/mmbiz/L4qjYtOibummHn90t1mnaibYiaR8ljyicF3MW7XX3BLp1qZgUb7CtZ0DxqYFI4uAQH1FWs3hUicpibjF0pOqLEQyDMlg/0]]></PicUrl> <MsgId>5836982871638042400</MsgId> <MediaId><![CDATA[PGKsO3LAgbVTsFYO7FGu51KUYa07D0C_Nozz2fn1z6VYtHOsF59PTFl0vagGxkVH]]></MediaId> </xml>
XML格式講解
ToUserName 消息接收方微信號,一般為公眾平台賬號微信號
FromUserName 消息發送方微信號
CreateTime 消息創建時間
MsgType 消息類型;語音消息為voice
MediaId 媒體ID
Format 語音格式,這里為amr
MsgId 消息ID號
附:AMR接口簡介
全稱Adaptive Multi-Rate,主要用於移動設備的音頻,壓縮比比較大,但相對其他的壓縮格式質量比較差,由於多用於人聲,通話,效果還是很不錯的。
4. 視頻
發送視頻
后台格式:
xml><ToUserName><![CDATA[gh_680bdefc8c5d]]></ToUserName> <FromUserName><![CDATA[oIDrpjqASyTPnxRmpS9O_ruZGsfk]]></FromUserName> <CreateTime>1359028186</CreateTime> <MsgType><![CDATA[video]]></MsgType> <MediaId><![CDATA[DBVFRIj29LB2hxuYpc0R6VLyxwgyCHZPbRj_IIs6YaGhutyXUKtFSDcSCPeoqUYr]]></MediaId> <ThumbMediaId><![CDATA[mxUJ5gcCeesJwx2T9qsk62YzIclCP_HnRdfTQcojlPeT2G9Q3d22UkSLyBFLZ01J]]></ThumbMediaId> <MsgId>5836981613212624665</MsgId> </xml>
XML格式講解
ToUserName 消息接收方微信號,一般為公眾平台賬號微信號
FromUserName 消息發送方微信號
CreateTime 消息創建時間
MsgType 消息類型;視頻消息為video
MediaId 媒體ID
ThumbMediaId 媒體縮略ID?
MsgId 消息ID號
5. 位置
發送位置
后台格式:
<xml> <ToUserName><![CDATA[gh_680bdefc8c5d]]></ToUserName> <FromUserName><![CDATA[oIDrpjqASyTPnxRmpS9O_ruZGsfk]]></FLACFromUserName> <CreateTime>1359036619</CreateTime> <MsgType><![CDATA[location]]></MsgType> <Location_X>22.539968</Location_X> <Location_Y>113.954980</Location_Y> <Scale>16</Scale> <Label><![CDATA[中國廣東省深圳市南山區華僑城深南大道9789號 郵政編碼: 518057]]></Label> <MsgId>5837017832671832047</MsgId> </xml>
XML格式講解
ToUserName 消息接收方微信號,一般為公眾平台賬號微信號
FromUserName 消息發送方微信號
CreateTime 消息創建時間
MsgType 消息類型,地理位置為location
Location_X 地理位置緯度
Location_Y 地理位置經度
Scale 地圖縮放大小
Label 地理位置信息
MsgId 消息ID號
6. 鏈接
發送鏈接
后台格式:
<xml> <ToUserName><![CDATA[gh_680bdefc8c5d]]></ToUserName> <FromUserName><![CDATA[oIDrpjl2LYdfTAM-oxDgB4XZcnc8]]></FromUserName> <CreateTime>1359709372</CreateTime> <MsgType><![CDATA[link]]></MsgType> <Title><![CDATA[微信公眾平台開發者的江湖]]></Title> <Description><![CDATA[陳坤的微信公眾號這段時間大火,大家..]]></Description> <Url><![CDATA[http://israel.duapp.com/web/photo.php]]></Url> <MsgId>5839907284805129867</MsgId> </xml>
XML格式講解
ToUserName 消息接收方微信號,一般為公眾平台賬號微信號
FromUserName 消息發送方微信號
CreateTime 消息創建時間
MsgType 消息類型,鏈接為link
Title 圖文消息標題
Description 圖文消息描述
Url 點擊圖文消息跳轉鏈接
MsgId 消息ID號
發送消息類型
目前普通公眾賬號能向用戶推送三種格式的消息:文本、圖文、音樂。其中圖文消息包括單條圖文消息和多條圖文消息,展示方式有一點點不同。
下面就這幾種分別詳解如下:
1. 文本消息格式
回復文本
后台格式:
<xml> <ToUserName><![CDATA[oIDrpjqASyTPnxRmpS9O_ruZGsfk]]></ToUserName> <FromUserName><![CDATA[gh_680bdefc8c5d]]></FromUserName> <CreateTime>1359036631</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[【深圳】天氣實況 溫度:27℃ 濕度:59% 風速:東北風3級 11月03日 周日 27℃~23℃ 小雨 東北風4-5級 11月04日 周一 26℃~21℃ 陣雨 微風 11月05日 周二 27℃~22℃ 陰 微風]]></Content> <FuncFlag>0</FuncFlag> </xml>
XML格式講解
FromUserName 消息發送方
ToUserName 消息接收方
CreateTime 消息創建時間
MsgType 消息類型,文本消息必須填寫text
Content 消息內容,大小限制在2048字節,字段為空為不合法請求
FuncFlag 星標字段
2. 圖文消息格式
2.1 單條圖文消息
回復單條圖文
后台格式:
<xml> <ToUserName><![CDATA[oIDrpjqASyTPnxRmpS9O_ruZGsfk]]></ToUserName> <FromUserName><![CDATA[gh_680bdefc8c5d]]></FromUserName> <CreateTime>1359011899</CreateTime> <MsgType><![CDATA[news]]></MsgType> <Content><![CDATA[]]></Content> <ArticleCount>1</ArticleCount> <Articles> <item> <Title><![CDATA[[蘋果產品信息查詢]]></Title> <Description><![CDATA[序列號:USE IMEI NUMBER IMEI號:358031058974471 設備名稱:iPhone 5C 設備顏色: 設備容量: 激活狀態:已激活 電話支持:未過期[2014-01-13] 硬件保修:未過期[2014-10-14] 生產工廠:中國]]> </Description> <PicUrl><![CDATA[http://www.doucube.com/weixin/weather/icon/banner.jpg]]></PicUrl> <Url><![CDATA[]]></Url> </item> </Articles> <FuncFlag>0</FuncFlag> </xml>
2.2 多圖文消息
回復多圖文
后台數據格式
<xml> <ToUserName><![CDATA[oIDrpjqASyTPnxRmpS9O_ruZGsfk]]></ToUserName> <FromUserName><![CDATA[gh_680bdefc8c5d]]></FromUserName> <CreateTime>1359011829</CreateTime> <MsgType><![CDATA[news]]></MsgType> <Content><![CDATA[]]></Content> <ArticleCount>5</ArticleCount> <Articles> <item> <Title><![CDATA[【深圳】天氣實況 溫度:3℃ 濕度:43﹪ 風速:西南風2級]]></Title> <Description><![CDATA[]]></Description> <PicUrl><![CDATA[http://www.doucube.com/weixin/weather/icon/banner.jpg]]></PicUrl> <Url><![CDATA[]]></Url> </item> <item> <Title><![CDATA[06月24日 周四 2℃~-7℃ 晴 北風3-4級轉東南風小於3級]]></Title> <Description><![CDATA[]]></Description> <PicUrl><![CDATA[http://www.doucube.com/weixin/weather/icon/d00.gif]]></PicUrl> <Url><![CDATA[]]></Url> </item> <item> <Title><![CDATA[06月25日 周五 -1℃~-8℃ 晴 東南風小於3級轉東北風3-4級]]></Title> <Description><![CDATA[]]></Description> <PicUrl><![CDATA[http://www.doucube.com/weixin/weather/icon/d00.gif]]></PicUrl> <Url><![CDATA[]]></Url> </item> <item> <Title><![CDATA[06月26日 周六 -1℃~-7℃ 多雲 東北風3-4級轉東南風小於3級]]></Title> <Description><![CDATA[]]></Description> <PicUrl><![CDATA[http://www.doucube.com/weixin/weather/icon/d01.gif]]></PicUrl> <Url><![CDATA[]]></Url> </item> <item> <Title><![CDATA[06月27日 周日 0℃~-6℃ 多雲 東南風小於3級轉東北風3-4級]]></Title> <Description><![CDATA[]]></Description> <PicUrl><![CDATA[http://www.doucube.com/weixin/weather/icon/d01.gif]]></PicUrl> <Url><![CDATA[]]></Url> </item> </Articles> <FuncFlag>0</FuncFlag> </xml>
XML格式講解
FromUserName 消息發送方
ToUserName 消息接收方
CreateTime 消息創建時間
MsgType 消息類型,圖文消息必須填寫news
Content 消息內容,圖文消息可填空
ArticleCount 圖文消息個數,限制為10條以內
Articles 多條圖文消息信息,默認第一個item為大圖
Title 圖文消息標題
Description 圖文消息描述
PicUrl 圖片鏈接,支持JPG、PNG格式,較好的效果為大圖640*320,小圖80*80
Url 點擊圖文消息跳轉鏈接
FuncFlag 星標字段
3. 音樂消息
回復音樂消息
后台格式:
<xml> <ToUserName><![CDATA[ollB4jqgdO_cRnVXk_wRnSywgtQ8]]></ToUserName> <FromUserName><![CDATA[gh_b629c48b653e]]></FromUserName> <CreateTime>1372310544</CreateTime> <MsgType><![CDATA[music]]></MsgType> <Music> <Title><![CDATA[最炫民族風]]></Title> <Description><![CDATA[鳳凰傳奇]]></Description> <MusicUrl><![CDATA[http://zj189.cn/zj/download/music/zxmzf.mp3]]></MusicUrl> <HQMusicUrl><![CDATA[http://zj189.cn/zj/download/music/zxmzf.mp3]]></HQMusicUrl> </Music> <FuncFlag>0</FuncFlag> </xml>
XML格式講解
ToUserName 接收方帳號(收到的OpenID)
FromUserName 開發者微信號
CreateTime 消息創建時間
MsgType 消息類型,此處為music
Title 音樂標題
Description 音樂描述
MusicUrl 音樂鏈接
HQMusicUrl 高質量音樂鏈接,WIFI環境優先使用該鏈接播放音樂
FuncFlag 位0x0001被標志時,星標剛收到的消息。
事件消息類型
目前用戶在關注和取消關注,以及點擊菜單的時候會自動向公眾平台發送事件推送消息:
1. 關注事件
<xml> <ToUserName><![CDATA[gh_b629c48b653e]]></ToUserName> <FromUserName><![CDATA[ollB4jv7LA3tydjviJp5V9qTU_kA]]></FromUserName> <CreateTime>1372307736</CreateTime> <MsgType><![CDATA[event]]></MsgType> <Event><![CDATA[subscribe]]></Event> <EventKey><![CDATA[]]></EventKey> </xml>
2. 取消關注事件
<xml> <ToUserName><![CDATA[gh_b629c48b653e]]></ToUserName> <FromUserName><![CDATA[ollB4jqgdO_cRnVXk_wRnSywgtQ8]]></FromUserName> <CreateTime>1372309890</CreateTime> <MsgType><![CDATA[event]]></MsgType> <Event><![CDATA[unsubscribe]]></Event> <EventKey><![CDATA[]]></EventKey> </xml>
3. 菜單點擊事件
<xml> <ToUserName><![CDATA[gh_680bdefc8c5d]]></ToUserName> <FromUserName><![CDATA[oIDrpjqASyTPnxRmpS9O_ruZGsfk]]></FromUserName> <CreateTime>1377886191</CreateTime> <MsgType><![CDATA[event]]></MsgType> <Event><![CDATA[CLICK]]></Event> <EventKey><![CDATA[天氣深圳]]></EventKey> </xml>
XML格式講解
ToUserName 接收方微信號
FromUserName 發送方微信號,若為普通用戶,則是一個OpenID
CreateTime 消息創建時間
MsgType 消息類型,event
Event 事件類型,subscribe(訂閱)、unsubscribe(取消訂閱)、CLICK(自定義菜單點擊事件)
EventKey 事件KEY值,與自定義菜單接口中KEY值對應
第四章 接收用戶消息
定義消息類型處理函數
在接收到的消息中根據消息類型,定義不同的消息處理函數,
public function responseMsg() { $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; if (!empty($postStr)){ $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $RX_TYPE = trim($postObj->MsgType); switch ($RX_TYPE) { case "text": $resultStr = $this->receiveText($postObj); break; case "image": $resultStr = $this->receiveImage($postObj); break; case "location": $resultStr = $this->receiveLocation($postObj); break; case "voice": $resultStr = $this->receiveVoice($postObj); break; case "video": $resultStr = $this->receiveVideo($postObj); break; case "link": $resultStr = $this->receiveLink($postObj); break; case "event": $resultStr = $this->receiveEvent($postObj); break; default: $resultStr = "unknow msg type: ".$RX_TYPE; break; } echo $resultStr; }else { echo ""; exit; } }
原文:http://www.cnblogs.com/txw1958/p/wechat-tutorial.html
定義並實現回復函數
在這里,我們先全部用文本消息回復實現
private function transmitText($object, $content, $flag = 0) { $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>%d</FuncFlag> </xml>"; $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $flag); return $resultStr; }
原文:http://www.cnblogs.com/txw1958/p/wechat-tutorial.html
各種消息類型函數實現
1、事件消息
private function receiveEvent($object) { $contentStr = ""; switch ($object->Event) { case "subscribe": $contentStr = "歡迎關注方倍工作室"; break; case "unsubscribe": $contentStr = ""; break; case "CLICK": switch ($object->EventKey) { default: $contentStr = "你點擊了菜單: ".$object->EventKey; break; } break; default: $contentStr = "receive a new event: ".$object->Event; break; } $resultStr = $this->transmitText($object, $contentStr); return $resultStr; }
效果展示:
2、文本消息
private function receiveText($object) { $funcFlag = 0; $contentStr = "你發送的是文本,內容為:".$object->Content; $resultStr = $this->transmitText($object, $contentStr, $funcFlag); return $resultStr; }
效果展示:
3、圖片消息
private function receiveImage($object) { $funcFlag = 0; $contentStr = "你發送的是圖片,地址為:".$object->PicUrl; $resultStr = $this->transmitText($object, $contentStr, $funcFlag); return $resultStr; }
效果展示:
4、語音消息
private function receiveVoice($object) { $funcFlag = 0; $contentStr = "你發送的是語音,媒體ID為:".$object->MediaId; $resultStr = $this->transmitText($object, $contentStr, $funcFlag); return $resultStr; }
效果展示:
5、視頻消息
private function receiveVideo($object) { $funcFlag = 0; $contentStr = "你發送的是視頻,媒體ID為:".$object->MediaId; $resultStr = $this->transmitText($object, $contentStr, $funcFlag); return $resultStr; }
效果展示:
6、位置消息
private function receiveLocation($object) { $funcFlag = 0; $contentStr = "你發送的是位置,緯度為:".$object->Location_X.";經度為:".$object->Location_Y.";縮放級別為:".$object->Scale.";位置為:".$object->Label; $resultStr = $this->transmitText($object, $contentStr, $funcFlag); return $resultStr; }
效果展示:
7、鏈接消息
private function receiveLink($object) { $funcFlag = 0; $contentStr = "你發送的是鏈接,標題為:".$object->Title.";內容為:".$object->Description.";鏈接地址為:".$object->Url; $resultStr = $this->transmitText($object, $contentStr, $funcFlag); return $resultStr; }
效果展示:
第五章 回復用戶不同消息
定義回復消息處理函數
1、文本消息
private function transmitText($object, $content, $flag = 0) { $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>%d</FuncFlag> </xml>"; $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $flag); return $resultStr; }
2、圖文消息
private function transmitNews($object, $arr_item, $flag = 0) { if(!is_array($arr_item)) return; $itemTpl = " <item> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <PicUrl><![CDATA[%s]]></PicUrl> <Url><![CDATA[%s]]></Url> </item> "; $item_str = ""; foreach ($arr_item as $item) $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['Picurl'], $item['Url']); $newsTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[news]]></MsgType> <Content><![CDATA[]]></Content> <ArticleCount>%s</ArticleCount> <Articles> $item_str</Articles> <FuncFlag>%s</FuncFlag> </xml>"; $resultStr = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($arr_item), $flag); return $resultStr; }
3、音樂消息
private function transmitMusic($object, $musicArray, $flag = 0) { $itemTpl = "<Music> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <MusicUrl><![CDATA[%s]]></MusicUrl> <HQMusicUrl><![CDATA[%s]]></HQMusicUrl> </Music>"; $item_str = sprintf($itemTpl, $musicArray['Title'], $musicArray['Description'], $musicArray['MusicUrl'], $musicArray['HQMusicUrl']); $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[music]]></MsgType> $item_str <FuncFlag>%d</FuncFlag> </xml>"; $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $flag); return $resultStr; }
原文:http://www.cnblogs.com/txw1958/p/wechat-tutorial.html
構造不同回復消息類型
1、回復文本
if($keyword == "文本"){ $contentStr = "這是個文本消息"; $resultStr = $this->transmitText($object, $contentStr, $funcFlag); }
效果展示:
2、回復單圖文
else if($keyword == "圖文" || $keyword == "單圖文"){ $dateArray = array(); $dateArray[] = array("Title"=>"單圖文標題", "Description"=>"單圖文內容", "Picurl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958"); $resultStr = $this->transmitNews($object, $dateArray, $funcFlag); }
效果展示:
3、回復多圖文
else if($keyword == "多圖文"){ $dateArray = array(); $dateArray[] = array("Title"=>"多圖文1標題", "Description"=>"", "Picurl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958"); $dateArray[] = array("Title"=>"多圖文2標題", "Description"=>"", "Picurl"=>"http://d.hiphotos.bdimg.com/wisegame/pic/item/f3529822720e0cf3ac9f1ada0846f21fbe09aaa3.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958"); $dateArray[] = array("Title"=>"多圖文3標題", "Description"=>"", "Picurl"=>"http://g.hiphotos.bdimg.com/wisegame/pic/item/18cb0a46f21fbe090d338acc6a600c338644adfd.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958"); $resultStr = $this->transmitNews($object, $dateArray, $funcFlag); }
效果展示:
4、回復音樂
else if($keyword == "音樂"){ $musicArray = array("Title"=>"最炫民族風", "Description"=>"歌手:鳳凰傳奇", "MusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3","HQMusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3");$resultStr = $this->transmitMusic($object, $musicArray, $funcFlag); }
效果展示:
==================