无法加载外部实体(flickr网站)

时间:2012-03-07 16:37:53

标签: php xml flickr

我正在尝试从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>

1 个答案:

答案 0 :(得分:0)

simplexml_load_file需要文件名或网址,但file_get_contents已从指定的网址中提取数据。因此,要么将$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; 
}