📜 ⬆️ ⬇️

Automatic addition, removal of posts and comments to them on Facebook

Good day to all.

I want to share the experience of automatically adding and then deleting news, as well as comments to them on the pages of users, groups, events and fans. Facebook.com pages.

For implementation, we need PHP with the curl module connected, PHP Facebook SDK link .

I was assigned the task: from admin. site panels add news to the corresponding group in VKontakte and fan. Facebook page.
')
Dealing with the contact was not difficult. This article and this one helped. Only I ran into such a bug or restriction: my wall.post method does not return post_id , although it is written in the documentation that it should.

But this is the topic of another article. Now I want to consider step by step how to add news to Facebook pages.

Create an application on Facebook



Follow the link , create a new application (in the upper right corner).
After creating the application we get:
Appid
App Secret
Do not forget below to specify the App Domain and Website corresponding to the site from which news will be published.

Add news to Facebook page




require '../src/facebook.php';

define('FACEBOOK_APP_ID',"123456"); // AppId
define('FACEBOOK_SECRET',"123456"); // App Secret
define('PAGE_ID',"123456"); // (, , )
define('UID',"123456"); //

$user = null;

$facebook = new Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_SECRET,
'cookie' => true
));

$user = $facebook->getUser(); // UID , 0.

/**
* - redirect_uri: URL
* - scope:
*/
if($user == 0) {
$login_url = $facebook->getLoginUrl($params = array('scope' => 'manage_pages,offline_access,publish_stream, read_stream'));

// $login_url

}

$params = array(
'name' => “My title”,
'href' => “http://www.mysite.ru”,
'description' => “Description”
);
$message = “Message”; //
$attachment = urlencode(json_encode($params)); //

$url = 'https://api.facebook.com/method/stream.publish?message='.$message.'&attachment='.$attachment.'&target_id='.PAGE_ID.'&uid='.UID.'&access_token='.$facebook->getAccessToken();

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, "");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch, CURLOPT_URL, $url);

$res = curl_exec($ch);
curl_close($ch);



If the request is successful, Facebook will return the record identifier, if the JSON string fails, with an error. We will keep the identifier where it is convenient for us to continue to use.
Approximate message type:

image

Parameters in the attachment variable are passed in JSON format . You can transfer not only text data, but also media (pictures, flash, music). More details can be found here .

Deleting news



define('POST_ID',"12345"); // ,
define('UID',"12345"); // ,

$url = 'https://api.facebook.com/method/stream.remove?post_id='.POST_ID.'&uid='.UID.'&access_token='.$facebook->getAccessToken();

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, "");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch, CURLOPT_URL, $url);

$res = curl_exec($ch);
curl_close($ch);


In case of success, it returns 1 or true , otherwise - a string in JSON format .

Add comments to the news



define('POST_ID',"12345"); //

$url = 'https://graph.facebook.com/'.POST_ID.'/comments';

$attachment = array(
'access_token' => $facebook->getAccessToken(),
'message' => "Hi",
);

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

$res = curl_exec($ch);
curl_close ($ch);


If successful, the query returns the comment ID , if the string is in the wrong JSON format . The comment ID can be used to delete it:

define('COMMENT_ID',"12345"); //
define(UID',"12345"); // ,

$url = 'https://api.facebook.com/method/stream.removeComment?comment_id='.COMMENT_ID.'&uid=123&access_token='. $facebook->getAccessToken()

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, "");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch, CURLOPT_URL, $url);

$res = curl_exec($ch);
curl_close ($ch);


In case of success, it returns 1 or true , otherwise - a string in JSON format .

View all comments on the news.



//
$url = 'https://graph.facebook.com/'.POST_ID.'?access_token='.$facebook->getAccessToken();
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, "");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch, CURLOPT_URL, $url);

$result = curl_exec($ch);
curl_close($ch);


In response, we get either a JSON format string with data about comments, or a string with errors.

Materials used



Source: https://habr.com/ru/post/129597/


All Articles