如何通过 flickr.photos.search API 调用访问 XML?

发布于 2024-08-20 14:14:42 字数 1910 浏览 4 评论 0原文

我客户的系统需要使用 flickr.photos.search API 调用从其 Flickr 帐户访问私人照片。我设置了它并获取了该调用生成的 URL。当我在浏览器中访问该 URL 时,它会按照应有的方式输出 XML。

(API 参考:http://www.flickr.com/services/ api/flickr.photos.search.html

但是,在 PHP 中,我想访问该 XML 并使用 simplexml PHP 扩展显示它。我不知道如何访问 XML,因为它不是位于 .xml 文件中,而是位于动态 URL 中。

XML 文件(来自浏览器)如下所示:

<rsp stat="ok">
  <photos page="1" pages="1" perpage="100" total="4">
    <photo id="4332852622" owner="36520372@N05" secret="xxxxxxxx" server="2760" farm="3" title="building" ispublic="0" isfriend="0" isfamily="0"/>
    <photo id="4332113745" owner="36520372@N05" secret="xxxxxxx" server="2803" farm="3" title="digging" ispublic="0" isfriend="0" isfamily="0"/>
    <photo id="4332852444" owner="36520372@N05" secret="xxxxxxx" server="4025" farm="5" title="house" ispublic="0" isfriend="0" isfamily="0"/>
    <photo id="4332113699" owner="36520372@N05" secret="xxxxxxx" server="2802" farm="3" title="PaulLuomaHab" ispublic="0" isfriend="0" isfamily="0"/>
  </photos>
</rsp>

对于 PHP,我正在尝试这样做:

$rsp = simplexml_load_file($flickrURL);
foreach($rsp->photos->photo as $photo) {
    echo $photo->title;
}

它根本不返回任何内容。我在这里错过了什么吗?

注意:我在上面的foreach循环中添加了echo "Ding!";,它确实 echo Ding!Ding!Ding!Ding! ,这意味着它识别出有 4 张照片并循环播放正确的次数。

那么显然它只是出于某种原因对 $photo->title 不满意?

另外

当我使用print_r($photo)时我得到这个:

SimpleXMLElement Object ( [@attributes] => Array ( [id] => 4332852622 [owner] => 36520372@N05 [secret] => 88fff62f43 [server] => 2760 [farm] => 3 [title] => building [ispublic] => 0 [isfriend] => 0 [isfamily] => 0 ) )

My client's system needs to access private photos from their Flickr account using the flickr.photos.search API call. I set it up and got the generated URL for that call. When I visit that URL in the browser, it outputs XML like it should.

(API reference: http://www.flickr.com/services/api/flickr.photos.search.html)

However, in PHP I want to access that XML and display it using the simplexml PHP extension. I can't figure out how to access the XML, since it's not sitting in a .xml file but rather in a dynamic URL.

The XML file (from the browser) looks like this:

<rsp stat="ok">
  <photos page="1" pages="1" perpage="100" total="4">
    <photo id="4332852622" owner="36520372@N05" secret="xxxxxxxx" server="2760" farm="3" title="building" ispublic="0" isfriend="0" isfamily="0"/>
    <photo id="4332113745" owner="36520372@N05" secret="xxxxxxx" server="2803" farm="3" title="digging" ispublic="0" isfriend="0" isfamily="0"/>
    <photo id="4332852444" owner="36520372@N05" secret="xxxxxxx" server="4025" farm="5" title="house" ispublic="0" isfriend="0" isfamily="0"/>
    <photo id="4332113699" owner="36520372@N05" secret="xxxxxxx" server="2802" farm="3" title="PaulLuomaHab" ispublic="0" isfriend="0" isfamily="0"/>
  </photos>
</rsp>

And with PHP I'm trying this:

$rsp = simplexml_load_file($flickrURL);
foreach($rsp->photos->photo as $photo) {
    echo $photo->title;
}

It returns nothing at all. Am I missing something here?

NOTE: I added echo "Ding!"; inside of the foreach loop above, and it DOES echo Ding!Ding!Ding!Ding!, which means it's recognizing there are 4 photos and looping through the right number of times.

So apparently it's just not happy with $photo->title for some reason?

Also

When I use print_r($photo) I get this:

SimpleXMLElement Object ( [@attributes] => Array ( [id] => 4332852622 [owner] => 36520372@N05 [secret] => 88fff62f43 [server] => 2760 [farm] => 3 [title] => building [ispublic] => 0 [isfriend] => 0 [isfamily] => 0 ) )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怎樣才叫好 2024-08-27 14:14:42

我自己解决了,呵呵。标题是 XML 元素的属性,而不是节点,因此它的访问方式类似于数组,而不是对象属性(令人困惑的术语)。

所以它是 echo $photo['title']; 和噗!有用。

I solved it on my own, doh. Title is an attribute of the XML element, and not a node, so it's accessed like an array rather than like an object attribute (confusing terminology).

So it's echo $photo['title']; and poof! it works.

无言温柔 2024-08-27 14:14:42

使用 SimpleXML 时,您应该始终以它所代表的节点来命名您的 PHP 变量。它有助于清楚地了解您在 XML 树中的位置。例如,您的代码应该是:

$rsp = simplexml_load_file($flickrURL);
foreach($rsp->photos->photo as $photo)
{
    echo $photo['title'];
}

When using SimpleXML, you should always name your PHP variable after the node it represents. It helps keeping a clear idea of where you are in your XML tree. For instance, your code should be:

$rsp = simplexml_load_file($flickrURL);
foreach($rsp->photos->photo as $photo)
{
    echo $photo['title'];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文