微信公众平台消息接口开发(2)
生活随笔
收集整理的這篇文章主要介紹了
微信公众平台消息接口开发(2)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
轉自:http://www.chenwg.com/%E4%BA%92%E8%81%94%E7%BD%91/%E5%BE%AE%E4%BF%A1%E5%85%AC%E4%BC%97%E5%B9%B3%E5%8F%B0%E6%B6%88%E6%81%AF%E6%8E%A5%E5%8F%A3%E5%BC%80%E5%8F%91%EF%BC%882%EF%BC%89.html
消息推送,也就是用戶向公眾賬號發送的消息的類型,目前支持的有文本、圖片、地理位置、鏈接、事件消息等五種,公眾賬號的回復消息有三種,文本、音樂、圖文。
我開發兩個公眾平臺的應用,一個是天氣寶寶,一個是翻譯寶寶,其中天氣寶寶返回的是圖文信息、翻譯寶寶返回的是文字信息,截圖如下:
?
接下來將以這兩個應用為例講解公眾平臺應用的開發。
翻譯寶寶使用的是有道翻譯的api,但是有道翻譯的api是有使用限制的,每個小時不超過1000次,對于大部分人來說,這已經夠了,申請的地址:http://fanyi.youdao.com/openapi?path=data-mode?,申請完后就有API Key了。
下面是翻譯寶寶第一版本的源碼:
| 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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | <!--?php /**?? * wechat php test?? */ // define your token define("TOKEN", "weixin"); $wechatObj = new wechatCallbackapiTest(); $wechatObj--->responseMsg(); class wechatCallbackapiTest { ????public function valid() ????{ ????????$echoStr = $_GET["echostr"]; ????????//valid signature , option ????????if($this->checkSignature()){ ????????????echo $echoStr; ????????????exit; ????????} ????} ????public function responseMsg() ????{ ????????//get post data, May be due to the different environments ????????$postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; ????????//extract 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(!empty( $keyword )) ????????????????{ ????????????????????$msgType = "text"; ????????????????????$contentStr = $keyword; ????????????????????// 判斷是否首次關注 ????????????????????if ( $keyword == "Hello2BizUser" ) { ????????????????????????$contentStr = "歡迎關注翻譯寶寶,請輸入你要翻譯的單詞或句子!"; ????????????????????}else { ????????????????????????$contentStr =YouDaoTranslate($keyword); ????????????????????} ????????????????????$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); ????????????????????echo $resultStr; ????????????????}else{ ????????????????????echo "Input something..."; ????????????????} ????????}else { ????????????echo ""; ????????????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; ????????} ????} } /** ?* ?* http://fanyi.youdao.com/openapi.do?keyfrom=&key=&type=data&doctype=json&version=1.1&q=翻譯 { ????"errorCode":0 ????"query":"翻譯", ????"translation":["translation"], // 有道翻譯 ????"basic":{ // 有道詞典-基本詞典 ????"phonetic":"fān yì", ????"explains":[ ????"translate", ????"interpret" ????????????] }, "web":[ // 有道詞典-網絡釋義 { ????"key":"翻譯", ????"value":["translator","translation","translate","Interpreter"] }, {...} ] } ?*/ function YouDaoTranslate($keyword) { ????????$url="http://fanyi.youdao.com/openapi.do?keyfrom=*****&key=*******&type=data&doctype=json&version=1.1&q=".urlencode($keyword); ????????//初始化一個cURL對象 ????????$curl=curl_init(); ????????//設置要抓取的URL ????????curl_setopt($curl,CURLOPT_URL,$url); ????????//設置cURL參數,要求結果保存到字符串中還是輸出到屏幕上 ????????curl_setopt($curl,CURLOPT_RETURNTRANSFER,1); ????????//運行cURL,請求網頁 ????????$output=curl_exec($curl); ????????//解析返回的結果,設置true是在php中將其轉為數組 ????????$youdaoResult=json_decode($output,true); ????????$errorcode=$youdaoResult['errorCode']; ????????foreach ($explains as $value){ ????????????$translation=$translation."\n".$value; ????????} ????????return $youdaoResult['translation'][0]; ????} ?> |
轉載于:https://www.cnblogs.com/hxxy2003/archive/2013/03/19/2968730.html
總結
以上是生活随笔為你收集整理的微信公众平台消息接口开发(2)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 简历要避开哪些坑,资深面试官告诉你!
- 下一篇: 什么是GAC?