本文轉自:http://www.cnblogs.com/txw1958/p/wechat-tutorial.htmlphp
關鍵字:微信公衆平臺開發 做者:方倍工做室html
在這篇微信公衆平臺開發教程中,咱們假定你已經有了PHP語言程序、MySQL數據庫、計算機網絡通信、及HTTP/XML/CSS/JS等基礎。git
咱們將使用微信公衆帳號方倍工做室做爲講解的例子,二維碼見底部。web
本系列教程將引導你完成以下任務:數據庫
第一章 申請服務器資源json
建立新浪雲計算應用api
申請帳號服務器
咱們使用SAE新浪雲計算平臺做爲服務器資源,而且申請PHP環境+MySQL數據庫做爲程序運行環境。 申請地址:http://sae.sina.com.cn/ ,使用新浪微博帳號能夠直接登陸SAE,登陸後SAE將贈送500個免費雲豆。微信
建立新應用 http://www.cnblogs.com/txw1958/p/wechat-tutorial.html 網絡
登陸後點擊頂部【個人首頁】
點擊下側的建立新應用,這時會彈出提示, 禁止放置違法違規內容,點擊繼續建立,彈出以下窗口。
選擇一個未使用的appid,若是總是已經被使用不知道該什麼好,就填寫你的QQ號或者手機號吧。
填寫二級域名AppID、應用名稱、驗證碼,開發語言選擇PHP,應用類型選擇web應用。而後點擊建立應用
應用建立成功。並自動跳轉到應用列表中,能夠看到已經有剛纔建立的CCTV-7
建立版本 http://www.cnblogs.com/txw1958/p/wechat-tutorial.html
選擇CCTV-7右側的應用管理下面的代碼管理,
跳轉到代碼管理
點擊右側的
版本號默認爲1,點擊建立,成功後以下圖所示:
到這裏,就成功建立了一個域名URL爲 http://cctv7.sinaapp.com/ 的應用了。
原文:http://www.cnblogs.com/txw1958/p/wechat-tutorial.html
上傳代碼 http://www.cnblogs.com/txw1958/p/wechat-tutorial.html
將如下代碼複製下來,另存爲index.php。必須使用專業的開發編輯軟件操做,例如Notepad++,不要使用Windows自帶的記事本等。
<?php /* 方倍工做室 http://www.cnblogs.com/txw1958/ CopyRight 2013 www.doucube.com All Rights Reserved */ define("TOKEN", "weixin"); $wechatObj = new wechatCallbackapiTest(); if (isset($_GET['echostr'])) { $wechatObj->valid(); }else{ $wechatObj->responseMsg(); } class wechatCallbackapiTest { public function valid() { $echoStr = $_GET["echostr"]; if($this->checkSignature()){ echo $echoStr; exit; } } private function checkSignature() { $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 ){ return true; }else{ return false; } } public function responseMsg() { $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($keyword == "?" || $keyword == "?") { $msgType = "text"; $contentStr = date("Y-m-d H:i:s",time()); $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; } }else{ echo ""; exit; } } } ?>
而後將index.php文件壓縮成ZIP格式,注意不能用RAR格式
這樣會生成一個index.zip的文件。或者直接下載方倍已經壓縮好的zip文件 點此下載
在代碼管理界面中,選擇操做按鈕。
選擇上傳代碼包。
點擊上傳文件,選擇剛纔壓縮好的index.zip文件,點擊上傳,上傳成功後以下所示
點擊操做按鈕下的代碼編輯,
咱們能夠看到index.php已經上傳成功,雙擊能夠查看編輯裏面的代碼
新浪雲應用的建立就成功了。
第二章 啓用開發模式
微信公衆平臺開發模式
高級功能
微信公衆平臺地址:https://mp.weixin.qq.com
登陸微信公衆平臺後臺,選擇高級功能,進入後就看到兩種模式
咱們須要先關閉編輯模式。點擊編輯模式的進入
滑動關閉
開發模式
進入開發模式裏面
點擊成爲開發者
彈出URL和Token填寫框
此處的URL爲上篇中介紹的雲應用的域名,而Token在index.php中定義爲weixin。提交後提示你已成爲開發者。
再滑動右上角啓用按鈕。
恭喜,你成功啓用開發模式。
自動回覆
在上面的例子中,實現了一個發送「?」就能回覆當前時間的功能。 效果以下:
至此,你的微信公衆平臺帳號已經實現自動回覆了。
第三章 基礎接口消息及事件
全部帳號在申請以後,都將得到基礎接口的權限,基礎接口中將包括接收用戶消息,向用戶回覆消息,接受事件推送等三種服務。
接收用戶消息
目前普通用戶能向公衆帳號推送五種格式的消息:文本(包括表情)、語音、圖片、視頻、位置、連接。 下面就這五種分別詳解以下:
1. 文本(包括表情) 發送文本及表情
2. 圖片
發送圖片
3. 語音
發送語音
4. 視頻
發送視頻
5. 位置
發送位置
6. 連接
發送連接
向用戶回覆消息
目前普通公衆帳號能向用戶推送六種格式的消息:文本、圖文、音樂、圖片、語音、視頻。其中圖文消息包括單條圖文消息和多條圖文消息,展現方式有一點點不一樣。
下面就這幾種分別詳解以下:【圖片、語音、視頻因爲須要用到和高級接口相關的media_id,在本教程中暫不討論。】
1. 文本消息格式 回覆文本
2. 圖文消息格式
2.1 單條圖文消息 回覆單條圖文
2.2 多圖文消息
回覆多圖文
3. 音樂消息
回覆音樂消息
接收事件推送
目前用戶在關注和取消關注,以及點擊菜單的時候會自動向公衆平臺發送事件推送消息:
1. 關注事件
第四章 微信公衆平臺PHP SDK
方倍工做室開發了微信公衆平臺的PHPSDK,集成了目前全部消息及事件的接收及發送,代碼以下:
<?php /* 方倍工做室 http://www.cnblogs.com/txw1958/ CopyRight 2014 All Rights Reserved */ define("TOKEN", "weixin"); $wechatObj = new wechatCallbackapiTest(); if (!isset($_GET['echostr'])) { $wechatObj->responseMsg(); }else{ $wechatObj->valid(); } class wechatCallbackapiTest { //驗證消息 public function valid() { $echoStr = $_GET["echostr"]; if($this->checkSignature()){ echo $echoStr; exit; } } //檢查簽名 private function checkSignature() { $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr, SORT_STRING); $tmpStr = implode($tmpArr); $tmpStr = sha1($tmpStr); if($tmpStr == $signature){ return true; }else{ return false; } } //響應消息 public function responseMsg() { $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; if (!empty($postStr)){ $this->logger("R ".$postStr); $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $RX_TYPE = trim($postObj->MsgType); switch ($RX_TYPE) { case "event": $result = $this->receiveEvent($postObj); break; case "text": $result = $this->receiveText($postObj); break; case "image": $result = $this->receiveImage($postObj); break; case "location": $result = $this->receiveLocation($postObj); break; case "voice": $result = $this->receiveVoice($postObj); break; case "video": $result = $this->receiveVideo($postObj); break; case "link": $result = $this->receiveLink($postObj); break; default: $result = "unknown msg type: ".$RX_TYPE; break; } $this->logger("T ".$result); echo $result; }else { echo ""; exit; } } //接收事件消息 private function receiveEvent($object) { $content = ""; switch ($object->Event) { case "subscribe": $content = "歡迎關注方倍工做室 "; $content .= (!empty($object->EventKey))?("\n來自二維碼場景 ".str_replace("qrscene_","",$object->EventKey)):""; break; case "unsubscribe": $content = "取消關注"; break; case "SCAN": $content = "掃描場景 ".$object->EventKey; break; case "CLICK": switch ($object->EventKey) { case "COMPANY": $content = "方倍工做室提供互聯網相關產品與服務。"; break; default: $content = "點擊菜單:".$object->EventKey; break; } break; case "LOCATION": $content = "上傳位置:緯度 ".$object->Latitude.";經度 ".$object->Longitude; break; case "VIEW": $content = "跳轉連接 ".$object->EventKey; break; default: $content = "receive a new event: ".$object->Event; break; } $result = $this->transmitText($object, $content); return $result; } //接收文本消息 private function receiveText($object) { switch ($object->Content) { case "文本": $content = "這是個文本消息"; break; case "圖文": case "單圖文": $content = array(); $content[] = array("Title"=>"單圖文標題", "Description"=>"單圖文內容", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958"); break; case "多圖文": $content = array(); $content[] = array("Title"=>"多圖文1標題", "Description"=>"", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958"); $content[] = array("Title"=>"多圖文2標題", "Description"=>"", "PicUrl"=>"http://d.hiphotos.bdimg.com/wisegame/pic/item/f3529822720e0cf3ac9f1ada0846f21fbe09aaa3.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958"); $content[] = array("Title"=>"多圖文3標題", "Description"=>"", "PicUrl"=>"http://g.hiphotos.bdimg.com/wisegame/pic/item/18cb0a46f21fbe090d338acc6a600c338644adfd.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958"); break; case "音樂": $content = array("Title"=>"最炫民族風", "Description"=>"歌手:鳳凰傳奇", "MusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3", "HQMusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3"); break; default: $content = date("Y-m-d H:i:s",time()); break; } if(is_array($content)){ if (isset($content[0]['PicUrl'])){ $result = $this->transmitNews($object, $content); }else if (isset($content['MusicUrl'])){ $result = $this->transmitMusic($object, $content); } }else{ $result = $this->transmitText($object, $content); } return $result; } //接收圖片消息 private function receiveImage($object) { $content = array("MediaId"=>$object->MediaId); $result = $this->transmitImage($object, $content); return $result; } //接收位置消息 private function receiveLocation($object) { $content = "你發送的是位置,緯度爲:".$object->Location_X.";經度爲:".$object->Location_Y.";縮放級別爲:".$object->Scale.";位置爲:".$object->Label; $result = $this->transmitText($object, $content); return $result; } //接收語音消息 private function receiveVoice($object) { if (isset($object->Recognition) && !empty($object->Recognition)){ $content = "你剛纔說的是:".$object->Recognition; $result = $this->transmitText($object, $content); }else{ $content = array("MediaId"=>$object->MediaId); $result = $this->transmitVoice($object, $content); } return $result; } //接收視頻消息 private function receiveVideo($object) { $content = array("MediaId"=>$object->MediaId, "ThumbMediaId"=>$object->ThumbMediaId, "Title"=>"", "Description"=>""); $result = $this->transmitVideo($object, $content); return $result; } //接收連接消息 private function receiveLink($object) { $content = "你發送的是連接,標題爲:".$object->Title.";內容爲:".$object->Description.";連接地址爲:".$object->Url; $result = $this->transmitText($object, $content); return $result; } //回覆文本消息 private function transmitText($object, $content) { $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content); return $result; } //回覆圖片消息 private function transmitImage($object, $imageArray) { $itemTpl = "<Image> <MediaId><![CDATA[%s]]></MediaId> </Image>"; $item_str = sprintf($itemTpl, $imageArray['MediaId']); $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[image]]></MsgType> $item_str </xml>"; $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time()); return $result; } //回覆語音消息 private function transmitVoice($object, $voiceArray) { $itemTpl = "<Voice> <MediaId><![CDATA[%s]]></MediaId> </Voice>"; $item_str = sprintf($itemTpl, $voiceArray['MediaId']); $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[voice]]></MsgType> $item_str </xml>"; $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time()); return $result; } //回覆視頻消息 private function transmitVideo($object, $videoArray) { $itemTpl = "<Video> <MediaId><![CDATA[%s]]></MediaId> <ThumbMediaId><![CDATA[%s]]></ThumbMediaId> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> </Video>"; $item_str = sprintf($itemTpl, $videoArray['MediaId'], $videoArray['ThumbMediaId'], $videoArray['Title'], $videoArray['Description']); $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[video]]></MsgType> $item_str </xml>"; $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time()); return $result; } //回覆圖文消息 private function transmitNews($object, $newsArray) { if(!is_array($newsArray)){ return; } $itemTpl = " <item> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <PicUrl><![CDATA[%s]]></PicUrl> <Url><![CDATA[%s]]></Url> </item> "; $item_str = ""; foreach ($newsArray 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> </xml>"; $result = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($newsArray)); return $result; } //回覆音樂消息 private function transmitMusic($object, $musicArray) { $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 </xml>"; $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time()); return $result; } //日誌記錄 private function logger($log_content) { if(isset($_SERVER['HTTP_APPNAME'])){ //SAE sae_set_display_errors(false); sae_debug($log_content); sae_set_display_errors(true); }else if($_SERVER['REMOTE_ADDR'] != "127.0.0.1"){ //LOCAL $max_size = 10000; $log_filename = "log.xml"; if(file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)){unlink($log_filename);} file_put_contents($log_filename, date('H:i:s')." ".$log_content."\r\n", FILE_APPEND); } } } ?>
使用第一章一樣方法,將上述代碼另存爲index.php,壓縮成index.zip,或者直接點擊下載,上傳到SAE,你的公衆帳號就能接收及回覆全部消息類型及事件通知了。
在公衆帳號中回覆如下文字,你將獲得和上一章同樣的回覆內容。
文本
圖文
多圖文
音樂
時間
還能夠嘗試如下操做,體驗一下其餘消息
發送一張圖片給公衆帳號
發送一段語音給公衆帳號
發送一段視頻給公衆帳號
發送位置信息給公衆帳號
發送收藏中的連接給公衆帳號
第五章 微信公衆平臺開發模式原理分析
在體驗了上一節的各類功能以後,咱們只是知其然,這一節裏面,將介紹在上面的基礎上介紹微信公衆平臺收發消息機制及原理,這是知其因此然。
開發模式成爲開發者時的消息校驗原理
在開發者首次提交驗證申請時,微信服務器將發送GET請求到填寫的URL上,而且帶上四個參數(signature、timestamp、nonce、echostr),開發者經過對簽名(即signature)的效驗,來判斷此條消息的真實性。
此後,每次開發者接收用戶消息的時候,微信也都會帶上前面三個參數(signature、timestamp、nonce)訪問開發者設置的URL,開發者依然經過對簽名的效驗判斷此條消息的真實性。效驗方式與首次提交驗證申請一致。
參數 | 描述 |
---|---|
signature | 微信加密簽名,signature結合了開發者填寫的token參數和請求中的timestamp參數、nonce參數。 |
timestamp | 時間戳 |
nonce | 隨機數 |
echostr | 隨機字符串 |
開發者經過檢驗signature對請求進行校驗(下面有校驗方式)。若確認這次GET請求來自微信服務器,請原樣返回echostr參數內容,則接入生效,成爲開發者成功,不然接入失敗。
加密/校驗流程以下: 1. 將token、timestamp、nonce三個參數進行字典序排序 2. 將三個參數字符串拼接成一個字符串進行sha1加密 3. 開發者得到加密後的字符串可與signature對比,標識該請求來源於微信
啓用接口是由代碼中的checkSignature()函數來實現校驗的。若是對這一原理難以理解,能夠暫時不用深究,繼續看下面。
成爲開發者後消息收發時的原理
再來看下這個圖,當用戶發送一個「?」時,系統回覆了一個時間
這一原理的消息流程圖以下所示。
從上圖能夠看出,用戶在發送一個?後,微信服務器將組裝一個消息發送給咱們本身的服務器,本身的服務器而後回覆一個時間,而且將該時間也按必定的規則組裝,回覆給公衆帳號,公衆帳號再回復給用戶,在這個收發過程當中,發送方和接收方進行了調換(ToUserName和FromUserName值互換),收發都是以xml格式在後臺進行傳輸的,
因此掌握各類消息類型的收發就是進行微信公衆平臺開發的基礎!
下面對前面所述的各類消息類型講解其XML數據包的格式。
各類收發消息的XML數據包分析
接收消息
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值對應
第六章 開發天氣預報功能
這一章裏,咱們來快速開發天氣預報功能、咱們使用方倍工做室的相應接口來實現。下面代碼實現了這樣該功能。
<?php /* 方倍工做室 CopyRight 2014 All Rights Reserved */ define("TOKEN", "weixin"); $wechatObj = new wechatCallbackapiTest(); if (!isset($_GET['echostr'])) { $wechatObj->responseMsg(); }else{ $wechatObj->valid(); } class wechatCallbackapiTest { public function valid() { $echoStr = $_GET["echostr"]; if($this->checkSignature()){ echo $echoStr; exit; } } private function checkSignature() { $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr, SORT_STRING); $tmpStr = implode($tmpArr); $tmpStr = sha1($tmpStr); if($tmpStr == $signature){ return true; }else{ return false; } } public function responseMsg() { $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; if (!empty($postStr)){ $this->logger("R ".$postStr); $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $RX_TYPE = trim($postObj->MsgType); switch ($RX_TYPE) { case "event": $result = $this->receiveEvent($postObj); break; case "text": $result = $this->receiveText($postObj); break; } $this->logger("T ".$result); echo $result; }else { echo ""; exit; } } private function receiveEvent($object) { $content = ""; switch ($object->Event) { case "subscribe": $content = "歡迎關注方倍工做室 "; break; } $result = $this->transmitText($object, $content); return $result; } private function receiveText($object) { $keyword = trim($object->Content);$url = "http://apix.sinaapp.com/weather/?appkey=".$object->ToUserName."&city=".urlencode($keyword); $output = file_get_contents($url); $content = json_decode($output, true); $result = $this->transmitNews($object, $content); return $result; } private function transmitText($object, $content) { $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content); return $result; } private function transmitNews($object, $newsArray) { if(!is_array($newsArray)){ return; } $itemTpl = " <item> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <PicUrl><![CDATA[%s]]></PicUrl> <Url><![CDATA[%s]]></Url> </item> "; $item_str = ""; foreach ($newsArray 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> </xml>"; $result = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($newsArray)); return $result; } private function logger($log_content) { } } ?>
在公衆帳號中使用的命令以下:
在你的公衆帳號輸入相應的命令,實現效果相似以下所示:
第七章 小結
總的來講,經過本教程,你獲得瞭如下收穫:
接下來該作什麼呢?你能夠學習開發一些基礎的經常使用功能,推薦:方倍工做室 編寫,機械工業出版社 出版的《微信公衆平臺開發最佳實踐》,裏面包含不少php開發技巧、數據庫使用、及近30項微信公衆平臺實用功能或技術 。
點擊圖標購買:
學習完這一層次以後,你能夠開始本身勝任一個小型的微信公衆平臺帳號功能的開發了,好比經過爲一個帳號開發出自定義菜單,爲種菜單添加相應的響應消息,學習完這一層次以後,你能勝任一箇中小型帳號的功能的開發了。
另外咱們提供微信公衆平臺開發實戰指導,內容包括:1. 微信公衆平臺基礎疑難解答;2. 贈送微信公衆平臺開發實戰教程電子版,高級接口教程電子版,贈送《微信公衆平臺開發最佳實踐》;3.微信公衆平臺開發技術諮詢;4. 全部問題在24小時內獲得回覆;5.緊急技術問題的電話指導;6. 提供天氣、快遞、點歌、翻譯、人臉識別、四六級、星座、解夢、笑話、蘋果產品查詢等功能源碼;7. 專屬QQ羣指導。歡迎加入。
服務購買地址 http://item.taobao.com/item.htm?id=37979420283
關注咱們
更多功能、代碼、教程、行業祕聞,將在微信公衆帳號中發佈,歡迎關注,二維碼見下 我的微信帳號
原文:http://www.cnblogs.com/txw1958/p/wechat-tutorial.html
====================================================================
★學習微信公衆平臺開發教程 點擊進入 ★微信平臺付費定製開發 聯繫QQ 1354386063