Php Can';t使用JSON解码处理foreach级别
我已经处理这个问题大约5个小时了,所以我想是时候在这里提问了 我使用FacebookGraphAPI检索数据,并使用JSON解码将其全部放在PHP上 以下是FB图:Php Can';t使用JSON解码处理foreach级别,php,json,facebook,foreach,Php,Json,Facebook,Foreach,我已经处理这个问题大约5个小时了,所以我想是时候在这里提问了 我使用FacebookGraphAPI检索数据,并使用JSON解码将其全部放在PHP上 以下是FB图: { "feed": { "data": [ { "message": "A file.", "id": "831407506978898_831408573645458", "attachments": { "data": [
{
"feed": {
"data": [
{
"message": "A file.",
"id": "831407506978898_831408573645458",
"attachments": {
"data": [
{
"target": {
"id": "1041214692589250",
"url": "https://www.facebook.com/download/A-PDF-FILE.pdf"
},
"title": "Clase 01 - Vías de administración.pdf",
"type": "file_upload",
"url": "https://www.facebook.com/download/A-PDF-FILE.pdf"
}
]
}
},
{
"picture": "https://fbcdn-photos-c-a.akamaihd.net/A-PHOTO.jpg",
"message": "A photo.",
"id": "831407506978898_831408496978799",
"attachments": {
"data": [
{
"description": "A photo.",
"media": {
"image": {
"height": 540,
"src": "https://fbcdn-photos-c-a.akamaihd.net/A-PHOTO.jpg",
"width": 720
}
},
"target": {
"id": "10207838160017396",
"url": "https://fbcdn-photos-c-a.akamaihd.net/A-PHOTO.jpg"
},
"type": "photo",
"url": "https://fbcdn-photos-c-a.akamaihd.net/A-PHOTO.jpg"
}
]
}
},
{
"picture": "https://fbcdn-photos-c-a.akamaihd.net/A-PHOTO.jpg",
"id": "831407506978898_831408450312137",
"attachments": {
"data": [
{
"media": {
"image": {
"height": 540,
"src": "https://fbcdn-photos-c-a.akamaihd.net/A-PHOTO.jpg",
"width": 720
}
},
"target": {
"id": "10207838168217601",
"url": "https://fbcdn-photos-c-a.akamaihd.net/A-PHOTO.jpg"
},
"type": "photo",
"url": "https://fbcdn-photos-c-a.akamaihd.net/A-PHOTO.jpg"
}
]
}
},
{
"message": "TEST",
"id": "831407506978898_831407576978891"
},
{
"id": "831407506978898_831407516978897"
}
],
"paging": {
"previous": "https://graph.facebook.com/...alotofjunk"
}
},
"id": "0000000000000"
}
我的PHP如下所示:
<?php
header('Content-Type: text/html; charset=utf-8');
$limit = 60; // The number of posts fetched
$access_token='TOKEN NUMBER';
$group_id = 'GROUPNUMBER';
$url1 = 'https://graph.facebook.com/'.$group_id.'?access_token='.$access_token;
$des = json_decode(file_get_contents($url1)) ;
$url2 = "https://graph.facebook.com/{$group_id}/feed?access_token={$access_token}";
$data = json_decode(file_get_contents($url2));
?>
<?
$counter = 0;
foreach($data->data as $d) {
if($counter==$limit)
break;
?>
<? $themessage = (isset($d->message) ? $d->message : false); ?>
<? print $themessage ?>
<? $thepicture = (isset($d->picture) ? $d->picture : false); ?>
<? print "<img src=\"$thepicture\">" ?>
<!--THE PROBLEM IS FROM HERE.... -->
<?
$counter = 0;
foreach($d->attachments->data as $d2) {
if($counter==$limit)
break;
?>
<? $attachments = (isset($d2->url) ? $d2->url : false); ?>
<? print $attachments ?>
<?
}
?>
<!-- ...TO HERE -->
<?
$counter++;
}
?>
我获得了$themessage和$thepicture的完美输出,但如果使用$attachments,我会收到以下错误:
- 注意:未定义的属性:stdClass::$attachments in
- 注意:正在尝试获取
- 警告:为中的foreach()提供的参数无效
我怎样才能解决这个问题?。非常感谢 链接对象时需要小心,尤其是在循环中。一个空的物体会把整个表演打倒。试试这个:
$counter = 0;
if( isset( $d->attachments ) )
{
foreach( $d->attachments->data as $d2 )
{
....
}
}
出现此错误是因为在第一个循环中缺少提要对象
这里更换
foreach($data->data as $d) {
if($counter==$limit)
break;
?>
与
foreach($data->feed->data as $d) {
if($counter==$limit)
break;
?>
您的JSON是这种格式的。请将您的JSON粘贴到此处并进行检查
我试过你的代码我可以在这里打印url
$data=“您的JSON在此”
foreach($data->feed->data as$d){
$themessage=(设置为($d->message)?$d->message:false);
打印(“\n”。$themessage);
$thepicture=(设置为($d->picture)?$d->picture:false);
打印(“\n”);
foreach($d->附件->数据为$d2){
$attachments=(isset($d2->url)?$d2->url:false);
打印(“\n”。$附件);
}
}
旁注:您正在初始化$counter=0;两次一次内循环
在循环之外,即使是$attachment也是不好的,这是我在第一次查看代码后的看法,无论您是出于什么原因,它都可以运行良好!。谢谢兄弟!
foreach($data->feed->data as $d) {
if($counter==$limit)
break;
?>
$data="Your JSON Here"
foreach($data->feed->data as $d) {
$themessage = (isset($d->message) ? $d->message : false);
print("\n".$themessage);
$thepicture = (isset($d->picture) ? $d->picture : false);
print("\n<img src='$thepicture'>");
foreach($d->attachments->data as $d2) {
$attachments = (isset($d2->url) ? $d2->url : false);
print("\n".$attachments);
}
}