微信开发学习:输入城市+天气接收天气预报,非非非非常好玩


    这次实践一下这篇 http://www.jb51.net/article/51923.htm

    输入城市+天气四个字,接收天气预报,数据来源是百度天气预报接口。原来的代码功能多,比较复杂,我精简了一下,相对容易看懂。

完成后的效果:




wx_token_read.php文件:

<?php
//装载模板文件
include_once("wx_tq_tpl.php");

//获取微信发送数据
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

  //返回回复数据
if (!empty($postStr)) {

    //解析数据
    $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
    //发送消息方ID
    $fromUsername = $postObj->FromUserName;
    //接收消息方ID
    $toUsername = $postObj->ToUserName;
    //消息类型
    $form_MsgType = $postObj->MsgType;

    //事件消息
    if ($form_MsgType == "text") {
        //获取用户发送的文字内容
        $form_Content = trim($postObj->Content);

        if (!empty($form_Content)) {
            $city = "";
            if (substr_count($form_Content, '天气') != 0 && $form_Content != '天气') {
                $geshu = substr_count($form_Content, '天气');
                $t = explode("天气", $form_Content);
                for ($i = 0; $i <= $geshu; $i++) {
                    if ($t[$i] != '') {
                        $city = $t[$i];
                    }
                    break;
                }
            }

            if ($city == "") {
                $resultStr = "请输入 城市+天气";
            } else {
                $mykey = "**************";  // 输入自己的百度key
                $url = "http://api.map.baidu.com/telematics/v3/weather?location=" . $city . "&output=json&ak=" . $mykey;
                $output = file_get_contents($url);
                $contentStr = json_decode($output, true);

                if ($contentStr['status'] == 'success') {
                    $T[0]['Title'] = $contentStr['date'] . " " . $contentStr['results'][0]['currentCity'] . "天气";
                    $T[0]['Description'] = $contentStr['results'][0]['weather_data'][0]['temperature']." ".$contentStr['results'][0]['weather_data'][0]['weather'] ." " . $contentStr['results'][0]['weather_data'][0]['wind'];
                    $T[0]['PicUrl'] = $contentStr['results'][0]['weather_data'][0]['dayPictureUrl'];
                    $T[0]['Url'] = $contentStr['results'][0]['weather_data'][0]['dayPictureUrl'];

                    for ($i = 1, $aaa = 0; $i <= 4; $i++) {

                        $T[$i]['Title'] = $contentStr['results'][0]['weather_data'][$aaa]['date'] . " " . $contentStr['results'][0]['weather_data'][$aaa]['temperature'] . " " . $contentStr['results'][0]['weather_data'][$aaa]['weather'] . " " . $contentStr['results'][0]['weather_data'][$aaa]['wind'];
                        $T[$i]['Description'] = "";
                        $T[$i]['PicUrl'] = $contentStr['results'][0]['weather_data'][$aaa]['dayPictureUrl'];
                        $T[$i]['Url'] = $contentStr['results'][0]['weather_data'][$aaa]['dayPictureUrl'];
                        $aaa++;
                    }

                    $resultStr = sprintf($tqTpl,
                        $fromUsername,
                        $toUsername,
                        time(),
                        "news",
                        $T[0]['Title'],
                        $T[0]['Description'],
                        $T[0]['PicUrl'],
                        $T[0]['Url'],

                        $T[1]['Title'],
                        $T[1]['Description'],
                        $T[1]['PicUrl'],
                        $T[1]['Url'],

                        $T[2]['Title'],
                        $T[2]['Description'],
                        $T[2]['PicUrl'],
                        $T[2]['Url'],

                        $T[3]['Title'],
                        $T[3]['Description'],
                        $T[3]['PicUrl'],
                        $T[3]['Url'],

                        $T[4]['Title'],
                        $T[4]['Description'],
                        $T[4]['PicUrl'],
                        $T[4]['Url']);
                }
            }

            echo $resultStr;
            exit;

        }
    } elseif ($form_MsgType == "event")// 接收到事件
    {
        //获取事件类型
        $form_Event = $postObj->Event;
        //订阅事件
        if ($form_Event == "subscribe") // 接收到的事件为:关注
        {
            //回复欢迎文字消息
            $return_str = "欢迎使用天气预报aaa\n";
            $msgType = "text";
            $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, $return_str);
            echo $resultStr;
            exit;
        }

    }

}

