无法使用JSON解码处理foreach级别

时间:2015-09-19 05:30:55

标签: php json facebook foreach

我一直在处理这个问题大约5个小时,所以我觉得是时候在这里问了。

我正在使用Facebook Graph API检索数据,并使用JSON解码将所有数据放在PHP上。

这是FB图:

{
  "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的完美输出,但是我带有$附件我收到以下错误:

  • 注意:未定义的属性:stdClass :: $ attachments in ...
  • 注意:尝试获取非对象的属性...
  • 警告:为......中的foreach()提供的参数无效

我已经读过这个:Trouble with Facebook multi-level json php foreach loop,但没有运气。

我该如何解决这个问题?非常感谢!

2 个答案:

答案 0 :(得分:1)

链接对象时需要小心 - 特别是在循环中。一个空的物体将打倒整个节目。试试这个:

$counter = 0;
if( isset( $d->attachments ) )
{
    foreach( $d->attachments->data as $d2 )
    {
        ....
    }
}

答案 1 :(得分:1)

 Notice: Undefined property: stdClass::$attachments in...
 Notice: Trying to get property of non-object in...
 Warning: Invalid argument supplied for foreach() in...

您收到此错误,因为您在第一个循环中缺少Feed对象   这里DEMO

替换

foreach($data->data as $d) {
if($counter==$limit)
break;
?>

。通过

 foreach($data->feed->data as $d) {
    if($counter==$limit)
    break;
    ?>

您的JSON采用此格式粘贴您的JSON JSON Format Viewer并检查JSON DATA

我尝试了你的代码,我可以在这里打印网址DEMO

$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);
   }
}

旁注:你正在宣布$ counter = 0;内循环两次 并且在循环之外它很糟糕即使对于$附件,我的看法是在第一次查看你的代码后无论你有什么理由支持它