2012-08-23 51 views
0

我收到來自Flickr正確的反應是檢索標題:PHP Flickr的API從flickr.photosets.getInfo響應

<?xml version="1.0" encoding="utf-8" ?> 
<rsp stat="ok"> 
    <photoset id="72157630469695108" owner="[email protected]" 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

+0

只是爲了確保,$ album_info_xml包含一個文件名,而不是實際的XML字符串? – ekholm

+0

這是flickr api調用的網址,例如'$ album_info_xml ='http://api.flickr.com/services/rest/?method = flickr.photosets.getInfo&api_key ='。$ api。'&photoset_id ='。$ set_id;' – martincarlin87

+0

好的,你有沒有檢查過響應用'print_r($ album_info);'? – ekholm

回答

1

您應該直接使用SimpleXMLElement,然後xpath找到您的節點。

$album_info = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8" ?> 
<rsp stat="ok"> 
    <photoset id="72157630469695108" owner="[email protected]" 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

0

對於在同一位置的人,這並獲得成功

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