php呼叫yahoo sina api天氣預報的實作程式碼

WBOY
發布: 2016-07-25 08:56:22
原創
1000 人瀏覽過
curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 ); // 以檔案流的形式取得資訊回傳 $tmpInfo = curl_exec ( $curl ); // 執行操作
  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'; //預設城市北京這裡要注意的是city 要填音我試過用中文有好幾個地區都呼叫不到
  5. static $weatherXML = '';
  6. static $woeid_file = "woeid";
  7. static $file_path = "data/";
  8. /**
  9. * 取得遠端xml並快取到本地
  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. //取得woeid
  24. $woeid = self::getWOEID();
  25. self::$url = self::$url . $woeid[0];
  26. //取得當天天氣
  27. $XML = self::vget(self::$url);
  28. //儲存當天天氣到檔案
  29. self::cacheXML($XML);
  30. self::analysisXML($XML);
  31. }
  32. static public function analysisXML($simple) {
  33. $p = xml_parser_create(); xml_parse_into_struct($p, $simple, $vals, $index);
  34. xml_parser_free($p);
  35. //本週天氣
  36. $weekindex = $index['YWEATHER :FORECAST'];
  37. $week = array();
  38. foreach($weekindex as $k=>$v){
  39. $week[$k] = $vals[$v]['attributes '];
  40. }
  41. unset($index);
  42. unset($vals);
  43. print_r($week);
  44. /*
  45. * day 星期
  46. * date 日期
  47. * low 最低溫度
  48. * high 最高溫度
  49. * test 天氣狀態
  50. * code 天氣圖標
  51. */
  52. }
  53. /*
  54. * 取得地區WOEID碼
  55. */
  56. static private function getWOEID(){
  57. static $woeid = array();
  58. if(isset($woeid[self::$city])){
  59. return $woeid[woeid[self::$city])){
  60. return $woeid[fself:: $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%20woeid%20from%20geo.places%20where%20text='". 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. //儲存到檔案
  74. $woeidSTR = json_encode($woeid );
  75. file_put_contents(self::$file_path . self::$woeid_file, $woeidSTR);
  76. return $rs;
  77. }else{
  78. //如果找不到城市woeid 預設城市就改為北京
  79. self::$city = "Beijing";
  80. return self::getWOEID();
  81. }
  82. }
  83. /**
  84. * 建立xml快取
  85. * @param $contents 要快取的內容
  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 ( '沒有寫權限' );
  91. }
  92. /**
  93. * 模擬取得內容函數
  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. .4322)";
  99. $curl = curl_init (); // 啟動一個CURL會話
  100. curl_setopt ( $curl, CURLOPT_URL, $url ); // 要存取的位址
  101. curl_setopt (URL, $url ); // 要存取的位址
  102. curl_setopt ( $curl, CURV , 0 ); // 對認證憑證來源的檢查
  103. curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, 1 ); // 從憑證檢查SSL加密演算法是否存在
  104. curl_setopt ( $curl, CURPT_USERA, $ // 模擬使用者使用的瀏覽器
  105. @curl_setopt ( $curl, CURLOPT_FOLLOWLOCATION, 1 ); // 使用自動跳轉
  106. curl_setopt ( $curl, CURLOPT_AUTOREFERER, 1 ); ( $curl, CURLOPT_HTTPGET, 1 ); // 傳送一個常規的Post請求
  107. curl_setopt ( $curl, CURLOPT_TIMEOUT, 120 ); // 設定超時限制防止死迴圈
  108. curl_setopt ( $PTc, CUR3) ; // 顯示返回的Header區域內容
  109. curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 ); // 獲取的信息以文件流的形式返回
  110. $tmpInfo = curl_exec ( $curl ); // 執行操作
  111. if (curl_errno ( $curl )) {
  112. curl_close ( $curl ); // 關閉CURL會話
  113. die('Errno' . curl_error ( $curl )) ;
  114. }
  115. } return $tmpInfo; // 回傳資料
  116. }
  117. }
  118. weather::getXML("Changsha");
複製程式碼

2,新浪天氣 網址:http://php.weather.sina.com.cn 代碼:

  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=';//密碼是固定值
  4. static $city = ' %B1%B1%BE%A9'; //城市預設北京
  5. static $weatherXML = '';
  6. static $file_path = "data/";
  7. /**
  8. * 取得遠端xml並快取到本地
  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(自我::$城市) 。 '-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. 回傳true;
  21. }
  22. }
  23. $contents = self::vget( self: :$url . self::$city );
  24. self::cacheXML ( $contents );
  25. self::analysisXML();
  26. }
  27. / **
  28. * 解析xml
  29. */
  30. static public function AnalysisXML() {
  31. $XML = simplexml_load_file(self::$weatherXML );
  32. print_r($XML);
  33. }
  34. }
  35. }
  36. }
  37. }
  38. }
  39. }
  40. }
  41. }
  42. }
  43. }
  44. }
  45. }
  46. }
  47. }
  48. }
  49. }
  50. }
  51. }
  52. /**
  53. * 建立xml快取
  54. * @param $contents 要快取的內容
  55. */
  56. 靜態私有函數cacheXML($contents) {
  57. $contents = str_ireplace ( '', " n", $contents );
  58. file_put_contents ( self::$weatherXML, $contents ) 或死( '沒有寫權限' );
  59. }
  60. /**
  61. * 模擬取得內容函數
  62. * @param type $url
  63. * @return type
  64. */
  65. static private function vget($url) {
  66. $user_agent = "Mozilla/4.0 (相容; MSIE) 6.0; Windows NT 5.2; .NET CLR 1.1.4322)"
  67. $curl =curl_init(); // 啟動一個CURL會話
  68. curl_setopt ( $curl, CURLOPT_URL, $url ); // 要存取的位址
  69. curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, 0 );認證證書來源的檢查
  70. curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, 1 ); // 從憑證檢查SSL加密演算法是否存在
curl_setopt ( $curl, CURLOPT_USERAGENT, $user_agent );器
@curl_setopt ( $curl, CURLOPT_FOLLOWLOCATION, 1 ); // 使用自動跳轉 curl_setopt ( $curl, CURLOPT_AUTOREFERER, 1 ); // 自動設定Referer curl_setopt ( $curl, CURLOPT_TIMEOUT, 120 ); // 設定超時限制防止死循環
curl_setopt ( $curl, CURLOPT_HEADER, 0 );頭區域內容
if (curl_no (if curl )) { curl_close ( $curl ); // 關閉CURL會話
die('Errno' .curl_error ( $curl )) ;
}
curl_close ( $curl )) ;
}
curl_close ( $curl ); // 關閉CURL會話
return $tmpInfo; // 回傳資料 }}weather::getXML();複製程式碼複製程式碼複製程式碼
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!