微信開發學習:輸入城市+天氣接收天氣預報,非非非非常好玩
阿新 • • 發佈:2019-01-08
這次實踐一下這篇 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℃" } ] } ]