我知道像这样的问题以前被问过很多次。但相信我,我已经搜索过了,却没有得到我的问题特有的答案。
我正试图从一个json响应中获取信息,该响应是我从facebook图形api中获得的,使用php。
响应如下:
{
"albums": {
"data": [
{
"id": "1111111111111111111111111111",
"created_time": "2015-02-27T22:46:22+0000",
"photos": {
"data": [
{
"name": "wedding",
"source": "https://scontent.xx.fbcdn.net/hphotos-xpf1/v/t1.0-9/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"created_time": "2015-02-27T22:46:28+0000",
"id": "1111111111111111111111111111"
},
{
"source": "https://scontent.xx.fbcdn.net/hphotos-xpf1/v/t1.0-9/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"created_time": "2015-02-27T22:47:03+0000",
"id": "1111111111111111111111111111"
},
{
"source": "https://fbcdn-sphotos-e-a.akamaihd.net/hphotos-ak-xfp1/v/t1.0-9/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"created_time": "2015-02-27T22:47:10+0000",
"id": "1111111111111111111111111111"
},
{
"name": "Shells",
"source": "https://fbcdn-sphotos-g-a.akamaihd.net/hphotos-ak-xap1/v/t1.0-9/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"created_time": "2015-02-28T00:09:45+0000",
"id": "1111111111111111111111111111"
}
],
"paging": {
"cursors": {
"before": "MTU5NjUyNDQzMzkxMDkwNQ==",
"after": "MTU5NjU0MDcwMzkwOTI3OA=="
}
}
}
},
{
"id": "1111111111111111666111111111",
"created_time": "2015-02-27T22:35:44+0000",
"photos": {
"data": [
{
"source": "https://scontent.xx.fbcdn.net/hphotos-xpf1/v/t1.0-9/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"created_time": "2015-02-27T22:35:46+0000",
"id": "111111111111111111111111333"
}
],
"paging": {
"cursors": {
"before": "MTU5NjUyMjU1NzI0NDQyNg==",
"after": "MTU5NjUyMjU1NzI0NDQyNg=="
}
}
}
}
],
"paging": {
"cursors": {
"after": "MTU5NjUyMjU1MDU3Nzc2MA==",
"before": "MTU5NjUyNDQwNzI0NDI0MQ=="
}
}
},
"id": "76577536756346436343434"
}
问题是;响应具有类似2个级别的数据阵列。我需要从"照片"数据"数组中获取"来源"answers"名称"。
这就是我所做的:
<?php
$fbpageid = "I HAVE A VALID PAGE ID I USED HERE";
$fbaccess_token = "I HAVE A VALID ACCESS TOKEN I USED HERE";
$fburl = "https://graph.facebook.com/v2.2/{$fbpageid}?fields=albums{photos{name,source}}&access_token={$fbaccess_token}";
/***I ENTERED THIS URL ABOVE INTO MY BROWSER WITH ALL THE VARIABLES CORRECT, IT WORKED FINE.. ..GAVE ME A RESPONSE LIKE THE ONE ABOVE**/
$fbfetch = file_get_contents($fburl); /***FROM MY PREVIOUS INSTAGRAM PROJECTS, I KNOW THIS WORKS FINE**/
$albums = json_decode($fbfetch); /***I KNOW THIS WORKS FINE TOO**/
<?php
foreach ($albums->albums->data as $photo) {
echo $photo->id . '<br>'; /**THIS WORKS**/
}
?>
foreach ($albums->albums->data->photos->data as $photo) { ?>
<a href="<?php echo $photo['source']; ?>" title="<?php echo $photo['name']; ?>">
<img src="<?php echo $photo['source']; ?>" alt="<?php echo $photo['name']; ?>" />
</a>
<?php } /**THIS DOESNT**/ ?>
我可以从一级的"相册"数据"数组中获得像"id"这样的信息(顺便说一句,我不需要),但这是我所能做到的。我真正需要的是"照片"数组中的信息。
如果能为解决这个问题提供任何帮助,我们将不胜感激。
<?php
$fbfetch='{
"albums": {
"data": [
{
"id": "1111111111111111111111111111",
"created_time": "2015-02-27T22:46:22+0000",
"photos": {
"data": [
{
"name": "wedding",
"source": "https://scontent.xx.fbcdn.net/hphotos-xpf1/v/t1.0-9/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"created_time": "2015-02-27T22:46:28+0000",
"id": "1111111111111111111111111111"
},
{
"source": "https://scontent.xx.fbcdn.net/hphotos-xpf1/v/t1.0-9/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"created_time": "2015-02-27T22:47:03+0000",
"id": "1111111111111111111111111111"
},
{
"source": "https://fbcdn-sphotos-e-a.akamaihd.net/hphotos-ak-xfp1/v/t1.0-9/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"created_time": "2015-02-27T22:47:10+0000",
"id": "1111111111111111111111111111"
},
{
"name": "Shells",
"source": "https://fbcdn-sphotos-g-a.akamaihd.net/hphotos-ak-xap1/v/t1.0-9/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"created_time": "2015-02-28T00:09:45+0000",
"id": "1111111111111111111111111111"
}
],
"paging": {
"cursors": {
"before": "MTU5NjUyNDQzMzkxMDkwNQ==",
"after": "MTU5NjU0MDcwMzkwOTI3OA=="
}
}
}
},
{
"id": "1111111111111111666111111111",
"created_time": "2015-02-27T22:35:44+0000",
"photos": {
"data": [
{
"source": "https://scontent.xx.fbcdn.net/hphotos-xpf1/v/t1.0-9/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"created_time": "2015-02-27T22:35:46+0000",
"id": "111111111111111111111111333"
}
],
"paging": {
"cursors": {
"before": "MTU5NjUyMjU1NzI0NDQyNg==",
"after": "MTU5NjUyMjU1NzI0NDQyNg=="
}
}
}
}
],
"paging": {
"cursors": {
"after": "MTU5NjUyMjU1MDU3Nzc2MA==",
"before": "MTU5NjUyNDQwNzI0NDI0MQ=="
}
}
},
"id": "76577536756346436343434"
}';
$albums = json_decode($fbfetch);
//print_r($albums); //For troubleshooting
foreach ($albums->albums->data as $photos) { // I changed $photo to $photos here
echo $photos->id . '<br>'; /**THIS WORKS**/
//print_r($photos); //For troubleshooting
foreach ($photos->photos->data as $photo) { ?>
<?php //print_r($photo); //For troubleshooting ?>
<a href="<?php echo $photo->source; ?>" title="<?php echo $photo->name; ?>">
<img src="<?php echo $photo->source; ?>" alt="<?php echo $photo->name; ?>" />
</a>
<?php }
}
?>
出于我的目的,我使用了字符串格式的示例JSON。你会注意到我用过的一些print_r。他们现在被评论掉了。我只是用它们来帮助找到问题。问题出在最后。
<a href="<?php echo $photo->source; ?>" title="<?php echo $photo->name; ?>">
<img src="<?php echo $photo->source; ?>" alt="<?php echo $photo->name; ?>" />
</a>
这些被写成数组$photo['source']
,而不是对象$photo->source
(是的,我仍然使用嵌入式foreach)
我希望这会有所帮助!它对我来说运行得很完美
1111111111111111111111111111<br> <a href="https://scontent.xx.fbcdn.net/hphotos-xpf1/v/t1.0-9/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" title="wedding">
<img src="https://scontent.xx.fbcdn.net/hphotos-xpf1/v/t1.0-9/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" alt="wedding" />
</a>
<a href="https://scontent.xx.fbcdn.net/hphotos-xpf1/v/t1.0-9/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" title="">
<img src="https://scontent.xx.fbcdn.net/hphotos-xpf1/v/t1.0-9/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" alt="" />
</a>