麻烦与Facebook多层次的JSON PHP foreach循环 [英] Trouble with Facebook multi-level json php foreach loop
问题描述
我知道像这样的问题已经被问过很多次。但请相信我,我已搜查没有得到特殊的回答我的问题。
我试图从一个JSON响应我从Facebook图形API得到的信息,使用PHP。
回应是这样的:
{
相册:{
数据:
{
ID:1111111111111111111111111111
CREATED_TIME:2015-02-27T22:46:22 + 0000,
相片: {
数据:
{
名:婚礼,
源:https://scontent.xx.fbcdn.net/hphotos-xpf1/v/t1.0-9/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CREATED_TIME:2015-02-27T22:46:28 + 0000,
ID:1111111111111111111111111111
},
{
源:https://scontent.xx.fbcdn.net/hphotos-xpf1/v/t1.0-9/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CREATED_TIME:2015-02-27T22:47:03 + 0000,
ID:1111111111111111111111111111
},
{
资源: \"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
},
{
名:壳,
资源: \"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
}
]
分页:{
游标:{
之前:MTU5NjUyNDQzMzkxMDkwNQ ==
内经:MTU5NjU0MDcwMzkwOTI3OA ==
}
}
}
},
{
ID:1111111111111111666111111111
CREATED_TIME:2015-02-27T22:35:44 + 0000,
相片: {
数据:
{
源:https://scontent.xx.fbcdn.net/hphotos-xpf1/v/t1.0-9/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CREATED_TIME:2015-02-27T22:35:46 + 0000,
ID:111111111111111111111111333
}
]
分页:{
游标:{
之前:MTU5NjUyMjU1NzI0NDQyNg ==
内经:MTU5NjUyMjU1NzI0NDQyNg ==
}
}
}
}
]
分页:{
游标:{
内经:MTU5NjUyMjU1MDU3Nzc2MA ==
之前:MTU5NjUyNDQwNzI0NDI0MQ ==
}
}
},
ID:76577536756346436343434
}
问题是;响应有像2级别的数据阵列。
我需要的源和名,从照片,数据数组。
这是我干了什么:
< PHP
$ fbpageid =我有我这里用有效的页面ID;
$ fbaccess_token =我有一个有效的访问令牌,我这里使用的;
$ fburl = \"https://graph.facebook.com/v2.2/{$fbpageid}?fields=albums{photos{name,source}}&access_token={$fbaccess_token}\";
/ ***我进入这个网址高于走进我的浏览器使用正确的变量,它工作得很好.. ..GAVE ME喜欢的人的响应高于** /$ fbfetch =的file_get_contents($ fburl); / ***从我的preVIOUS Instagram的项目,我知道这工作正常** /$ =专辑json_de code($ fbfetch); / ***我知道这工作正常TOO ** / < PHP
的foreach($ albums-> albums->数据$照片){
回声$光电> ID。 '< BR>'; / **这个工程** /
}
?>的foreach($ albums-> albums->数据 - > photos->数据$照片)?{>
< A HREF =< PHP的echo $照片['源'];?>中标题=< PHP的echo $照片['名'];?>>
< IMG SRC =< PHP的echo $照片['源'];?>中ALT =< PHP的echo $照片['名'];?>中/> &所述; / A>
< PHP} / **这并不** /&GT?;
我能够得到这样从专辑,ID(我不需要通过的方式)在第1层的数据阵列信息,但是这远,我去。
我真正需要的是照片阵中的信息。
任何帮助以解决这一问题将是非常美联社preciated。
< PHP
$ fbfetch ='{
相册:{
数据:
{
ID:1111111111111111111111111111
CREATED_TIME:2015-02-27T22:46:22 + 0000,
相片: {
数据:
{
名:婚礼,
源:https://scontent.xx.fbcdn.net/hphotos-xpf1/v/t1.0-9/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CREATED_TIME:2015-02-27T22:46:28 + 0000,
ID:1111111111111111111111111111
},
{
源:https://scontent.xx.fbcdn.net/hphotos-xpf1/v/t1.0-9/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CREATED_TIME:2015-02-27T22:47:03 + 0000,
ID:1111111111111111111111111111
},
{
资源: \"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
},
{
名:壳,
资源: \"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
}
]
分页:{
游标:{
之前:MTU5NjUyNDQzMzkxMDkwNQ ==
内经:MTU5NjU0MDcwMzkwOTI3OA ==
}
}
}
},
{
ID:1111111111111111666111111111
CREATED_TIME:2015-02-27T22:35:44 + 0000,
相片: {
数据:
{
源:https://scontent.xx.fbcdn.net/hphotos-xpf1/v/t1.0-9/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CREATED_TIME:2015-02-27T22:35:46 + 0000,
ID:111111111111111111111111333
}
]
分页:{
游标:{
之前:MTU5NjUyMjU1NzI0NDQyNg ==
内经:MTU5NjUyMjU1NzI0NDQyNg ==
}
}
}
}
]
分页:{
游标:{
内经:MTU5NjUyMjU1MDU3Nzc2MA ==
之前:MTU5NjUyNDQwNzI0NDI0MQ ==
}
}
},
ID:76577536756346436343434
}; $ =专辑json_de code($ fbfetch);//的print_r($专辑); //有关疑难解答
的foreach($ albums-> albums->数据$照片){//我改变$照片这里$照片
回声$ photos-> ID。 '< BR>'; / **这个工程** /
//的print_r($照片); //有关疑难解答
的foreach($ photos-> photos->数据$照片)?{>
< PHP //的print_r($照片); //有关疑难解答&GT?;
< A HREF =< PHP的echo $光电>源;>中标题=< PHP的echo $光电>名;>中>
< IMG SRC =< PHP的echo $光电>源;>中ALT =< PHP的echo $光电>名;>中/> &所述; / A> < PHP}
}
?>
对于我而言,我用字符串格式的例子JSON。你会注意到一些的print_r的,我用。他们现在被注释掉。我只是用它们来帮助查找问题。问题是在最后一位。
< A HREF =< PHP的echo $光电>源;>中标题=< PHP的echo $光电>名;>中>
< IMG SRC =< PHP的echo $光电>源;>中ALT =< PHP的echo $光电>名;>中/>
&所述; / A>
这些写出来作为阵列 $照片['源']
而不是对象 $光电>源
(是的,我还使用了嵌入式的foreach)
我希望这是有帮助!它完美地跑了我。用下面的输出的一个较长版本
1111111111111111111111111111< BR> < A HREF =https://scontent.xx.fbcdn.net/hphotos-xpf1/v/t1.0-9/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx称号=婚纱>
< IMG SRC =https://scontent.xx.fbcdn.net/hphotos-xpf1/v/t1.0-9/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxALT =婚礼/> &所述; / A> < A HREF =https://scontent.xx.fbcdn.net/hphotos-xpf1/v/t1.0-9/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx称号=>
&所述; IMG SRC =https://scontent.xx.fbcdn.net/hphotos-xpf1/v/t1.0-9/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxALT =/> &所述; / A>
I know questions like this one have been asked many times before. But believe me, I have SEARCHED without getting an answer peculiar to my problem.
I'm trying to get information from a json response I got from facebook graph api, using php.
the response looks like this:
{
"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"
}
The problem is; the response has like 2 levels data arrays. I need to get "source" and "name" from the "photos" "data" array.
This is what I've done:
<?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**/ ?>
I'm able to get information like "id" (which I don't need, by the way) from the "albums" "data" array on the 1st level, but that's as far as I go. What I really need are the information in the "photos" array.
Any help with solving this problem would be really appreciated.
<?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 }
}
?>
For my purposes, I used your example JSON in string format. You'll notice a few print_r's that I used. They are now commented out. I just used them to help find the issue. The problem was in the final bit.
<a href="<?php echo $photo->source; ?>" title="<?php echo $photo->name; ?>">
<img src="<?php echo $photo->source; ?>" alt="<?php echo $photo->name; ?>" />
</a>
These were written out as arrays $photo['source']
instead of objects $photo->source
(And, yes, I still used the embeded foreach)
I hope that's helpful! It ran perfectly for me. With a longer version of the following output:
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>
这篇关于麻烦与Facebook多层次的JSON PHP foreach循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!