微信公众平台消息接口开发(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

消息推送,也就是用户向公众账号发送的消息的类型,目前支持的有文本、图片、地理位置、链接、事件消息等五种,公众账号的回复消息有三种,文本、音乐、图文。

我开发两个公众平台的应用,一个是天气宝宝,一个是翻译宝宝,其中天气宝宝返回的是图文信息、翻译宝宝返回的是文字信息,截图如下:

豌豆荚截图20130319003340 豌豆荚截图20130319003400

接下来将以这两个应用为例讲解公众平台应用的开发。

翻译宝宝使用的是有道翻译的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;
        }
    }
}
/**
 *
{
    "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];
    }
?>
 
posted @ 2013-03-19 11:01  寒星晓月 专注移动互联网  阅读(1248)  评论(0编辑  收藏  举报