如何从Facebook帐户中提取评论到我的Webapp?

时间:2018-03-08 08:24:39

标签: php facebook-graph-api

我试图通过将结果转换为数组然后打印数组,使用Graph API从Facebook帐户中提取所有评论到我的Webapp,但我得到的结果是一个空白数组,后面是相应的帖子我不知道什么是错的。

if (isset($accessToken)) 
                             {     
echo 'Posts:';
echo '<br>';
// getting all posts published by user
try {
    $posts_request = $fb->get('/me/feed');
} catch(Facebook\Exceptions\FacebookResponseException $e) {
    // When Graph returns an error
    echo 'Graph returned an error: ' . $e->getMessage();
    exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
    // When validation fails or other local issues
    echo 'Facebook SDK returned an error: ' . $e->getMessage();
    exit;
}
}
$total_posts = array();
$posts_response = $posts_request->getGraphEdge();

$ response_array = $ posts_response-&gt; asArray();     if($ fb-&gt; next($ posts_response)){

    $total_posts = array_merge($total_posts, $response_array);
    while ($posts_response = $fb->next($posts_response)) {
        $response_array = $posts_response->asArray();
        $total_posts = array_merge($total_posts, $response_array);
    }
    foreach ($total_posts as $key) {

      if(isset($key['message'])){
       echo $key['message'];
       echo '<br>','<br>';
     }
    }
    //print_r($total_posts);
} else {

    $posts_response = $posts_request->getGraphEdge()->asArray();
    foreach ($posts_response as $key) {

      if(isset($key['message'])){
       echo $key['message'];
       echo '<br>','<br>';
       $o_id= $key['id'];
       echo $o_id;

try {
        // Returns a `Facebook\FacebookResponse` object
        $response = $fb->get('/113932879436074_129461837883178/comments',$accessToken);
    } 
    catch(Facebook\Exceptions\FacebookResponseException $e) 
    {
        echo 'Graph returned an error: ' . $e->getMessage();
        exit;
    } 
    catch(Facebook\Exceptions\FacebookSDKException $e) 
    {
        echo 'Facebook SDK returned an error: ' . $e->getMessage();
        exit;
    }
    $graphNode = $response->getGraphEdge();
    $comments=$graphNode->asArray();
    print_r($comments);

}

Output:

113932879436074_131399157689446Array()#TrueStory 113932879436074_131393101023385Array()下午好,Pihu Jaiswal 113932879436074_1294​​61837883178Array()超级无聊

1 个答案:

答案 0 :(得分:0)

if($ fb-&gt; next($ posts_response)){

    $total_posts = array_merge($total_posts, $response_array);
    while ($posts_response = $fb->next($posts_response)) {
        $response_array = $posts_response->asArray();
        $total_posts = array_merge($total_posts, $response_array);
    }
    foreach ($total_posts as $key) {

      if(isset($key['message'])){
       echo $key['message'];
       echo '<br>','<br>';
     }
    }
    //print_r($total_posts);
} else {

    $posts_response = $posts_request->getGraphEdge()->asArray();
    foreach ($posts_response as $key) {

      if(isset($key['message'])){
       echo $key['message'];
       echo '<br>','<br>';
       $o_id= $key['id'];
       echo $o_id;

try {
        // Returns a `Facebook\FacebookResponse` object

        $response = $fb->get('me?fields=id,name,posts{comments{message}}');
    } 
    catch(Facebook\Exceptions\FacebookResponseException $e) 
    {
        echo 'Graph returned an error: ' . $e->getMessage();
        exit;
    } 
    catch(Facebook\Exceptions\FacebookSDKException $e) 
    {
        echo 'Facebook SDK returned an error: ' . $e->getMessage();
        exit;
    }
    $graphNode = $response->getGraphNode();
    $comments=$graphNode->asArray();
    print_r($comments);

}