In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the Wechat development of how to quickly query the city weather, the article is very detailed, has a certain reference value, interested friends must read it!
Wechat http://www.php.cn/php/php-tp-demand.html" target= "_ blank" > query city weather, first of all, you need to find an API to get the weather, here, using Baidu apistore, you can go to Baidu to apply and use API.
Log in to your Baidu account and use your mobile phone to send a request to get apikey. With apikey, you can follow its example to request city weather. (you can inquire according to the Chinese name of the city, pinyin, city number, etc.)
You can do the test locally now, and then put it in your own domain name space script after the request is completed.
The script for the test is for example: (note that apikey filled in his 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 headercurl_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 ""; 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 ['weather']. "\ n"; $contentStr. = "mean temperature:". $v ['temp']. "℃\ n"; $contentStr. = "minimum temperature:". "℃\ n"; $contentStr. = "maximum temperature:". $v ['hitchtmp']. "℃\ 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 in your own apikey, you should be able to get the requested weather:
If you can return the normal data, you can put it in your domain name space. (the url entered by the developer Center in the public platform, which has functions such as connecting to Wechat interface)
Public function responseMsg () {/ / get post data, May be due to the different environments $postStr = $GLOBALS ["HTTP_RAW_POST_DATA"]; / / receive XML data from Wechat / / extract post data if (! empty ($postStr)) {/ / parse the XML from post as an object $postObj $postObj = simplexml_load_string ($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj- > FromUserName; / / user requesting the message $toUsername = $postObj- > ToUserName / / official account of "I" id $keyword = trim ($postObj- > Content); / / message content sent by the user $time = time (); / / timestamp $msgtype = 'text'; / / message type: text $textTpl = "% s" If ($postObj- > MsgType = = 'event') {/ / if the message type in the XML message is event if ($postObj- > Event = =' subscribe') {/ / if it is a subscription event $contentStr = "Welcome to subscribe misaka last summer! \ nMore 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') / / get what information to return if ($which== "translation") {/ / if you want to translate / / call youdao translation API to translate} 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 apikey you applied for, fill in the colon after / / 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 ['city']. "\ n"; $contentStr. = "date:" $v ['date']. "\ n"; $contentStr. = "Weather:" V ['weather']. "\ n"; $contentStr. = "mean temperature:". $v ['temp']. "℃\ n"; $contentStr. = "minimum temperature:". "℃\ n"; $contentStr. = "maximum temperature:". $v ['hitchtmp']. "℃\ 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\ nenter weather XX to query city weather"; $resultStr = sprintf ($textTpl, $fromUsername, $toUsername, $time, $msgtype, $contentStr); echo $resultStr; exit ();
When you are finished (don't forget to fill in apikey), enter the weather in Shanghai in your Subscription account, and you should be able to find out the weather in Shanghai that day.
The above is all the contents of the article "how to quickly query the City Weather in the Development of Wechat". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.