yahoo sina API天気予報を呼び出すPHPの実装コード

WBOY
リリース: 2016-07-25 08:56:22
オリジナル
956 人が閲覧しました
  1. header ( 'Content-Type: text/html; charset = utf-8' );
  2. クラス天気 {
  3. static $url = 'http://xml.weather.yahoo.com /forecastrss?u=c&w=';
  4. static $city = 'Beijing'; //ここで注意すべき点は、city にはピンインが必要であることですが、いくつかの地域では呼び出すことができません。 static $weatherXML = '';
  5. static $woeid_file = "woeid";
  6. static $file_path = "data/";
  7. /**
  8. * リモート XML を取得してローカルにキャッシュします
  9. */
  10. static public function getXML($city = null) {
  11. if ($city != null){
  12. self::$city = $city;
  13. }
  14. self::$weatherXML = self::$file_path . md5(self::$city) ;
  15. if (file_exists ( self::$weatherXML )) {
  16. $fileTime = filemtime ( self::$weatherXML );
  17. $stater = time () - $fileTime - 60 * 60 * 2;
  18. if ($stater > 0) {
  19. return true
  20. }
  21. }
  22. //woeid = self::getWOEID();
  23. self::$url = self::$url .その日の天気
  24. $ XML = self::vget(self::$url);
  25. //その日の天気をファイルに保存します
  26. self::cacheXML($XML);
  27. self::analysisXML($XML) );
  28. }
  29. 静的パブリック関数分析XML($simple) {
  30. $p = xml_parser_create();
  31. xml_parse_into_struct($p, $simple, $vals, $index);
  32. xml_parser_free($p);
  33. //今週の天気
  34. $weekindex = $index['YWEATHER:FORECAST'];
  35. $week = array();
  36. foreach($weekindex as $k=>$v){
  37. $week[$k] = $vals[$v][' 属性'];
  38. }
  39. unset($index);
  40. unset($vals);
  41. print_r($week);
  42. /*
  43. * 曜日 週
  44. * date date
  45. * 最低気温が低い
  46. * 最高気温が高い
  47. *天気ステータスをテスト
  48. * コード天気アイコン
  49. */
  50. }
  51. /*
  52. * エリア WOEID コードを取得
  53. */
  54. static private function getWOEID(){
  55. static $woeid = array();
  56. if(isset($ woeid[self::$city])) {
  57. return $woeid[self::$city];
  58. }
  59. if (file_exists( self::$file_path . self::$woeid_file )) {
  60. $woeidSTR = file_get_contents (self::$file_path . self:: $woeid_file);
  61. $woeid = json_decode($woeidSTR , true);
  62. if(isset($woeid[self::$city])){
  63. return $woeid[self: :$city];
  64. }
  65. }
  66. $geoPlaces = "http://query.yahooapis.com/v1/public/yql?q=select%20woeid%20from%20geo.places%20where%20text='".self ::$city."%20CH" ;
  67. $XML = simplexml_load_file( $geoPlaces );
  68. if(isset($XML->results->place[0])){
  69. $rs = $woeid [self::$city] = $XML ->results->place[0]->woeid;
  70. //ファイルに保存
  71. $woeidSTR = json_encode($woeid);
  72. file_put_contents(self::$file_path . self::$woeid_file, $woeidSTR) ;
  73. return $rs;
  74. }else{
  75. //都市 woeid が見つからない場合、デフォルトの都市は北京に変更されます
  76. self::$city = "Beijing";
  77. return self::getWOEID();
  78. }
  79. }
  80. /**
  81. * XMLキャッシュを作成します
  82. * @param $contents キャッシュされるコンテンツ
  83. */
  84. 静的プライベート関数cacheXML($contents) {
  85. $contents = str_ireplace ( '', " n", $contents );
  86. $contents = mb_convert_encoding ( $contents, 'utf-8', 'gbk' );
  87. file_put_contents ( self:: $weatherXML, $contents ) or die ( '書き込み権限なし' );
  88. }
  89. /**
  90. * コンテンツを取得する関数をシミュレートします
  91. * @param type $url
  92. * @return type
  93. */
  94. static private function vget($url) {
  95. $user_agent = "Mozilla/4.0 (互換性; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)";
  96. $curl =curl_init (); // CURL セッションを開始します
  97. curl_setopt ( $curl, CURLOPT_URL, $url ); // アクセスするアドレス
  98. curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, 0 ); // 認証証明書のソースを確認します。
  99. curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, 1 ); // 証明書から SSL 暗号化アルゴリズムが存在するかどうかを確認します
  100. curl_setopt ( $curl, CURLOPT_USERAGENT, $user_agent ) // ユーザーが使用するブラウザをシミュレートします
  101. @curl_setopt ( $curl , CURLOPT_FOLLOWLOCATION, 1 ); // 自動リダイレクトを使用します
  102. curl_setopt ( $curl, CURLOPT_AUTOREFERER, 1 ); // リファラーを自動的に設定します
  103. curl, CURLOPT_HTTPGET, 1 ) // 通常の Post リクエストを送信します
  104. curl_setopt ( $ curl, CURLOPT_TIMEOUT , 120 ); // 無限ループを防ぐためにタイムアウト制限を設定します
  105. curl, CURLOPT_HEADER, 0 ) // 返されたヘッダー領域の内容を表示します
  106. curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );取得された情報はファイル ストリーム内にあります。
  107. $tmpInfo =curl_exec ( $curl ) // 操作を実行します
  108. if (curl_errno ( $curl )) {
  109. curl_close ( $curl ); session
  110. die('Errno' .curl_error ( $curl )) ;
  111. }
  112. curl_close ( $curl ); // CURL セッションを閉じる
  113. return $tmpInfo; // データを返す
  114. }
  115. }
  116. weather::getXML(" ");
コードをコピー

2、新浪天气 地址:http://php.weather.sina.com.cn 代:

  1. header ( 'Content-Type: text/html; charset = utf-8' );
  2. クラス天気 {
  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(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. 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. 静的プライベート関数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 (互換性; 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 ); // 定期的な投稿要求を送信します
  57. curl_setopt ( $curl, CURLOPT_TIMEOUT, 120 ); // 設定超過制限による死循環防止
  58. curl_setopt ( $curl, CURLOPT_HEADER, 0 ); // 返されたヘッダー領域の内容を表示します
  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();
复制代


ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!