Facebook 多级 json php foreach 循环的问题
Trouble with Facebook multi-level json php foreach loop
我知道像这样的问题以前已经被问过很多次了。但是请相信我,我已经搜索过了,但没有得到我的问题所特有的答案。
我正在尝试使用 php.
从 facebook 图 api 获得的 json 响应中获取信息
响应如下所示:
{
"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 个级别的数据数组。
我需要从 "photos" "data" 数组中获取 "source" 和 "name"。
这是我所做的:
<?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**/ ?>
我可以从第一层的 "albums" "data" 数组中获取 "id" 之类的信息(顺便说一句,我不需要),但那是据我所知。
我真正需要的是 "photos" 数组中的信息。
如果能帮助解决这个问题,我们将不胜感激。
<?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>
我知道像这样的问题以前已经被问过很多次了。但是请相信我,我已经搜索过了,但没有得到我的问题所特有的答案。
我正在尝试使用 php.
从 facebook 图 api 获得的 json 响应中获取信息响应如下所示:
{
"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 个级别的数据数组。 我需要从 "photos" "data" 数组中获取 "source" 和 "name"。
这是我所做的:
<?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**/ ?>
我可以从第一层的 "albums" "data" 数组中获取 "id" 之类的信息(顺便说一句,我不需要),但那是据我所知。 我真正需要的是 "photos" 数组中的信息。
如果能帮助解决这个问题,我们将不胜感激。
<?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>