Search code examples
phparraysflickr

PHP Flickr API - Retrieve Title from flickr.photosets.getInfo Response


I am getting the correct response from Flickr which is:

<?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>

For some reason I can't get the title, I've tried the following:

$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'];
}

And a couple of other crazy things, I know it's something silly but don't know what it is I have missed.

Response can be seen here: http://www.flickr.com/services/api/explore/flickr.photosets.getInfo

Just use 72157630469695108 as the set id or alternatively use this url: http://api.flickr.com/services/rest/?method=flickr.photosets.getInfo&api_key=7ccedd2c89ca10303394b8085541d9de&photoset_id=72157630469695108


Solution

  • You should use SimpleXMLElement directly, then xpath to find your node.

    $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.