2017-07-27 52 views
-2

IM剛開: 「提供的foreach無效參數()」「爲foreach()提供的無效參數」讀取XML?

的用於嘗試讀取XML

PHP的特定商品時的每一行:

$urls="http://www.floatrates.com/daily/usd.xml";  

$url= $urls; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
$data = curl_exec($ch); 
curl_close($ch); 
$feed = new SimpleXMLElement($data,LIBXML_NOCDATA); 


foreach ($feed->channel->item as $item) { 
$title  = $item->title; 
if (preg_match('/ARS/',$title)) { 
    echo $title; 
} 

} 

回答

0
$urls = "http://www.floatrates.com/daily/usd.xml"; 
$url = $urls; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
$data = curl_exec($ch); 
curl_close($ch); 
$feed = new SimpleXMLElement($data, LIBXML_NOCDATA); 
foreach ($feed->children() as $item) { 
if ("item" == $item->getName()) { 
    $title = $item->title; 
     if (preg_match('/ARS/', $title)) { 
      echo $title; 
     } 
    } 

} 
+0

弗拉基米爾的代碼有效。 – RockwoodON

+0

偉大的作品!,但爲什麼行:「$頻道= $飼料 - >頻道;」? – Targaryen

0

它可能意味着$ feed沒有包含您期望它包含的內容,並且您沒有將數組傳遞給foreach使用print_r($feed);來查看它實際包含的內容,並從那裏開始。