使用cURL PHP / Graph API在Facebook上发表评论回复

时间:2011-04-22 18:08:44

标签: php facebook curl facebook-graph-api feed

我知道如何在朋友的墙上张贴Feed。例如:

$url = 'https://graph.facebook.com/' . $fbId . '/feed';

$attachment =  array(
        'access_token'  => $accessToken,
        'message'       => $msg,
        'name'          => $name,
        'link'          => $link,
        'description'   => $desc,
        'picture'       => $logo,
);

// set the target url
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$go = curl_exec($ch);
curl_close ($ch);

$go = explode(":", $go);
$go = str_ireplace('"', '', $go[1]);
$go = str_ireplace('}', '', $go);
return $go;

但我想知道,如何使用cURL PHP或Facebook Graph API发布对特定Feed的回复。有人可以帮我解决这个问题吗?

4 个答案:

答案 0 :(得分:4)

好的,首先,这是提取id的更好方法:

$go = json_decode($go, TRUE);
if( isset($go['id']) ) {
// We successfully posted on FB
}

所以你会使用类似的东西:

$url = 'https://graph.facebook.com/' . $fbId . '/feed';

$attachment =  array(
        'access_token'  => $accessToken,
        'message'       => "Hi",
);

// set the target url
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$go = curl_exec($ch);
curl_close ($ch);

$go = json_decode($go, TRUE);
if( isset($go['id']) ) {
    $url = "https://graph.facebook.com/{$go['id']}/comments";

    $attachment =  array(
            'access_token'  => $accessToken,
            'message'       => "Hi comment",
    );

    // set the target url
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $comment = curl_exec($ch);
    curl_close ($ch);
    $comment = json_decode($comment, TRUE);
    print_r($comment);
}

enter image description here

答案 1 :(得分:3)

使用

FB.api('/[POST_ID]/comments', 'post', { 'message' : 'comment post' }); 

确保您拥有publish_stream权限。

答案 2 :(得分:2)

你试过这个:

https://graph.facebook.com/" . $go . "/comment

我认为,如果您可以使用/feed发布Feed,则可以使用/comment网址发布评论。

谢谢。

答案 3 :(得分:0)

我没试过,但你应该尝试以下方法:

  1. 获取您刚刚发布的帖子的ID。检查是否有任何值图表api返回。 如果没有,你可以从“https://graph.facebook.com/”的“id”字段中获取id。$ fbId。“/ feed”。

  2. 使用FB.api('/ [POST_ID] / comments','post',{'message':'comment post'}); 确保你拥有publish_stream特权。