我們將使用微信公眾賬號(hào)方倍工作室作為講解的例子,二維碼見底部。
本系列教程將引導(dǎo)你完成如下任務(wù):
創(chuàng)建新浪云計(jì)算平臺(tái)應(yīng)用啟用微信公眾平臺(tái)開發(fā)模式基礎(chǔ)接口消息及事件微信公眾平臺(tái)php sdk微信公眾平臺(tái)開發(fā)模式原理開發(fā)天氣預(yù)報(bào)功能
第一章 申請(qǐng)服務(wù)器資源
創(chuàng)建新浪云計(jì)算應(yīng)用
申請(qǐng)賬號(hào)
我們使用sae新浪云計(jì)算平臺(tái)作為服務(wù)器資源,并且申請(qǐng)php環(huán)境+mysql數(shù)據(jù)庫作為程序運(yùn)行環(huán)境。
申請(qǐng)地址: ,使用新浪微博賬號(hào)可以直接登錄sae,登錄后sae將贈(zèng)送500個(gè)免費(fèi)云豆。
創(chuàng)建新應(yīng)用
登錄后點(diǎn)擊頂部【我的首頁】
點(diǎn)擊下側(cè)的創(chuàng)建新應(yīng)用,這時(shí)會(huì)彈出提示, 禁止放置違法違規(guī)內(nèi)容,點(diǎn)擊繼續(xù)創(chuàng)建,彈出如下窗口。
選擇一個(gè)未使用的appid,如果老是已經(jīng)被使用不知道該什么好,就填寫你的qq號(hào)或者手機(jī)號(hào)吧。
填寫二級(jí)域名appid、應(yīng)用名稱、驗(yàn)證碼,開發(fā)語言選擇php,應(yīng)用類型選擇web應(yīng)用。然后點(diǎn)擊創(chuàng)建應(yīng)用
應(yīng)用創(chuàng)建成功。并自動(dòng)跳轉(zhuǎn)到應(yīng)用列表中,可以看到已經(jīng)有剛才創(chuàng)建的cctv-7
創(chuàng)建版本
選擇cctv-7右側(cè)的應(yīng)用管理下面的代碼管理,
跳轉(zhuǎn)到代碼管理
點(diǎn)擊右側(cè)的
版本號(hào)默認(rèn)為1,點(diǎn)擊創(chuàng)建,成功后如下圖所示:
到這里,就成功創(chuàng)建了一個(gè)域名url為 http://cctv7.sinaapp.com/ 的應(yīng)用了。
上傳代碼
將以下代碼復(fù)制下來,另存為index.php。必須使用專業(yè)的開發(fā)編輯軟件操作,例如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格式
這樣會(huì)生成一個(gè)index.zip的文件。或者直接下載方倍已經(jīng)壓縮好的zip文件
在代碼管理界面中,選擇操作按鈕。
選擇上傳代碼包。
點(diǎn)擊上傳文件,選擇剛才壓縮好的index.zip文件,點(diǎn)擊上傳,上傳成功后如下所示,如果上傳有問題,請(qǐng)?jiān)赾hrome瀏覽器下重試一下。
點(diǎn)擊操作按鈕下的代碼編輯,
我們可以看到index.php已經(jīng)上傳成功,雙擊可以查看編輯里面的代碼
新浪云應(yīng)用的創(chuàng)建就成功了。
第二章 啟用開發(fā)模式
微信公眾平臺(tái)開發(fā)模式
高級(jí)功能
微信公眾平臺(tái)地址:
登錄微信公眾平臺(tái)后臺(tái),選擇高級(jí)功能,進(jìn)入后就看到兩種模式
我們需要先關(guān)閉編輯模式。點(diǎn)擊編輯模式的進(jìn)入
滑動(dòng)關(guān)閉
開發(fā)模式
進(jìn)入開發(fā)模式里面
點(diǎn)擊成為開發(fā)者
彈出url和token填寫框
此處的url為上篇中介紹的云應(yīng)用的域名,而token在index.php中定義為weixin。提交后提示你已成為開發(fā)者。
再滑動(dòng)右上角啟用按鈕。
恭喜,你成功啟用開發(fā)模式。
自動(dòng)回復(fù)
在上面的例子中,實(shí)現(xiàn)了一個(gè)發(fā)送“?”就能回復(fù)當(dāng)前時(shí)間的功能。
效果如下:
至此,你的微信公眾平臺(tái)賬號(hào)已經(jīng)實(shí)現(xiàn)自動(dòng)回復(fù)了。
第三章 基礎(chǔ)接口消息及事件
所有賬號(hào)在申請(qǐng)之后,都將獲得基礎(chǔ)接口的權(quán)限,基礎(chǔ)接口中將包括接收用戶消息,向用戶回復(fù)消息,接受事件推送等三種服務(wù)。
接收用戶消息
目前普通用戶能向公眾賬號(hào)推送五種格式的消息:文本(包括表情)、語音、圖片、視頻、位置、鏈接。
下面就這五種分別詳解如下:
1. 文本(包括表情)
發(fā)送文本及表情
2. 圖片
發(fā)送圖片
3. 語音
發(fā)送語音
4. 視頻
發(fā)送視頻
5. 位置
發(fā)送位置
6. 鏈接
發(fā)送鏈接
向用戶回復(fù)消息
目前普通公眾賬號(hào)能向用戶推送六種格式的消息:文本、圖文、音樂、圖片、語音、視頻。其中圖文消息包括單條圖文消息和多條圖文消息,展示方式有一點(diǎn)點(diǎn)不同。
下面就這幾種分別詳解如下:【圖片、語音、視頻由于需要用到和高級(jí)接口相關(guān)的media_id,在本教程中暫不討論。】
1. 文本消息格式
回復(fù)文本
2. 圖文消息格式
2.1 單條圖文消息
回復(fù)單條圖文
2.2 多圖文消息
回復(fù)多圖文
3. 音樂消息
回復(fù)音樂消息
接收事件推送
目前用戶在關(guān)注和取消關(guān)注,以及點(diǎn)擊菜單的時(shí)候會(huì)自動(dòng)向公眾平臺(tái)發(fā)送事件推送消息:
1. 關(guān)注事件
第四章 微信公眾平臺(tái)php sdk
方倍工作室開發(fā)了微信公眾平臺(tái)的phpsdk,集成了目前所有消息及事件的接收及發(fā)送,代碼如下:
<?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
{
//驗(yàn)證消息
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;
}
}
//響應(yīng)消息
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 = "歡迎關(guān)注方倍工作室 ";
$content .= (!empty($object->eventkey))?("\n來自二維碼場(chǎng)景 ".str_replace("qrscene_","",$object->eventkey)):"";
break;
case "unsubscribe":
$content = "取消關(guān)注";
break;
case "scan":
$content = "掃描場(chǎng)景 ".$object->eventkey;
break;
case "click":
switch ($object->eventkey)
{
case "company":
$content = "方倍工作室提供互聯(lián)網(wǎng)相關(guān)產(chǎn)品與服務(wù)。";
break;
default:
$content = "點(diǎn)擊菜單:".$object->eventkey;
break;
}
break;
case "location":
$content = "上傳位置:緯度 ".$object->latitude.";經(jīng)度 ".$object->longitude;
break;
case "view":
$content = "跳轉(zhuǎn)鏈接 ".$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 = "這是個(gè)文本消息";
break;
case "圖文":
case "單圖文":
$content = array();
$content[] = array("title"=>"單圖文標(biāo)題", "description"=>"單圖文內(nèi)容", "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標(biāo)題", "description"=>"", "picurl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "url" =>"http://m.cnblogs.com/?u=txw1958");
$content[] = array("title"=>"多圖文2標(biāo)題", "description"=>"", "picurl"=>"http://d.hiphotos.bdimg.com/wisegame/pic/item/f3529822720e0cf3ac9f1ada0846f21fbe09aaa3.jpg", "url" =>"http://m.cnblogs.com/?u=txw1958");
$content[] = array("title"=>"多圖文3標(biāo)題", "description"=>"", "picurl"=>"http://g.hiphotos.bdimg.com/wisegame/pic/item/18cb0a46f21fbe090d338acc6a600c338644adfd.jpg", "url" =>"http://m.cnblogs.com/?u=txw1958");
break;
case "音樂":
$content = array("title"=>"最炫民族風(fēng)", "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;
}
//接收?qǐng)D片消息
private function receiveimage($object)
{
$content = array("mediaid"=>$object->mediaid);
$result = $this->transmitimage($object, $content);
return $result;
}
//接收位置消息
private function receivelocation($object)
{
$content = "你發(fā)送的是位置,緯度為:".$object->location_x.";經(jīng)度為:".$object->location_y.";縮放級(jí)別為:".$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 = "你發(fā)送的是鏈接,標(biāo)題為:".$object->title.";內(nèi)容為:".$object->description.";鏈接地址為:".$object->url;
$result = $this->transmittext($object, $content);
return $result;
}
//回復(fù)文本消息
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;
}
//回復(fù)圖片消息
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;
}
//回復(fù)語音消息
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;
}
//回復(fù)視頻消息
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;
}
//回復(fù)圖文消息
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;
}
//回復(fù)音樂消息
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,或者直接點(diǎn)擊下載,上傳到sae,你的公眾賬號(hào)就能接收及回復(fù)所有消息類型及事件通知了。
在公眾賬號(hào)中回復(fù)以下文字,你將得到和上一章一樣的回復(fù)內(nèi)容。
文本
圖文
多圖文
音樂
時(shí)間
還可以嘗試以下操作,體驗(yàn)一下其他消息
發(fā)送一張圖片給公眾賬號(hào)
發(fā)送一段語音給公眾賬號(hào)
發(fā)送一段視頻給公眾賬號(hào)
發(fā)送位置信息給公眾賬號(hào)
發(fā)送收藏中的鏈接給公眾賬號(hào)第五章 微信公眾平臺(tái)開發(fā)模式原理分析
在體驗(yàn)了上一節(jié)的各種功能之后,我們只是知其然,這一節(jié)里面,將介紹在上面的基礎(chǔ)上介紹微信公眾平臺(tái)收發(fā)消息機(jī)制及原理,這是知其所以然。
開發(fā)模式成為開發(fā)者時(shí)的消息校驗(yàn)原理
在開發(fā)者首次提交驗(yàn)證申請(qǐng)時(shí),微信服務(wù)器將發(fā)送get請(qǐng)求到填寫的url上,并且?guī)纤膫€(gè)參數(shù)(signature、timestamp、nonce、echostr),開發(fā)者通過對(duì)簽名(即signature)的效驗(yàn),來判斷此條消息的真實(shí)性。
此后,每次開發(fā)者接收用戶消息的時(shí)候,微信也都會(huì)帶上前面三個(gè)參數(shù)(signature、timestamp、nonce)訪問開發(fā)者設(shè)置的url,開發(fā)者依然通過對(duì)簽名的效驗(yàn)判斷此條消息的真實(shí)性。效驗(yàn)方式與首次提交驗(yàn)證申請(qǐng)一致。
參數(shù) |
描述 |
---|---|
signature |
微信加密簽名,signature結(jié)合了開發(fā)者填寫的token參數(shù)和請(qǐng)求中的timestamp參數(shù)、nonce參數(shù)。 |
timestamp |
時(shí)間戳 |
nonce |
隨機(jī)數(shù) |
echostr |
隨機(jī)字符串 |
開發(fā)者通過檢驗(yàn)signature對(duì)請(qǐng)求進(jìn)行校驗(yàn)(下面有校驗(yàn)方式)。若確認(rèn)此次get請(qǐng)求來自微信服務(wù)器,請(qǐng)?jiān)瓨臃祷豦chostr參數(shù)內(nèi)容,則接入生效,成為開發(fā)者成功,否則接入失敗。
加密/校驗(yàn)流程如下:
1. 將token、timestamp、nonce三個(gè)參數(shù)進(jìn)行字典序排序
2. 將三個(gè)參數(shù)字符串拼接成一個(gè)字符串進(jìn)行sha1加密
3. 開發(fā)者獲得加密后的字符串可與signature對(duì)比,標(biāo)識(shí)該請(qǐng)求來源于微信啟用接口是由代碼中的checksignature()函數(shù)來實(shí)現(xiàn)校驗(yàn)的。如果對(duì)這一原理難以理解,可以暫時(shí)不用深究,繼續(xù)看下面。
成為開發(fā)者后消息收發(fā)時(shí)的原理
再來看下這個(gè)圖,當(dāng)用戶發(fā)送一個(gè)“?”時(shí),系統(tǒng)回復(fù)了一個(gè)時(shí)間
這一原理的消息流程圖如下所示。
從上圖可以看出,用戶在發(fā)送一個(gè)?后,微信服務(wù)器將組裝一個(gè)消息發(fā)送給我們自己的服務(wù)器,自己的服務(wù)器然后回復(fù)一個(gè)時(shí)間,并且將該時(shí)間也按一定的規(guī)則組裝,回復(fù)給公眾賬號(hào),公眾賬號(hào)再回復(fù)給用戶,在這個(gè)收發(fā)過程中,發(fā)送方和接收方進(jìn)行了調(diào)換(tousername和fromusername值互換),收發(fā)都是以xml格式在后臺(tái)進(jìn)行傳輸?shù)模?/p>
所以掌握各種消息類型的收發(fā)就是進(jìn)行微信公眾平臺(tái)開發(fā)的基礎(chǔ)!
下面對(duì)前面所述的各種消息類型講解其xml數(shù)據(jù)包的格式。
各種收發(fā)消息的xml數(shù)據(jù)包分析
接收消息
1. 文本(包括表情)
發(fā)送文本及表情
文字后臺(tái)格式:
<xml>
<tousername><![cdata[gh_680bdefc8c5d]]></tousername>
<fromusername><![cdata[oidrpjqasytpnxrmps9o_ruzgsfk]]></fromusername>
<createtime>1359028446</createtime>
<msgtype><![cdata[text]]></msgtype>
<content><![cdata[測(cè)試文字]]></content>
<msgid>5836982729904121631</msgid>
</xml>
表情后臺(tái)格式
<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 消息接收方微信號(hào),一般為公眾平臺(tái)賬號(hào)微信號(hào)
fromusername 消息發(fā)送方微信號(hào)
createtime 消息創(chuàng)建時(shí)間
msgtype 消息類型;文本消息為text
content 消息內(nèi)容
msgid 消息id號(hào)
可以看出,文本和表情的消息類型均為文本
2. 圖片
發(fā)送圖片
后臺(tái)格式:
<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 消息接收方微信號(hào),一般為公眾平臺(tái)賬號(hào)微信號(hào)
fromusername 消息發(fā)送方微信號(hào)
createtime 消息創(chuàng)建時(shí)間
msgtype 消息類型;圖片消息為image
picurl 圖片鏈接地址,可以用http get獲取
msgid 消息id號(hào)
3. 語音
發(fā)送語音
后臺(tái)格式:
<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 消息接收方微信號(hào),一般為公眾平臺(tái)賬號(hào)微信號(hào)
fromusername 消息發(fā)送方微信號(hào)
createtime 消息創(chuàng)建時(shí)間
msgtype 消息類型;語音消息為voice
mediaid 媒體id
format 語音格式,這里為amr
msgid 消息id號(hào)
附:amr接口簡(jiǎn)介
全稱adaptive multi-rate,主要用于移動(dòng)設(shè)備的音頻,壓縮比比較大,但相對(duì)其他的壓縮格式質(zhì)量比較差,由于多用于人聲,通話,效果還是很不錯(cuò)的。
4. 視頻
發(fā)送視頻
后臺(tái)格式:
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 消息接收方微信號(hào),一般為公眾平臺(tái)賬號(hào)微信號(hào)
fromusername 消息發(fā)送方微信號(hào)
createtime 消息創(chuàng)建時(shí)間
msgtype 消息類型;視頻消息為video
mediaid 媒體id
thumbmediaid 媒體縮略id?
msgid 消息id號(hào)
5. 位置
發(fā)送位置
后臺(tái)格式:
<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[中國廣東省深圳市南山區(qū)華僑城深南大道9789號(hào) 郵政編碼: 518057]]></label>
<msgid>5837017832671832047</msgid>
</xml>
xml格式講解
tousername 消息接收方微信號(hào),一般為公眾平臺(tái)賬號(hào)微信號(hào)
fromusername 消息發(fā)送方微信號(hào)
createtime 消息創(chuàng)建時(shí)間
msgtype 消息類型,地理位置為location
location_x 地理位置緯度
location_y 地理位置經(jīng)度
scale 地圖縮放大小
label 地理位置信息
msgid 消息id號(hào)
6. 鏈接
發(fā)送鏈接
后臺(tái)格式:
<xml>
<tousername><![cdata[gh_680bdefc8c5d]]></tousername>
<fromusername><![cdata[oidrpjl2lydftam-oxdgb4xzcnc8]]></fromusername>
<createtime>1359709372</createtime>
<msgtype><![cdata[link]]></msgtype>
<title><![cdata[微信公眾平臺(tái)開發(fā)者的江湖]]></title>
<description><![cdata[陳坤的微信公眾號(hào)這段時(shí)間大火,大家..]]></description>
<url><![cdata[http://israel.duapp.com/web/photo.php]]></url>
<msgid>5839907284805129867</msgid>
</xml>
xml格式講解
tousername 消息接收方微信號(hào),一般為公眾平臺(tái)賬號(hào)微信號(hào)
fromusername 消息發(fā)送方微信號(hào)
createtime 消息創(chuàng)建時(shí)間
msgtype 消息類型,鏈接為link
title 圖文消息標(biāo)題
description 圖文消息描述
url 點(diǎn)擊圖文消息跳轉(zhuǎn)鏈接
msgid 消息id號(hào)
發(fā)送消息
只介紹三種格式的消息:文本、圖文、音樂。其中圖文消息包括單條圖文消息和多條圖文消息,展示方式有一點(diǎn)點(diǎn)不同。
1. 文本消息格式
回復(fù)文本
后臺(tái)格式:
<xml>
<tousername><![cdata[oidrpjqasytpnxrmps9o_ruzgsfk]]></tousername>
<fromusername><![cdata[gh_680bdefc8c5d]]></fromusername>
<createtime>1359036631</createtime>
<msgtype><![cdata[text]]></msgtype>
<content><![cdata[【深圳】天氣實(shí)況 溫度:27℃ 濕度:59% 風(fēng)速:東北風(fēng)3級(jí)
11月03日 周日 27℃~23℃ 小雨 東北風(fēng)4-5級(jí)
11月04日 周一 26℃~21℃ 陣雨 微風(fēng)
11月05日 周二 27℃~22℃ 陰 微風(fēng)]]></content>
<funcflag>0</funcflag>
</xml>
xml格式講解
fromusername 消息發(fā)送方
tousername 消息接收方
createtime 消息創(chuàng)建時(shí)間
msgtype 消息類型,文本消息必須填寫text
content 消息內(nèi)容,大小限制在2048字節(jié),字段為空為不合法請(qǐng)求
funcflag 星標(biāo)字段
2. 圖文消息格式
2.1 單條圖文消息
回復(fù)單條圖文
后臺(tái)格式:
<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[[蘋果產(chǎn)品信息查詢]]></title>
<description><![cdata[序列號(hào):use imei number
imei號(hào):358031058974471
設(shè)備名稱:iphone 5c
設(shè)備顏色:
設(shè)備容量:
激活狀態(tài):已激活
電話支持:未過期[2014-01-13]
硬件保修:未過期[2014-10-14]
生產(chǎn)工廠:中國]]>
</description>
<picurl><![cdata[http://www.doucube.com/weixin/weather/icon/banner.jpg]]></picurl>
<url><![cdata[]]></url>
</item>
</articles>
<funcflag>0</funcflag>
</xml>
2.2 多圖文消息
回復(fù)多圖文
后臺(tái)數(shù)據(jù)格式
<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[【深圳】天氣實(shí)況 溫度:3℃ 濕度:43﹪ 風(fēng)速:西南風(fēng)2級(jí)]]></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℃ 晴 北風(fēng)3-4級(jí)轉(zhuǎn)東南風(fēng)小于3級(jí)]]></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℃ 晴 東南風(fēng)小于3級(jí)轉(zhuǎn)東北風(fēng)3-4級(jí)]]></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℃ 多云 東北風(fēng)3-4級(jí)轉(zhuǎn)東南風(fēng)小于3級(jí)]]></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℃ 多云 東南風(fēng)小于3級(jí)轉(zhuǎn)東北風(fēng)3-4級(jí)]]></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 消息發(fā)送方
tousername 消息接收方
createtime 消息創(chuàng)建時(shí)間
msgtype 消息類型,圖文消息必須填寫news
content 消息內(nèi)容,圖文消息可填空
articlecount 圖文消息個(gè)數(shù),限制為10條以內(nèi)
articles 多條圖文消息信息,默認(rèn)第一個(gè)item為大圖
title 圖文消息標(biāo)題
description 圖文消息描述
picurl 圖片鏈接,支持jpg、png格式,較好的效果為大圖640*320,小圖80*80
url 點(diǎn)擊圖文消息跳轉(zhuǎn)鏈接
funcflag 星標(biāo)字段
3. 音樂消息
回復(fù)音樂消息
后臺(tái)格式:
<xml>
<tousername><![cdata[ollb4jqgdo_crnvxk_wrnsywgtq8]]></tousername>
<fromusername><![cdata[gh_b629c48b653e]]></fromusername>
<createtime>1372310544</createtime>
<msgtype><![cdata[music]]></msgtype>
<music>
<title><![cdata[最炫民族風(fēng)]]></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 接收方帳號(hào)(收到的openid)
fromusername 開發(fā)者微信號(hào)
createtime 消息創(chuàng)建時(shí)間
msgtype 消息類型,此處為music
title 音樂標(biāo)題
description 音樂描述
musicurl 音樂鏈接
hqmusicurl 高質(zhì)量音樂鏈接,wifi環(huán)境優(yōu)先使用該鏈接播放音樂
funcflag 位0x0001被標(biāo)志時(shí),星標(biāo)剛收到的消息。
事件消息類型
目前用戶在關(guān)注和取消關(guān)注,以及點(diǎn)擊菜單的時(shí)候會(huì)自動(dòng)向公眾平臺(tái)發(fā)送事件推送消息:
1. 關(guān)注事件
<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. 取消關(guān)注事件
<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. 菜單點(diǎn)擊事件
<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 接收方微信號(hào)
fromusername 發(fā)送方微信號(hào),若為普通用戶,則是一個(gè)openid
createtime 消息創(chuàng)建時(shí)間
msgtype 消息類型,event
event 事件類型,subscribe(訂閱)、unsubscribe(取消訂閱)、click(自定義菜單點(diǎn)擊事件)
eventkey 事件key值,與自定義菜單接口中key值對(duì)應(yīng)
第六章 開發(fā)天氣預(yù)報(bào)功能
這一章里,我們來快速開發(fā)天氣預(yù)報(bào)功能、我們使用方倍工作室的相應(yīng)接口來實(shí)現(xiàn)。下面代碼實(shí)現(xiàn)了這樣該功能。
<?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 = "歡迎關(guān)注方倍工作室 ";
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)
{
}
}
?>
在公眾賬號(hào)中使用的命令如下:
1.發(fā)送城市名稱,如“深圳”,可以查詢?cè)摮鞘械奶鞖?br /> 在你的公眾賬號(hào)輸入相應(yīng)的命令,實(shí)現(xiàn)效果類似如下所示:
第七章 小結(jié)
總的來說,通過本教程,你得到了以下收獲:
1. 你通過本教程得到了一個(gè)免費(fèi)的云計(jì)算空間2. 你成功啟用了開發(fā)模式,并且實(shí)現(xiàn)了時(shí)間的自動(dòng)回復(fù)3. 你非常快速地就體驗(yàn)了各種消息接收及發(fā)送,比方倍當(dāng)年自己摸索所用的時(shí)間短了很多4. 你了解了微信公眾平臺(tái)開發(fā)的原理,并且熟悉了各種消息及發(fā)送是怎么一回事5. 你使用方倍工作室的接口,成功的開發(fā)了你的第一個(gè)微信公眾平臺(tái)功能——天氣預(yù)報(bào)。
接下來該做什么呢?你可以學(xué)習(xí)開發(fā)一些基礎(chǔ)的常用功能,推薦:方倍工作室 編寫,機(jī)械工業(yè)出版社 出版的《微信公眾平臺(tái)開發(fā)最佳實(shí)踐》,里面包含很多php開發(fā)技巧、數(shù)據(jù)庫使用、及近30項(xiàng)微信公眾平臺(tái)實(shí)用功能或技術(shù) 。