【问题标题】:failed to load external entity(flickr website)未能加载外部实体(flickr 网站)
【发布时间】:2012-03-07 16:37:52
【问题描述】:

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

【问题讨论】:

    标签: php xml flickr


    【解决方案1】:

    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; 
    }
    

    【讨论】:

    • 查看我编辑的答案,您没有正确解析返回的 xml。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-20
    • 1970-01-01
    • 1970-01-01
    • 2016-06-04
    • 1970-01-01
    相关资源
    最近更新 更多