約2年前、私はそのようなデータの便利でオープンソースをウェブで検索しましたが、何も見つかりませんでした。 国内のすべてのサービスは金銭のためにデータをエクスポートしました(むしろ、それは私を怖がらせる価格ではなく、支払いをいじっていました)が、西洋のサービスは自動処理に便利な形式でデータを提供しなかったか、私が住んでいる都市の存在を知りませんでした
Yahoo! 天気はこれらの欠点の欠如に満足しています。 現在の天気に関する情報と今後5日間の予報は、RSS形式でサーバーからエクスポートされます。 私の意見では、この形式の選択は非常に難しいです。 天気RSSフィードには1つのアイテムのみが含まれます。 同じデータをより簡潔な形式でエクスポートすることもできますが、ヤフーは標準のRSS処理方法に依存しているようです。これはかなり重要な議論でもあります。 標準は依然として標準です。
関心のある都市のRSSフィードアドレスは、 weather.yahoo.comで見つけることができます。
/2008/02/yahoo-weather.png) 
      一般に、URLは次のとおりです。
 xml.weather.yahoo.com/forecastrss?p=_&u=_
      
      最初のパラメーターは、対象の都市の識別子です。 2番目のパラメーターは、温度を表示する単位を決定します。 値「c」は摂氏温度、「f」は華氏温度に対応します。
いくつかの例:
- モスクワ-http://xml.weather.yahoo.com/forecastrss ? p = RSXX0063&u = c
- サンクトペテルブルク-http://xml.weather.yahoo.com/forecastrss ? p = RSXX0091&u = c
- エカテリンブルグ-http://xml.weather.yahoo.com/forecastrss ? p = RSXX1219&u = c
- ハンティ・マンシースク-http://xml.weather.yahoo.com/forecastrss?p=RSXX1234&u= c
- キエフ-http://xml.weather.yahoo.com/forecastrss ? p = UPXX0016&u = c
これを自動化する
以下で説明する
YahooWeather
      
      クラス(PHP5)は、HTTP経由でRSSをダウンロードし、データを前処理するように設計されています。
  <?php
クラスYahooWeather {
	 //風
	 public $ wind_chill;
	 public $ wind_direction;
	 public $ wind_speed;
	
	 //大気インジケーター
	パブリック$湿度;
	パブリック$可視性;
	公的圧力;
	
	 //日の出と日の入りの時刻をUNIXの時刻形式に変換します
	パブリック$日の出;
	パブリック$サンセット;
	
	 //現在の気温と天気
	 public $ temp;
	 public $ condition_text;
	 public $ condition_code;
	
	 // 5日間の天気予報
	パブリック$予測。
	
	パブリック$ユニット;
	
	関数__construct($コード、$単位= 'c'、$ lang = 'en'){
		 $ this-> units =($ units == 'c')? 'c': 'f';
		
		 $ url = 'http://xml.weather.yahoo.com/forecastrss?p='。
			 $ code。 '&u ='。$ this-> units;
		
		 $ xml_contents = file_get_contents($ url);
		 if($ xml_contents === false) 
			新しい例外をスロー(「エラー読み込み」。$ url);
		
		 $ xml = new SimpleXMLElement($ xml_contents);
		
		 //風
		 $ tmp = $ xml-> xpath( '/ rss / channel / yweather:wind');
		 if($ tmp === false)throw new Exception( "Error parsing XML。");
		 $ tmp = $ tmp [0];
		
		 $ this-> wind_chill =(int)$ tmp ['chill'];
		 $ this-> wind_direction =(int)$ tmp ['direction'];
		 $ this-> wind_speed =(int)$ tmp ['speed'];
		
		 //大気インジケーター
		 $ tmp = $ xml-> xpath( '/ rss / channel / yweather:atmosphere');
		 if($ tmp === false)throw new Exception( "Error parsing XML。");
		 $ tmp = $ tmp [0];
		
		 $ this->湿度=(int)$ tmp ['湿度'];
		 $ this-> visibility =(int)$ tmp ['visibility'];
		 $ this-> pressure =(int)$ tmp ['pressure'];
		
		 //日の出と日の入りの時刻をUNIXの時刻形式に変換します
		 $ tmp = $ xml-> xpath( '/ rss / channel / yweather:astronomy');
		 if($ tmp === false)throw new Exception( "Error parsing XML。");
		 $ tmp = $ tmp [0];
		
		 $ this-> sunrise = strtotime($ tmp ['sunrise']);
		 $ this-> sunset = strtotime($ tmp ['sunset']);
		
		 //現在の気温と天気
		 $ tmp = $ xml-> xpath( '/ rss / channel / item / yweather:condition');
		 if($ tmp === false)throw new Exception( "Error parsing XML。");
		 $ tmp = $ tmp [0];
		
		 $ this-> temp =(int)$ tmp ['temp'];
		 $ this-> condition_text = strtolower((string)$ tmp ['text']);
		 $ this-> condition_code =(int)$ tmp ['code'];
		
		 // 5日間の天気予報
		 $予測=配列();
		 $ tmp = $ xml-> xpath( '/ rss / channel / item / yweather:予測');
		 if($ tmp === false)throw new Exception( "Error parsing XML。");
		
		 foreach($ tmpを$日として){
			 $ this->予測[] =配列(
				 'date' => strtotime((string)$ day ['date'])、
				 'low' =>(int)$ day ['low']、
				 'high' =>(int)$ day ['high']、
				 'text' =>(文字列)$ day ['text']、
				 'code' =>(int)$ day ['code']
			 );
		 }
	 }
	
	パブリック関数__toString(){
		 $ u = "°"。(($ this-> units == 'c')? 'C': 'F');
		 return $ this-> temp。 '  '。$ u。'、 '。$ this-> condition_text;
	 }
 }
 ?> 
      使用例:
  <?php
 {
	 $ weather =新しいYahooWeather( 'RSXX0091');
 } catch(例外$ e){
	 echo "キャッチされた例外:" $ e-> getMessage();
	 exit();
 }
 echo '<h1>'。$ weather。 '</ h1>';
 echo "<pre>";
 print_r($天気);
 echo "</ pre>";
 ?> 
      結論:
