dongliuzhuan1219 2014-02-12 22:41
浏览 23
已采纳

通过facebook api“喜欢”循环

Hey all i am trying to get all the likes for a wall post in PHP. The array frame looks like this:

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== 
                ) 
            ) 
        ) 
    ) 

My current PHP code is:

$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 '=======================================================================================================';
}

The

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

doesnt seem to be working as-is. What am i missing in order to make it loop through all the "liked" posts in the array?

  • 写回答

2条回答 默认 最新

  • dqol6556 2014-02-13 08:01
    关注

    Yes

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

    will not work since

    $post['likes'] elements are again a nested array as

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

    So you need to do the loop as

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

    It is always better to do a check if the array key exists before doing the loop, so that if the returned data does not contain the key you do not get undefined index error.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据