未能加载外部实体(flickr网站)


failed to load external entity(flickr website)

我正试图从Flickr网站检索数据,在运行php代码后,它给了我两个错误。1) 未能加载外部实体2) 为foreach()提供的参数无效

在下面找到php代码:

<?php
$url = file_get_contents("http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=ff8c4c178209865b1ac5ee3f2d492de0&lat=51.5424&lon=-0.1734&radius=2&page=2&per_page=200&text=houses");
$xml = simplexml_load_file($url);
foreach ($xml->photo as $entry){
echo $entry->id;
echo $entry->owner;
echo $entry->title; 
}
?>

请参阅下面的xml结构摘录:

<rsp stat="ok">
<photos page="2" pages="6" perpage="200" total="1199">
<photo id="476179009" owner="55662771@N00" secret="cafd39b094" server="219" farm="1" title="Seeing the Sun Going Down" ispublic="1" isfriend="0" isfamily="0"/>
<photo id="5858562848" owner="40837632@N05" secret="19c083483f" server="5154" farm="6" title="Lords - June 2011 - E v SL - Rangana Herath Delivers" ispublic="1" isfriend="0" isfamily="0"/>
</photos>
</rsp>

simplexml_load_file需要一个文件名或URL,但file_get_contents已经从给定的URL中获取数据。因此,要么将$url传递给simplexml_load_file,要么使用simplexml_load_string

编辑:您的循环应该是这样的,请参阅simplexml文档。

foreach ($xml->photos->photo as $entry) {  
  echo $entry->attributes()->id;
  echo $entry->attributes()->owner;
  echo $entry->attributes()->title; 
}