通过facebook api“喜欢”循环

时间:2014-02-12 22:41:03

标签: php arrays facebook facebook-php-sdk

嘿所有我想在PHP中获得墙上帖子的所有喜欢。数组框架如下所示:

Array ( 
[data] => Array ( 
    [0] => Array ( 
        [id] => XXXXXXXX_XXXXXXXXXXX 
        [from] => Array ( 
            [name] => Bob Barker
            [id] => XXXXXXXXXXX
        ) 
        [message] => This is a message here!!! 
        [story] => We shared a message with others
        [story_tags] => Array ( 
            [0] => Array ( 
                [0] => Array ( 
                    [id] => XXXXXXXXXX 
                    [name] => Bob Barker
                    [offset] => 0 
                    [length] => 11 
                    [type] => user 
                ) 
            ) 
            [19] => Array ( 
                [0] => Array ( 
                    [id] => XXXXXXXXXXXXXXXXXXXXXXX 
                    [name] => NAME 
                    [offset] => 19 
                    [length] => 5 
                    [type] => page 
                ) 
            ) 
        ) 
        [picture] => [removed] 
        [link] => [removed]
        [name] => Timeline Photos 
        [caption] => This is just a caption for the post here 
        [properties] => Array ( 
            [0] => Array ( 
                [name] => By 
                [text] => NAME 
                [href] => [removed] 
            ) 
        ) 
        [icon] => https://fbstatic-a.akamaihd.net/rsrc.php/v2/yD/r/aS8ecmYRys0.gif 
        [actions] => Array ( 
            [0] => Array ( 
                [name] => Like 
                [link] => [removed]
            ) 
        ) 
        [privacy] => Array ( 
            [value] => 
        ) 
        [type] => photo 
        [status_type] => shared_story 
        [object_id] => XXXXXXXXXXXXXXXX
        [application] => Array ( 
            [name] => Photos 
            [id] => XXXXXXXXXX 
        ) 
        [created_time] => 2014-02-12T21:33:17+0000 
        [updated_time] => 2014-02-12T21:33:17+0000 
        [likes] => Array ( 
            [data] => Array ( 
                [0] => Array ( 
                    [id] => XXXXXXXXXXXX 
                    [name] => John Doe 
                ) 
                [1] => Array ( 
                    [id] => XXXXXXXXXXXX 
                    [name] => Steve Doe 
                ) 
            ) 
            [paging] => Array ( 
                [cursors] => Array ( 
                    [after] => XXXXXXXXXXXXXXXX 
                    [before] => XXXXXXXXXXXX== 
                ) 
            ) 
        ) 
    ) 

我目前的PHP代码是:

$feed = $facebook->api('/me/home'); //This is the array above

foreach($feed['data'] as $post) {
  $story = (isset($post['story']) ? $post['story'] : null);
  $id = (isset($post['id']) ? $post['id'] : null);
  $name = (isset($post['name']) ? $post['name'] : null);
  $message = (isset($post['message']) ? $post['message'] : null);
  $post_link = (isset($post['actions'][0]['link']) ? $post['actions'][0]['link'] : null);
  $picture = (isset($post['picture']) ? $post['picture'] : null);

  foreach($post['likes'] as $liked) {
      echo $post['id'];
  }

  echo $name . ' ' . $story . ' it was: ' . $message . ' and ' . $post_link .  ' and <img src="' . $picture . '" /><br />';
  echo '=======================================================================================================';
}

foreach($post['likes'] as $liked) {
      echo $post['id'];
}

似乎没有按原样运作。为了让它循环遍历数组中所有“喜欢”的帖子,我缺少什么?

2 个答案:

答案 0 :(得分:1)

foreach($post['likes'] as $liked) {
      echo $post['id'];
}

以后

无效

$post['likes']元素又是一个嵌套数组

likes => array => data => array=> ..

所以你需要做循环

if(array_key_exists('data',$post['likes'])){
        foreach($post['likes']['data'] as $liked=>$val) {
            echo '<br />Like ID: ' .$val["id"].'<br />' ;
        }
    }

最好在执行循环之前检查数组键是否存在,这样如果返回的数据不包含键,则不会得到未定义的索引错误。

答案 1 :(得分:0)

尝试

foreach($post['likes']->data as $like) {
  echo $like->id;
}