微信公众号用百度车联网API回复天气预报

1.申请百度地图开发平台访问应用(AK);
微信公众号用百度车联网API回复天气预报_第1张图片
点创建应用
微信公众号用百度车联网API回复天气预报_第2张图片
然后提交,AK就申请好了;

2.然后我们看下百度的车联网API中的天气查询,这是链接车联网API
这是API上面的接口实例http://api.map.baidu.com/telematics/v3/weather?location=北京&output=json&ak=E4805d16520de693a3fe707cdc962045
点看链接我们可以看到返回结果;返回结果可以是xml,也可以是json,
默认的是xml,这里返回的是json格式;
【提示:在代码中城市名是需要urlencode(城市名)转换的
这里 北京 =>%E5%8C%97%E4%BA%AC 】

3.然后我们可以编写代码了0.0

回复文本的消息

<xml>
<ToUserName>ToUserName>
<FromUserName>FromUserName>
<CreateTime>12345678CreateTime>
<MsgType>MsgType>
<Content>Content>
xml>

回复图文消息

<xml>
<ToUserName>ToUserName>
<FromUserName>FromUserName>
<CreateTime>12345678CreateTime>
<MsgType>MsgType>
<ArticleCount>2ArticleCount>
<Articles>
<item>
<Title>Title> 
<Description>Description>
<PicUrl>PicUrl>
<Url>Url>
item>
<item>
<Title>Title>
<Description>Description>
<PicUrl>PicUrl>
<Url>Url>
item>
Articles>
xml>

然后贴出我的代码


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()){
            header('content-type:text');
            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)){
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);

            $city = trim($postObj->Content);
            $report_arr = $this->receiveWeather($city);
            if($report_arr['status'] != "success"){
                $content = "请输入正确的城市名";
                $resultStr = $this->transmitText($postObj,$content);
            }
            else{
                $itemTpl = "
                            <![CDATA[【%s】%s %s %s %s]]> 
                            
                            
                            
                        ";

                $item_str = "";
                foreach ($report_arr['weather_data'] as $item)
                $item_str .= sprintf($itemTpl, $city, $item['date'], $item['weather'], $item['wind'], $item['temperature'], $item['dayPictureUrl']);
                $textTpl = "
                             
                            
                            %s
                            
                            %s
                            
                                $item_str
                            
                            ";
                $resultStr = sprintf($textTpl, $postObj->FromUserName, $postObj->ToUserName, time(), count($report_arr['weather_data']));
            }

            echo $resultStr;
        }
    }
    private function receiveWeather($cityName){
        $ak = "输入你自己申请的AK";
        $url = "http://api.map.baidu.com/telematics/v3/weather?location=".urlencode($cityName)."&output=json&ak=$ak";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);    // 设置你需要抓取的URL
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    // 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。
        $output = curl_exec($ch);   // 运行cURL,请求网页
        curl_close($ch);
        $result = json_decode($output, true); 
        $weather_report = array('weather_data' => $result['results'][0]['weather_data'], 
            'index' => $result['results'][0]['index'], 'status' => $result['status']);
        return $weather_report;
    }
    //发送文本信息
    private function transmitText($object,$content){
        if (!isset($content) || empty($content)){
            return "";
        }
        $xmlTpl = "
                       
                       
                       %s
                       
                       
                   ";
        $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), $content);

        return $result;
    }
}
?>

你可能感兴趣的:(微信公众号用百度车联网API回复天气预报)