生活随笔
收集整理的這篇文章主要介紹了
微信公众平台开发接口PHP SDK完整版
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
官方提供的SDK只有一個文本消息功能,我們將所有消息的消息類型及事件響應都整理了進來,并且加入日志記錄,代碼如下:
?
更新日志:
2013-01-01 版本1.0
2014-03-15 增加圖片、視頻、語音的內容回復
2014-04-09 增加菜單鏈接事件
2014-04-10 修改文本回復的判定方法
<?
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;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 = "unknow 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'])){
//SAEsae_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);}}
}?>
?
轉載于:https://www.cnblogs.com/joshua317/articles/3713376.html
總結
以上是生活随笔為你收集整理的微信公众平台开发接口PHP SDK完整版的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。