Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post a reply on a comment in Facebook using cURL PHP / Graph API

I know how to post a feed on the friend's wall. eg:

$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;

But I want to know, how to post a reply to the particular feed using cURL PHP or Facebook Graph API. Can anybody help me out from this problem?

like image 948
Ketan Modi Avatar asked Dec 27 '22 21:12

Ketan Modi


1 Answers

Okay, first of all, this is a better way of extracting the id:

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

So you would use something like:

$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

like image 97
ifaour Avatar answered Dec 30 '22 11:12

ifaour