PHP Flickr API - 从flickr.photosets.getInfo响应中检索标题

时间:2012-08-23 11:32:10

标签: php arrays flickr

我从Flickr得到了正确答案:

<?xml version="1.0" encoding="utf-8" ?>
<rsp stat="ok">
  <photoset id="72157630469695108" owner="15823425@N00" primary="5110549866" secret="fd716fb5ee" server="1136" farm="2" photos="101" count_views="67" count_comments="0" count_photos="101" count_videos="0" can_comment="0" date_create="1341701808" date_update="1345054078">
    <title>Montana</title>
    <description />
  </photoset>
</rsp>

出于某种原因,我无法获得头衔,我已经尝试了以下内容:

$album_info = simplexml_load_file($album_info_xml); // this is what the response is stored in

echo $album_info['photoset']['title'];

foreach($album_info->photoset as $ps) {
    echo $ps['title'];
}

还有其他一些疯狂的事情,我知道这是一件愚蠢的事情但不知道我错过了什么。

可以在此处看到响应:http://www.flickr.com/services/api/explore/flickr.photosets.getInfo

只需使用72157630469695108作为设置ID,或者使用此网址:http://api.flickr.com/services/rest/?method=flickr.photosets.getInfo&api_key=7ccedd2c89ca10303394b8085541d9de&photoset_id=72157630469695108

2 个答案:

答案 0 :(得分:1)

您应该直接使用SimpleXMLElement,然后使用xpath来查找您的节点。

$album_info = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8" ?>
<rsp stat="ok">
  <photoset id="72157630469695108" owner="15823425@N00" primary="5110549866" secret="fd716fb5ee" server="1136" farm="2" photos="101" count_views="67" count_comments="0" count_photos="101" count_videos="0" can_comment="1" date_create="1341701808" date_update="1345054078">
    <title>Montana</title>
    <description />
  </photoset>
</rsp>'); // this is what the response is stored in

$result = $album_info->xpath('//title');

foreach ($result as $title)
{
  echo $title . "\n";
}

Working example

答案 1 :(得分:0)

对于处于相同位置的任何人来说,这就是诀窍

$albumName = $album_info->photoset->title;