麻烦与Facebook多级json php foreach循环

时间:2015-03-03 02:17:16

标签: php arrays json facebook facebook-graph-api

我知道这样的问题之前已被问过多次。但是相信我,我已经搜索过而没有得到我的问题所特有的答案。

我正在尝试从facebook图表api获得的json响应中获取信息,使用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级数据阵列。 我需要从“照片”“数据”数组中获取“源”和“名称”。

这就是我所做的:

<?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”的信息(顺便提一下,我不需要这样的信息),但这就是我的目标。 我真正需要的是“照片”数组中的信息。

任何帮助解决这个问题的人都会非常感激。

1 个答案:

答案 0 :(得分:0)

<?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>