PHP WeChat development Query City weather, PHP query City Weather _php Tutorials

Source: Internet
Author: User
Tags cdata sprintf sunrise time

PHP Development Query City weather, PHP query City weather


PHP Query City Weather, first of all, you need to find a weather-getting API, here, I use Baidu's Apistore, application and use the API URL: http://apistore.baidu.com/apiworks/servicedetail/112.html

Login to Baidu account, and send a request to obtain Apikey mobile phone. With Apikey, you can follow its example to request City weather. (Can be based on the city Chinese name, pinyin, city number, etc. to inquire)

You can do the test locally now, after the request is completed, and then into your own domain namespace script.
Test the script for example: (note apikey fill in the 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 "
";p Rint_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. = "Average temperature:". $v [' temp ']. "℃\n"; $contentStr. = "Lowest 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 in your apikey, you should be able to get the requested weather:

If you can return to the normal data, then you can put it in your domain name space. (The URL of the developer center in the public platform, which has a connection interface and other functions)
If you do not understand the following code or first contact 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, due to the different environments $postStr = $GLOBALS ["Http_raw_post_data"]; Receives the outgoing XML data//extract the post data if (!empty ($POSTSTR)) {//parses the post to XML as an object $postobj $POSTOBJ = simplexml_load_string ($po   Ststr, ' simplexmlelement ', libxml_nocdata); $fromUsername = $POSTOBJ->fromusername; User $toUsername for Request message = $POSTOBJ->tousername; The "I" public number 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 the XML message is event if ($postObj->event = = ' Subscribe ') {//If it is a subscription event $contentStr = "Welcome to subscribe to Misaka last summer!"   \ n More exciting 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 are translating//calling Youdao translation API to translate}elseif ($w  Hich = = "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, fill 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[' City '.   "\ n"; $contentStr. = "Date:". $v [' Date '].   "\ n"; $contentStr. = "Weather:". $v [' Weather ']. "   \ n "; $contentStr. = "Average temperature:". $v [' temp '].   "℃\n"; $contentStr. = "Lowest 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 \ nthe input weather xx can query City weather";  $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgtype, $CONTENTSTR);  Echo $resultStr;  Exit (); }

After completion (don't forget to fill in Apikey), your subscription number, enter the weather in Shanghai, then you should be able to find the weather in Shanghai today.

The above is the whole content of this article, I hope that everyone's learning to help, but also hope that we support a lot of help the home.

http://www.bkjia.com/PHPjc/1136606.html www.bkjia.com true http://www.bkjia.com/PHPjc/1136606.html techarticle PHP Development Query City weather, PHP query City weather PHP City weather, first of all, you need to find a weather API, here, I use Baidu's Apistore, ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.