PHP micro-letter Query City weather, first of all, you need to find a weather API, here, I use Baidu's Apistore, application and use the API URL: http://apistore.baidu.com/apiworks/servicedetail/112.html
Login Baidu account, and the mobile phone to send requests to obtain apikey. With Apikey, you can follow its example to request City weather. (can be queried according to the city's Chinese name, pinyin, city number, etc.)
You can now test locally, after the request is completed, and then put into your own domain name space in the script.
Test Scripts For example: (note apikey to fill in their own application)
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 Apikey to Header curl_setopt ($ch, Curlopt_httpheader, $header);
curl_setopt ($ch, Curlopt_returntransfer, 1);
Execute HTTP request curl_setopt ($ch, Curlopt_url, $url);
$res = curl_exec ($ch);
$res = Json_decode ($res, true);
echo "<pre>";
Print_r ($res);
echo "</pre>";
$CONTENTSTR = ""; foreach ($res as $k => $v) {if ($k = = "Retdata") {$contentStr = "city:". $v [' Cities '].
"\ n"; $contentStr. = "Date:". $v [' Date '].
"\ n"; $contentStr. = "Weather:". $v [' Weather ']. "
\ n "; $contentStr. = "Average temperature:". $v [' temp '].
"℃\n"; $contentStr. = "Minimum temperature:". $v [' l_tmp ']. "
℃\n "; $contentStr. = "Maximum temperature:". $v [' h_tmp '].
"℃\n"; $contentStr. = "Wind:". $v [' WS '].
"\ n"; $contentStr. = "Wind Direction:". $v [' WD '].
"\ n"; $contentStr. = "Sunrise time:". $v [' Sunrise '].
"\ n"; $contentStr. = "Sunset time:". $v [' Sunset '].
"\ n"; $contentStr. = "Longitude:" . $v [' Longitude '].
"\ n"; $contentStr. = "Latitude:".
$v [' latitude '];
} Echo $contentStr;
If you fill out your own apikey, you should be able to obtain the requested weather:
If you can return to normal data, then you can put it into your domain space. (the URL that the developer center fills in the public platform, which has the function of connecting the micro-letter interface, etc.)
If you do not understand the following code or first contact with the micro-credit development, you can refer to my previous article: http://www.jb51.net/article/87252.htm
The following code is part of the responsemsg:
Public Function responsemsg () {<span style= ' white-space:pre ' > </span>//get post data, May is due to the DI Fferent environments $postStr = $GLOBALS ["Http_raw_post_data"]; Receive the micro-mail from the XML data//extract post data if (!empty ($POSTSTR)) {//parse post XML as an object $postobj $POSTOBJ = Simplexml_load_st
Ring ($POSTSTR, ' simplexmlelement ', libxml_nocdata); $fromUsername = $POSTOBJ->fromusername; The user who requested the message $toUsername = $POSTOBJ->tousername; "I" public id $keyword = Trim ($postObj->content); The message content sent by the user $time = time (); Time Stamp $msgtype = ' text '; Message type: Text $TEXTTPL = "<xml> <tousername><! [cdata[%s]]></tousername> <fromusername><! [cdata[%s]]></fromusername> <CreateTime>%s</CreateTime> <msgtype><! [cdata[%s]]></msgtype> <content><!
[cdata[%s]]></content> </xml> "; if ($postObj->msgtype = = ' event ') {//If the message type in the XML message is event if ($postObj->event = = ' Subscribe ') {//If it is a subscriptionPiece $contentStr = "Welcome to subscribe to Misaka last summer!"
\ n More wonderful content: Http://blog.csdn.net/misakaqunianxiatian ";
$RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgtype, $CONTENTSTR);
Echo $resultStr;
Exit (); } $which = Mb_substr ($keyword, 0, 2, ' UTF-8 ');//Get what information to return if ($which = = "Translate") {//If you want to translate//call Youdao translation API for translation}e
Lseif ($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 use the apikey of their own application, filled in after the colon///Add Apikey to Header curl_setopt ($ch, Curlopt_httpheader, $header);
curl_setopt ($ch, Curlopt_returntransfer, 1);
Execute 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 [' Cities '].
"\ n"; $contentStr. = "Date:". $v [' Date '].
"\ n"; $contentStr. = "Weather:". $v [' Weather ']. "
\ n "; $contentStr. = "Average temperature:". $v [' temp '].
"℃\n"; $contentStr. = "Minimum temperature:". $v [' l_tmp ']. "
℃\n "; $contentStr. = "Maximum temperature:". $v [' h_tmp '].
"℃\n"; $contentStr. = "Wind:". $v [' WS '].
"\ n"; $contentStr. = "Wind Direction:". $v [' WD '].
"\ n"; $contentStr. = "Sunrise time:". $v [' Sunrise '].
"\ n"; $contentStr. = "Sunset time:". $v [' Sunset '].
"\ n"; $contentStr. = "Longitude:". $v [' Longitude '].
"\ 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 \ nplease Enter the weather xx can query the city weather";
$RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgtype, $CONTENTSTR);
Echo $resultStr;
Exit ();
}
After the completion (don't forget to fill in the Apikey), your subscription number, enter the weather in Shanghai, then you should be able to find the weather in Shanghai that day.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.