2011-06-29 53 views
2

我希望標題轉換成數組是指所有:-)如何將RSS XML飼料中使用PHP

如何將RSS XML飼料中使用PHP

這怎麼可能轉換爲數組做...

例如:我使用的是以下網址:

https://www.googleapis.com/shopping/search/v1/public/products?key=AIzaSyBnjn7hr6Zok78f35Q49Od-3wtTYeVH3NI&country=US&q=digital+camera&alt=atom

這導致XML p attern。 我需要使用PHP將其轉換爲數組格式。

任何幫助將感恩和感激。

預先感謝....

+0

你介意使用外部庫如ZF嗎? – akond

+0

請指出您在查詢之前檢查過哪些http://stackoverflow.com/search?q=parse+rss+php,以及爲什麼他們沒有幫助解決您的問題? – Gordon

+0

[用PHP解析RSS/Atom提要的最佳方法]的可能重複(http://stackoverflow.com/questions/250679/best-way-to-parse-rss-atom-feeds-with-php) – Gordon

回答

2

步驟通過步驟

How to Read an RSS Feed With PHP

<?php 

function getFeed($feed_url) { 

    $content = file_get_contents($feed_url); 
    $x = new SimpleXmlElement($content); 

    echo "<ul>"; 

    foreach($x->channel->item as $entry) { 
     echo "<li><a href='$entry->link' title='$entry->title'>" . $entry->title . "</a></li>"; 
    } 
    echo "</ul>"; 
} 
?> 

$x = new SimpleXmlElement($content); 

echo "<ul>"; 

foreach($x->channel->item as $entry) { 
    echo "<li><a href='$entry->link' title='$entry->title'>" . $entry->title . "</a></li>"; 
} 
echo "</ul>"; 

XML to Array in PHP

/* 
    Working with XML. Usage: 
    $xml=xml2ary(file_get_contents('1.xml')); 
    $link=&$xml['ddd']['_c']; 
    $link['twomore']=$link['onemore']; 
    // ins2ary(); // dot not insert a link, and arrays with links inside! 
    echo ary2xml($xml); 
*/ 

// XML to Array 
function xml2ary(&$string) { 
    $parser = xml_parser_create(); 
    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); 
    xml_parse_into_struct($parser, $string, $vals, $index); 
    xml_parser_free($parser); 

    $mnary=array(); 
    $ary=&$mnary; 
    foreach ($vals as $r) { 
     $t=$r['tag']; 
     if ($r['type']=='open') { 
      if (isset($ary[$t])) { 
       if (isset($ary[$t][0])) $ary[$t][]=array(); else $ary[$t]=array($ary[$t], array()); 
       $cv=&$ary[$t][count($ary[$t])-1]; 
      } else $cv=&$ary[$t]; 
      if (isset($r['attributes'])) {foreach ($r['attributes'] as $k=>$v) $cv['_a'][$k]=$v;} 
      $cv['_c']=array(); 
      $cv['_c']['_p']=&$ary; 
      $ary=&$cv['_c']; 

     } elseif ($r['type']=='complete') { 
      if (isset($ary[$t])) { // same as open 
       if (isset($ary[$t][0])) $ary[$t][]=array(); else $ary[$t]=array($ary[$t], array()); 
       $cv=&$ary[$t][count($ary[$t])-1]; 
      } else $cv=&$ary[$t]; 
      if (isset($r['attributes'])) {foreach ($r['attributes'] as $k=>$v) $cv['_a'][$k]=$v;} 
      $cv['_v']=(isset($r['value']) ? $r['value'] : ''); 

     } elseif ($r['type']=='close') { 
      $ary=&$ary['_p']; 
     } 
    }  

    _del_p($mnary); 
    return $mnary; 
} 

// _Internal: Remove recursion in result array 
function _del_p(&$ary) { 
    foreach ($ary as $k=>$v) { 
     if ($k==='_p') unset($ary[$k]); 
     elseif (is_array($ary[$k])) _del_p($ary[$k]); 
    } 
} 

// Array to XML 
function ary2xml($cary, $d=0, $forcetag='') { 
    $res=array(); 
    foreach ($cary as $tag=>$r) { 
     if (isset($r[0])) { 
      $res[]=ary2xml($r, $d, $tag); 
     } else { 
      if ($forcetag) $tag=$forcetag; 
      $sp=str_repeat("\t", $d); 
      $res[]="$sp<$tag"; 
      if (isset($r['_a'])) {foreach ($r['_a'] as $at=>$av) $res[]=" $at=\"$av\"";} 
      $res[]=">".((isset($r['_c'])) ? "\n" : ''); 
      if (isset($r['_c'])) $res[]=ary2xml($r['_c'], $d+1); 
      elseif (isset($r['_v'])) $res[]=$r['_v']; 
      $res[]=(isset($r['_c']) ? $sp : '')."</$tag>\n"; 
     } 

    } 
    return implode('', $res); 
} 

// Insert element into array 
function ins2ary(&$ary, $element, $pos) { 
    $ar1=array_slice($ary, 0, $pos); $ar1[]=$element; 
    $ary=array_merge($ar1, array_slice($ary, $pos)); 
}