/2008/02/weather-report.png) 
      日の出と日の入りの時刻値(それぞれ
sunrise
      
      と
sunset
      
      フィールド)は、Unixの時刻形式に自動的に変換されます。 何らかの理由でYahoo! 明らかに不正確な大気圧値(0)を生成します。 ただし、XMLに対応する属性が存在することから、遅かれ早かれこのデータのエクスポートが修復される可能性が多少あります。
condition_code
      
      フィールドは、天気コードを格納するために使用されます。 現在の値のテキスト解釈は
condition_text
      
      フィールドに保存され、可能なオプションの完全なリストが次の表に示されています。
| コード | 説明 | 
| 0 | 竜巻 | 
| 1 | 熱帯嵐 | 
| 2 | ハリケーン | 
| 3 | 激しい雷雨 | 
| 4 | 雷雨 | 
| 5 | 混じった雨と雪 | 
| 6 | 雨とみぞれが混ざった | 
| 7 | 雪とみぞれの混合 | 
| 8 | 凍結霧雨 | 
| 9 | 霧雨 | 
| 10 | にわか雨 | 
| 11 | にわか雨 | 
| 12 | にわか雨 | 
| 13 | にわか雪 | 
| 14 | にわか雪 | 
| 15 | 吹雪 | 
| 16 | 雪 | 
| 17 | あられ | 
| 18 | みぞれ | 
| 19 | ほこり | 
| 20 | 霧深い | 
| 21 | もや | 
| 22 | スモーキー | 
| 23 | 吹き荒れ | 
| 24 | 風が強い | 
| 25 | 寒い | 
| 26 | 曇った | 
| 27 | だいたい曇(夜) | 
| 28 | おおむね曇り(日) | 
| 29日 | 所により曇り(夜) | 
| 30 | 曇り時々晴れ | 
| 31 | 晴れ(夜) | 
| 32 | 晴れ | 
| 33 | フェア(夜) | 
| 34 | フェア(日) | 
| 35 | にわか雨 | 
| 36 | 暑い | 
| 37 | 孤立した雷雨 | 
| 38 | 散在する雷雨 | 
| 39 | 散在する雷雨 | 
| 40 | にわか雨 | 
| 41 | 大雪 | 
| 42 | にわか雪 | 
| 43 | 大雪 | 
| 44 | 部分的に曇り | 
| 45 | 雷雨 | 
| 46 | にわか雨 | 
| 47 | 孤立した雷雨 | 
| 3200 | 利用できません | 
この記事はparadigm.ruブログに掲載されました。