?>

xml模板文件 wx_tq_tpl.php:

<?php
$tqTpl = "<xml>
            <ToUserName><![CDATA[%s]]></ToUserName>
            <FromUserName><![CDATA[%s]]></FromUserName>
            <CreateTime>%s</CreateTime>
            <MsgType><![CDATA[%s]]></MsgType>
            <ArticleCount>5</ArticleCount>
            <Articles>
            <item>
            <Title><![CDATA[%s]]></Title>
            <Description><![CDATA[%s]]></Description>
            <PicUrl><![CDATA[%s]]></PicUrl>
            <Url><![CDATA[%s]]></Url>
            </item>
            <item>
            <Title><![CDATA[%s]]></Title>
            <Description><![CDATA[%s]]></Description>
            <PicUrl><![CDATA[%s]]></PicUrl>
            <Url><![CDATA[%s]]></Url>
            </item>
            <item>
            <Title><![CDATA[%s]]></Title>
            <Description><![CDATA[%s]]></Description>
            <PicUrl><![CDATA[%s]]></PicUrl>
            <Url><![CDATA[%s]]></Url>
            </item>
            <item>
            <Title><![CDATA[%s]]></Title>
            <Description><![CDATA[%s]]></Description>
            <PicUrl><![CDATA[%s]]></PicUrl>
            <Url><![CDATA[%s]]></Url>
            </item>
            <item>
            <Title><![CDATA[%s]]></Title>
            <Description><![CDATA[%s]]></Description>
            <PicUrl><![CDATA[%s]]></PicUrl>
            <Url><![CDATA[%s]]></Url>
            </item>
            </Articles>
            </xml> ";

?>

接收到的百度天气预报数据,修改代码时用到:

    "error": 0,
    "status": "success",
    "date": "2016-11-15",
    "results": [
        {
            "currentCity": "北京",
            "pm25": "81",
            "index": [
                {
                    "title": "穿衣",
                    "zs": "较冷",
                    "tipt": "穿衣指数",
                    "des": "建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。"
                },
                {
                    "title": "洗车",
                    "zs": "较适宜",
                    "tipt": "洗车指数",
                    "des": "较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。"
                },
                {
                    "title": "旅游",
                    "zs": "适宜",
                    "tipt": "旅游指数",
                    "des": "天气较好,温度适宜,是个好天气哦。这样的天气适宜旅游,您可以尽情地享受大自然的风光。"
                },
                {
                    "title": "感冒",
                    "zs": "较易发",
                    "tipt": "感冒指数",
                    "des": "昼夜温差较大,较易发生感冒,请适当增减衣服。体质较弱的朋友请注意防护。"
                },
                {
                    "title": "运动",
                    "zs": "较不宜",
                    "tipt": "运动指数",
                    "des": "天气较好,但考虑天气寒冷,推荐您进行各种室内运动,若在户外运动请注意保暖并做好准备活动。"
                },
                {
                    "title": "紫外线强度",
                    "zs": "中等",
                    "tipt": "紫外线强度指数",
                    "des": "属中等强度紫外线辐射天气,外出时建议涂擦SPF高于15、PA+的防晒护肤品,戴帽子、太阳镜。"
                }
            ],
            "weather_data": [
                {
                    "date": "周二 11月15日 (实时:6℃)",
                    "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/qing.png",
                    "nightPictureUrl": "http://api.map.baidu.com/images/weather/night/qing.png",
                    "weather": "晴",
                    "wind": "微风",
                    "temperature": "11 ~ 0℃"
                },
                {
                    "date": "周三",
                    "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/mai.png",
                    "nightPictureUrl": "http://api.map.baidu.com/images/weather/night/mai.png",
                    "weather": "霾",
                    "wind": "微风",
                    "temperature": "11 ~ 2℃"
                },
                {
                    "date": "周四",
                    "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/mai.png",
                    "nightPictureUrl": "http://api.map.baidu.com/images/weather/night/mai.png",
                    "weather": "霾",
                    "wind": "微风",
                    "temperature": "8 ~ 5℃"
                },
                {
                    "date": "周五",
                    "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/mai.png",
                    "nightPictureUrl": "http://api.map.baidu.com/images/weather/night/mai.png",
                    "weather": "霾",
                    "wind": "微风",
                    "temperature": "9 ~ 6℃"
                }
            ]
        }
    ]





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值