Php calls the yahoosinaapi weather forecast implementation code

Source: Internet
Author: User
Tags filetime
Php calls the yahoosinaapi weather forecast implementation code

  1. Header ('content-Type: text/html; charset = utf-8 ');
  2. Class weather {
  3. Static $ url = 'http: // xml.weather.yahoo.com/forecastrss? U = c & w = ';
  4. Static $ city = 'Beijing'; // The default city is Beijing. Note that the city must be entered in Pinyin. I have tried Chinese and cannot call it in several regions.
  5. Static $ weatherXML = '';
  6. Static $ woeid_file = "woeid ";
  7. Static $ file_path = "data /";
  8. /**
  9. * Obtain remote xml and cache it locally
  10. */
  11. Static public function getXML ($ city = null ){
  12. If ($ city! = Null ){
  13. Self: $ city = $ city;
  14. }
  15. Self: $ weatherXML = self: $ file_path. md5 (self: $ city). '-weather. XML ';
  16. If (file_exists (self: $ weatherXML )){
  17. $ FileTime = filemtime (self: $ weatherXML );
  18. $ Stater = time ()-$ fileTime-60*60*2;
  19. If ($ stater> 0 ){
  20. Return true;
  21. }
  22. }
  23. // Obtain the woeid
  24. $ Woeid = self: getWOEID ();
  25. Self: $ url = self: $ url. $ woeid [0];
  26. // Obtain the weather of the current day
  27. $ XML = self: vget (self: $ url );
  28. // Save the day's weather to the file
  29. Self: cacheXML ($ XML );
  30. Self: analysisXML ($ XML );
  31. }
  32. Static public function analysisXML ($ simple ){
  33. $ P = xml_parser_create ();
  34. Xml_parse_into_struct ($ p, $ simple, $ vals, $ index );
  35. Xml_parser_free ($ p );
  36. // This week's weather
  37. $ Weekindex = $ index ['yweather: FORECAST '];
  38. $ Week = array ();
  39. Foreach ($ weekindex as $ k => $ v ){
  40. $ Week [$ k] = $ vals [$ v] ['bubuckets'];
  41. }
  42. Unset ($ index );
  43. Unset ($ vals );
  44. Print_r ($ week );
  45. /*
  46. * Day week
  47. * Date
  48. * Low minimum temperature
  49. * High maximum temperature
  50. * Test weather status
  51. * Code weather icon
  52. */
  53. }
  54. /*
  55. * Obtain the region WOEID.
  56. */
  57. Static private function getWOEID (){
  58. Static $ woeid = array ();
  59. If (isset ($ woeid [self: $ city]) {
  60. Return $ woeid [self: $ city];
  61. }
  62. If (file_exists (self: $ file_path. self: $ woeid_file )){
  63. $ WoeidSTR = file_get_contents (self ::$ file_path. self ::$ woeid_file );
  64. $ Woeid = json_decode ($ woeidSTR, true );
  65. If (isset ($ woeid [self: $ city]) {
  66. Return $ woeid [self: $ city];
  67. }
  68. }
  69. $ GeoPlaces = "http://query.yahooapis.com/v1/public/yql? Q = select % 20 woeid % 20 from % 20geo. places % 20 where % 20 text = '". self: $ city." % 20CH '";
  70. $ XML = simplexml_load_file ($ geoPlaces );
  71. If (isset ($ XML-> results-> place [0]) {
  72. $ Rs = $ woeid [self: $ city] = $ XML-> results-> place [0]-> woeid;
  73. // Save to file
  74. $ WoeidSTR = json_encode ($ woeid );
  75. File_put_contents (self: $ file_path. self: $ woeid_file, $ woeidSTR );
  76. Return $ rs;
  77. } Else {
  78. // If the woeid City cannot be found, change it to Beijing by default.
  79. Self: $ city = "Beijing ";
  80. Return self: getWOEID ();
  81. }
  82. }
  83. /**
  84. * Create an xml cache
  85. * @ Param $ contents content to be cached
  86. */
  87. Static private function cacheXML ($ contents ){
  88. $ Contents = str_ireplace (' '," \ N ", $ contents );
  89. $ Contents = mb_convert_encoding ($ contents, 'utf-8', 'gbk ');
  90. File_put_contents (self: $ weatherXML, $ contents) or die ('No write authorization ');
  91. }
  92. /**
  93. * Simulated content retrieval functions
  94. * @ Param type $ url
  95. * @ Return type
  96. */
  97. Static private function vget ($ url ){
  98. $ User_agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2;. net clr 1.1.4322 )";
  99. $ Curl = curl_init (); // Start a CURL session
  100. Curl_setopt ($ curl, CURLOPT_URL, $ url); // address to be accessed
  101. Curl_setopt ($ curl, CURLOPT_SSL_VERIFYPEER, 0); // Check the certificate source
  102. Curl_setopt ($ curl, CURLOPT_SSL_VERIFYHOST, 1); // check whether the SSL encryption algorithm exists from the certificate
  103. Curl_setopt ($ curl, CURLOPT_USERAGENT, $ user_agent); // simulate the browser used by the user
  104. @ Curl_setopt ($ curl, CURLOPT_FOLLOWLOCATION, 1); // use automatic redirect
  105. Curl_setopt ($ curl, CURLOPT_AUTOREFERER, 1); // automatically sets Referer
  106. Curl_setopt ($ curl, CURLOPT_HTTPGET, 1); // send a regular Post request
  107. Curl_setopt ($ curl, CURLOPT_TIMEOUT, 120); // sets the timeout limit to prevent endless loops.
  108. Curl_setopt ($ curl, CURLOPT_HEADER, 0); // display the returned Header content.
  109. Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1); // The obtained information is returned as a file stream.
  110. $ TmpInfo = curl_exec ($ curl); // perform the operation
  111. If (curl_errno ($ curl )){
  112. Curl_close ($ curl); // Close the CURL session
  113. Die ('errno'. curl_error ($ curl ));
  114. }
  115. Curl_close ($ curl); // Close the CURL session
  116. Return $ tmpInfo; // return data
  117. }
  118. }
  119. Weather: getXML ("Changsha ");

2, Sina Weather address: http://php.weather.sina.com.cn code:

  1. Header ('content-Type: text/html; charset = utf-8 ');
  2. Class weather {
  3. Static $ url = 'http: // php.weather.sina.com.cn/xml.php? Password = DJOYnieT8234jlsK & day = 0 & city = '; // The password is a fixed value.
  4. Static $ city = '% B1 % B1 % BE % A9'; // Default city: Beijing
  5. Static $ weatherXML = '';
  6. Static $ file_path = "data /";
  7. /**
  8. * Obtain remote xml and cache it locally
  9. */
  10. Static public function getXML ($ city = null ){
  11. If ($ city! = Null ){
  12. $ City = mb_convert_encoding ($ city, 'gbk', 'utf-8 ');
  13. Self: $ city = urlencode ($ city );
  14. }
  15. Self: $ weatherXML = self: $ file_path. md5 (self: $ city). '-weather. XML ';
  16. If (file_exists (self: $ weatherXML )){
  17. $ FileTime = filemtime (self: $ weatherXML );
  18. $ Stater = time ()-$ fileTime-60*60*2;
  19. If ($ stater> 0 ){
  20. Return true;
  21. }
  22. }
  23. $ Contents = self: vget (self: $ url. self: $ city );
  24. Self: cacheXML ($ contents );
  25. Self: analysisXML ();
  26. }
  27. /**
  28. * Parse xml
  29. */
  30. Static public function analysisXML (){
  31. $ XML = simplexml_load_file (self: $ weatherXML );
  32. Print_r ($ XML );
  33. }
  34. /**
  35. * Create an xml cache
  36. * @ Param $ contents content to be cached
  37. */
  38. Static private function cacheXML ($ contents ){
  39. $ Contents = str_ireplace (' '," \ N ", $ contents );
  40. File_put_contents (self: $ weatherXML, $ contents) or die ('No write authorization ');
  41. }
  42. /**
  43. * Simulated content retrieval functions
  44. * @ Param type $ url
  45. * @ Return type
  46. */
  47. Static private function vget ($ url ){
  48. $ User_agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2;. net clr 1.1.4322 )";
  49. $ Curl = curl_init (); // Start a CURL session
  50. Curl_setopt ($ curl, CURLOPT_URL, $ url); // address to be accessed
  51. Curl_setopt ($ curl, CURLOPT_SSL_VERIFYPEER, 0); // Check the certificate source
  52. Curl_setopt ($ curl, CURLOPT_SSL_VERIFYHOST, 1); // check whether the SSL encryption algorithm exists from the certificate
  53. Curl_setopt ($ curl, CURLOPT_USERAGENT, $ user_agent); // simulate the browser used by the user
  54. @ Curl_setopt ($ curl, CURLOPT_FOLLOWLOCATION, 1); // use automatic redirect
  55. Curl_setopt ($ curl, CURLOPT_AUTOREFERER, 1); // automatically sets Referer
  56. Curl_setopt ($ curl, CURLOPT_HTTPGET, 1); // send a regular Post request
  57. Curl_setopt ($ curl, CURLOPT_TIMEOUT, 120); // sets the timeout limit to prevent endless loops.
  58. Curl_setopt ($ curl, CURLOPT_HEADER, 0); // display the returned Header content.
  59. Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1); // The obtained information is returned as a file stream.
  60. $ TmpInfo = curl_exec ($ curl); // perform the operation
  61. If (curl_errno ($ curl )){
  62. Curl_close ($ curl); // Close the CURL session
  63. Die ('errno'. curl_error ($ curl ));
  64. }
  65. Curl_close ($ curl); // Close the CURL session
  66. Return $ tmpInfo; // return data
  67. }
  68. }
  69. Weather: getXML ();

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.