10.10.2010

[PHP]simplexml

Yahoo! Weather の RSS Feed をsimplexmlでパースする。

まずは、
取得したXMLをsimplexml_load_string()でsimplexmlオブジェクトに展開する。
$xml = file_get_contents($rss);
  $w = simplexml_load_string($xml);
後は、
default namespaceの要素を取得するなら、
引数なしでchildren()と実行し、
$ch = $w->channel->children();
namespaceの指定がある要素を取得するなら、
children($namespace) と引数ありで実行するだけ。
$ch = $w->channel->children($namespace);
全体はこんな感じ。
$ cat weather.php
<?php

  $rss = "http://weather.yahooapis.com/forecastrss?w=2348079&u=c";
  $xml = file_get_contents($rss);
  if(!$xml) exit(1);
  $w = simplexml_load_string($xml);

  $yw  = "http://xml.weather.yahoo.com/ns/rss/1.0";
  $geo = "http://www.w3.org/2003/01/geo/wgs84_pos#"

  // default namespace <channel><title>
  $ch = $w->channel->children();
  foreach($ch as $k => $v){
    if($k === "lastBuildDate") $weather[$k] = $v;
  }

  // <channel><yweather>
  $ch = $w->channel->children($yw);
  foreach($ch as $category => $kv){
    foreach($kv->attributes() as $k => $v){ 
      $category = (string)$category;
      $k = (string)$k;
      $v = (string)$v;
      $weather[$category][$k]=$v;
    }
  }

  // <channel><item><geo>
  $ch = $w->channel->item->children($geo);
  foreach($ch as $k => $v){
    $k = (string)$k;
    $v = (string)$v;
    $weather["location"][$k] = $v;
  }

  // <channel><item><yweather>
  $item = $w->channel->item->children($yw);
  foreach($item as $category => $kv){
    foreach($kv->attributes() as $k => $v){
      $category = (string)$category;
      $k = (string)$k;
      $v = (string)$v;
      if($k === "day" && $category === "forecast") $day = $v;
      if($category === "forecast"){
        $weather[$category][$day][$k] = $v;
      }
      else{
        $weather[$category][$k] = $v;
      }
    }
  }

  print_r($weather);
?>
実行すると
$ php weather.php
Array
(
    [location] => Array
        (  
            [city] => Auckland
            [region] =>
            [country] => New Zealand
            [lat] => -36.88
            [long] => 174.77
        )

    [units] => Array
        (  
            [temperature] => C
            [distance] => km
            [pressure] => mb
            [speed] => km/h
        )

    [wind] => Array
        (  
            [chill] => 11
            [direction] => 270
            [speed] => 24.14
        )

    [atmosphere] => Array
        (  
            [humidity] => 94
            [visibility] => 9.99
            [pressure] => 1015.92
            [rising] => 0
        )

    [astronomy] => Array
        (  
            [sunrise] => 6:43 am
            [sunset] => 7:32 pm
        )

    [condition] => Array
        (
            [text] => Rain Shower
            [code] => 11
            [temp] => 11
            [date] => Mon, 11 Oct 2010 2:00 am NZDT
        )

    [forecast] => Array
        (
            [Mon] => Array
                (
                    [day] => Mon
                    [date] => 11 Oct 2010
                    [low] => 8
                    [high] => 13
                    [text] => Showers
                    [code] => 11
                )

            [Tue] => Array
                (
                    [day] => Tue
                    [date] => 12 Oct 2010
                    [low] => 12
                    [high] => 16
                    [text] => Partly Cloudy
                    [code] => 30
                )

        )

)


便利だね、simplexml!!

0 件のコメント:

コメントを投稿