Heim  >  Artikel  >  Backend-Entwicklung  >  php调用yahoo sina api天气预报的实现代码

php调用yahoo sina api天气预报的实现代码

WBOY
WBOYOriginal
2016-07-25 08:56:22968Durchsuche
  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();
  34. xml_parse_into_struct($p, $simple, $vals, $index);
  35. xml_parser_free($p);
  36. //本周天气
  37. $weekindex = $index['YWEATHER:FORECAST'];
  38. $week = array();
  39. foreach($weekindex as $k=>$v){
  40. $week[$k] = $vals[$v]['attributes'];
  41. }
  42. unset($index);
  43. unset($vals);
  44. print_r($week);
  45. /*
  46. * day 星期
  47. * date 日期
  48. * low 最低温度
  49. * high 最高温度
  50. * test 天气状态
  51. * code 天气图标
  52. */
  53. }
  54. /*
  55. * 取得地区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%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.1.4322)";
  99. $curl = curl_init (); // 启动一个CURL会话
  100. curl_setopt ( $curl, CURLOPT_URL, $url ); // 要访问的地址
  101. curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, 0 ); // 对认证证书来源的检查
  102. curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, 1 ); // 从证书中检查SSL加密算法是否存在
  103. curl_setopt ( $curl, CURLOPT_USERAGENT, $user_agent ); // 模拟用户使用的浏览器
  104. @curl_setopt ( $curl, CURLOPT_FOLLOWLOCATION, 1 ); // 使用自动跳转
  105. curl_setopt ( $curl, CURLOPT_AUTOREFERER, 1 ); // 自动设置Referer
  106. curl_setopt ( $curl, CURLOPT_HTTPGET, 1 ); // 发送一个常规的Post请求
  107. curl_setopt ( $curl, CURLOPT_TIMEOUT, 120 ); // 设置超时限制防止死循环
  108. curl_setopt ( $curl, CURLOPT_HEADER, 0 ); // 显示返回的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. curl_close ( $curl ); // 关闭CURL会话
  116. return $tmpInfo; // 返回数据
  117. }
  118. }
  119. 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=';//password是固定值
  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(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. * 解析xml
  29. */
  30. static public function analysisXML() {
  31. $XML = simplexml_load_file(self::$weatherXML );
  32. print_r($XML);
  33. }
  34. /**
  35. * 创建xml缓存
  36. * @param $contents 要缓存的内容
  37. */
  38. static private function cacheXML($contents) {
  39. $contents = str_ireplace ( '', " \n", $contents );
  40. file_put_contents ( self::$weatherXML, $contents ) or die ( '没有写权限' );
  41. }
  42. /**
  43. * 模拟获取内容函数
  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 (); // 启动一个CURL会话
  50. curl_setopt ( $curl, CURLOPT_URL, $url ); // 要访问的地址
  51. curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, 0 ); // 对认证证书来源的检查
  52. curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, 1 ); // 从证书中检查SSL加密算法是否存在
  53. curl_setopt ( $curl, CURLOPT_USERAGENT, $user_agent ); // 模拟用户使用的浏览器
  54. @curl_setopt ( $curl, CURLOPT_FOLLOWLOCATION, 1 ); // 使用自动跳转
  55. curl_setopt ( $curl, CURLOPT_AUTOREFERER, 1 ); // 自动设置Referer
  56. curl_setopt ( $curl, CURLOPT_HTTPGET, 1 ); // 发送一个常规的Post请求
  57. curl_setopt ( $curl, CURLOPT_TIMEOUT, 120 ); // 设置超时限制防止死循环
  58. curl_setopt ( $curl, CURLOPT_HEADER, 0 ); // 显示返回的Header区域内容
  59. curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 ); // 获取的信息以文件流的形式返回
  60. $tmpInfo = curl_exec ( $curl ); // 执行操作
  61. if (curl_errno ( $curl )) {
  62. curl_close ( $curl ); // 关闭CURL会话
  63. die('Errno' . curl_error ( $curl )) ;
  64. }
  65. curl_close ( $curl ); // 关闭CURL会话
  66. return $tmpInfo; // 返回数据
  67. }
  68. }
  69. weather::getXML();
复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn