Originally wanted to get information from China's weather network and processing, and later found it too cumbersome to deal with, and to get all the city code, and then there is! Do not support the weather abroad!! There are a lot of graduates in our school who go to school abroad, so I think I should make a support for the weather abroad.
Therefore consider the direct call to other people's APIs, the first selection of the square times the studio has done the interface (but also no foreign). Until one day return to Beijing The weather temperature is below zero (when it was too hot to go out) ... Change! Change! Change!
Finally found the most reliable interface----Baidu's weather API.
Needless to say, first of all, we should be on the Baidu Map API application for a dedicated key (about a minute is done, very convenient)
The code is as follows:
Graphic Information 2 for weather "This is the micro-letter information template" $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> <item> <title><! [cdata[%s]]></title> <description><! [cdata[%s]]></description> <picurl><! [cdata[%s]]></picurl> <url><!
[cdata[%s]]></url> </item> </Articles> </xml> "//finally found that only five item= = no language ...
PHP Code
if (Substr_count ($keyword, ' weather ')!=0 && $keyword!= ' weather ') {$geshu = Substr_count ($keyword, ' weather ');
$t = Explode ("Weather", $keyword);
For ($i =0 $i <= $geshu $i + +) {if ($t [$i]!= ') {$city = $t [$i];
Break
} $mykey = "" Here is your API 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 ']."
Weather ";
$T [0][' Picurl ']= "http://zhengwairen-try2.stor.sinaapp.com/xytq.jpg";
$T [0][' Url ']= "http://zhengwairen-try2.stor.sinaapp.com/xytq.jpg"; The above two lines can be set to the top of the image you want if (Is_array ($contentStr [' Results '][0][' index ')) {$T [2][' Title ']= '] "PM2.5". $contentStr [' re Sults '][0][' pm25 ']. " \ n "." "". $contentStr [' Results '][0][' index '][0][' title ']. " 】"." (". $contentStr [' Results '][0][' IndeX '][0][' ZS '].
$contentStr [' Results '][0][' index '][0][' des '];
The next line is the car wash index, the feeling is not the theme or not good. $T [2][' title ']= $T [2][' title ']. " \ n "." "". $contentStr [' Results '][0][' index '][1][' title ']. " "(". $contentStr [' Results '][0][' index '][1][' ZS ']. ")
". $contentStr [' Results '][0][' index '][1][' des ']; $T [2][' title ']= $T [2][' title ']. " \ n "." "". $contentStr [' Results '][0][' index '][2][' title ']. " "(". $contentStr [' Results '][0][' index '][2][' ZS ']. ")
". $contentStr [' Results '][0][' index '][2][' des '];
else $guowai = 1;
For ($i =1, $aaa =0 $i <=5; $i + +) {if ($i ==2 && $guowai!=1) continue;
if ($guowai ==1 && $i ==5) continue; $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] [' Picurl ']= $contentStr [' Results '][0][' weather_data '] [$aaa] [' Daypictureurl ']];
$T [$i] [' Url ']= $contentStr [' Results '][0][' weather_data '] [$aaa] [' Daypictureurl '];
$AAA + +; $tianqi = sprintf ($TQTPL, $fromUsername, $toUsername, Time (), "News", $T [0][' Title '], $T [0][' Description '], $T [0][' Pic URL '], $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 '], $T [5][' title '], $T [5][' Description '], $T [5][' Picurl ']
], $T [5][' Url ']);
Echo $tianqi;
}
}
The effect chart is as follows:
You can change the $mykey variable to your own apikey. Another thing that I am puzzled is that I micro-credit graphics and text template is clearly 6 item, why the actual effect of the reply only 5? = = Ask the vast number of netizens to help explain
Finally, attach the code description. Users send "Beijing weather" and "weather Beijing" are all OK, so first do a string processing to get the correct city name. Using the URL given by the Baidu Map API, make a GET request (in fact, direct access) to obtain a JSON-type packet. The packet information is transferred into a group format, corresponding to the reply to the micro-trust users. Foreign weather is not the day of the specific description of the weather, so finally made a little change.