Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to publish post on facebook page wall as admin using facebook php sdk v4 and graph api 2.x

how to publish post on Facebook page wall as page admin user using facebook php sdk v4 and graph api 2.x?

I have spent lot of hours but mostly articles are old(examples with old Facebook PHP SDK) and bit confusing

I have figured out following steps

Step 1 : Redirect application to Facebook for logging admin
Step 2 : Obtain user access token (short lived)
Step 3 : Obtain user access token(long lived) by exchanging short lived token
Step 4 : Obtain Page access token

Can you explain please which functions of Facebook PHP SDK I need to call for above steps or you can give me url of some code example?

OR

If I am wrong then please correct me Thanks for advance

=======================
Note:

  • I have created application for app_id and app_secret
  • I have created Facebook page
  • I am using Facebook PHP SDK 4
  • Facebook PHP SDK 4 uses graph API 2.x (I assume)
like image 726
Ali Avatar asked Nov 01 '22 18:11

Ali


1 Answers

I have a tutorial that explains how to achieve posting to a page using the PHP SDK v4.0.x and Graph API v.x.

Essentially, you can get a Page Access Token by doing the following:

// get page access token
$access_token = (new FacebookRequest( $session, 'GET', '/' . $page_id,  array( 'fields' => 'access_token' ) ))
    ->execute()->getGraphObject()->asArray();

// save access token in variable for later use  
$access_token = $access_token['access_token'];

Then you can make a second API call to post something to a given page using the access token we obtained above:

// post to page
$page_post = (new FacebookRequest( $session, 'POST', '/'. $page_id .'/feed', array(
    'access_token' => $access_token,
    'name' => 'Facebook API: Posting As A Page using Graph API v2.x and PHP SDK 4.0.x',
    'link' => 'https://www.webniraj.com/2014/08/23/facebook-api-posting-as-a-page-using-graph-api-v2-x-and-php-sdk-4-0-x/',
    'caption' => 'The Facebook API lets you post to Pages you administrate via the API. This tutorial shows you how to achieve this using the Facebook PHP SDK v4.0.x and Graph API 2.x.',
    'message' => 'Check out my new blog post!',
  ) ))->execute()->getGraphObject()->asArray();

// return post_id
print_r( $page_post );
like image 68
Niraj Shah Avatar answered Nov 15 '22 06:11

Niraj Shah