Query city weather in PHP development, and query city weather in php. PHP development query of city weather, php query of city weather PHP query of city weather, first of all, you need to find an API to get the weather, here, I use Baidu's apistore, query city weather in PHP development, and query city weather in php
PHP query city weather, first of all, you need to find an API to get the weather, here, I am using Baidu's apistore, apply for and use the API URL: http://apistore.baidu.com/apiworks/servicedetail/112.html
Log on to the Baidu account and send a mobile phone request to obtain the apikey. With apikey, you can request city weather according to its example. (Query by Chinese city name, pinyin, and City number)
Now you can perform a local test. after the request is complete, put it in your domain name space script.
The test script is as follows: (note that you have entered your own application for apikey)
Header ('content-type: text/html; charset = UTF-8 '); $ ch = curl_init (); $ url = 'http: // apis.baidu.com/apistore/weatherservice/cityname? Cityname = Shanghai'; $ header = array ('apikey: ', // your apikey); // add the apikey to headercurl_setopt ($ ch, CURLOPT_HTTPHEADER, $ header ); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // execute the HTTP request curl_setopt ($ ch, CURLOPT_URL, $ url); $ res = curl_exec ($ ch ); $ res = json_decode ($ res, true); echo"";print_r($res);echo "
"; $ ContentStr =" "; foreach ($ res as $ k = >$ v) {if ($ k =" retData ") {$ contentStr =" city :". $ v ['city']. "\ n"; $ contentStr. = "date :". $ v ['Date']. "\ n"; $ contentStr. = "weather :". $ v ['weate']. "\ n"; $ contentStr. = "average temperature :". $ v ['temp ']. "℃ \ n"; $ contentStr. = "minimum temperature :". $ v ['l _ tmp ']. "℃ \ n"; $ contentStr. = "Maximum temperature :". $ v ['H _ tmp ']. "℃ \ n"; $ contentStr. = "Wind power :". $ v ['Ws ']. "\ n"; $ contentStr. = "wind direction :". $ v ['wd ']. "\ n"; $ contentStr. = "sunrise time :". $ v ['Sunrise ']. "\ n"; $ contentStr. = "sunset time :". $ v ['Sunset']. "\ n"; $ contentStr. = "Longitude :". $ v ['longyun']. "\ n"; $ contentStr. = "latitude :". $ v ['latitude '] ;}} echo $ contentStr;
If you enter your own apikey, you should be able to get the requested weather:
If you can return normal data, you can put it in your domain name space. (The url entered by the developer center on the public platform, which has functions such as connection interfaces)
If you do not understand the following code or the first contact with development, you can refer to my previous article: http://www.bkjia.com/article/87252.htm
The following code is part of responseMsg:
Public function responseMsg () {// get post data, May be due to the different environments $ postStr = $ GLOBALS ["HTTP_RAW_POST_DATA"]; // receive XML data // extract post data if (! Empty ($ postStr) {// Parse the XML from post as an object $ postObj = simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA ); $ fromUsername = $ postObj-> FromUserName; // The user requesting the message $ toUsername = $ postObj-> ToUserName; // "my" public id $ keyword = trim ($ postObj-> Content); // the message Content sent by the user $ time = time (); // timestamp $ msgtype = 'text'; // Message Type: text $ textTpl ="
%s
%s
% S
%s
%s
"; If ($ postObj-> MsgType = 'event') {// if the message type in XML is event if ($ postObj-> event = 'subscribe ') {// if it is a subscription event $ contentStr = "Welcome to subscribe to misaka last summer! \ N more highlights: http://blog.csdn.net/misakaqunianxiatian "; $ ResultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgtype, $ contentStr); echo $ resultStr; exit ();}} $ which = mb_substr ($ keyword, 0, 2, 'utf-8'); // obtain the information to be returned if ($ which = "translation ") {// If you want to translate, // call the Youdao translation API for translation} elseif ($ which = "weather") {$ wea = $ which; $ city = str_replace ($ wea, "", $ keyword); $ ch = curl_init (); $ url =' http://apis.baidu.com/apistore/weatherservice/cityname?cityname= '. $ City; $ header = array ('apikey: '); // The apikey here uses the applied apikey, and enter it after the colon. // add the apikey to the header curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ header); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // execute the HTTP request curl_setopt ($ ch, CURLOPT_URL, $ url ); $ res = curl_exec ($ ch); $ res = json_decode ($ res, true); $ contentStr = ""; foreach ($ res as $ k => $ v) {if ($ k = "retData") {$ contentStr = "city :". $ v ['city']. "\ n"; $ contentStr. = "date :". $ v ['Date']. "\ n"; $ contentStr. = "weather :". $ v ['weate']. "\ n"; $ contentStr. = "average temperature :". $ v ['temp ']. "℃ \ n"; $ contentStr. = "minimum temperature :". $ v ['l _ tmp ']. "℃ \ n"; $ contentStr. = "Maximum temperature :". $ v ['H _ tmp ']. "℃ \ n"; $ contentStr. = "Wind power :". $ v ['Ws ']. "\ n"; $ contentStr. = "wind direction :". $ v ['wd ']. "\ n"; $ contentStr. = "sunrise time :". $ v ['Sunrise ']. "\ n"; $ contentStr. = "sunset time :". $ v ['Sunset']. "\ n"; $ contentStr. = "Longitude :". $ v ['longyun']. "\ n"; $ contentStr. = "latitude :". $ v ['latitude '] ;}}$ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgtype, $ contentStr); echo $ resultStr; exit ();} else {$ contentStr = "input translation XXX can be translated (= ~ω ← =) \ n input weather XX to query city weather "; $ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgtype, $ contentStr); echo $ resultStr; exit ();}
After that (do not forget to fill in apikey), enter the weather Shanghai in your subscription number, so you should be able to check the weather in Shanghai on that day.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.
Descriphp queries City Weather. First, you need to find an API to get weather information. here, I use Baidu's apistore ,...