"为foreach()提供的参数无效"读XML?

时间:2017-07-27 19:43:50

标签: php xml curl feed

IM gettin:"为foreach()"

提供的参数无效 尝试读取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;
}

}

2 个答案:

答案 0 :(得分: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;
        }
    }

}

答案 1 :(得分:0)

这可能意味着$ feed不包含您希望它包含的内容,并且您没有将数组传递给foreach使用print_r($feed);来查看它实际包含的内容,然后从那里开始。< / p>