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

how to post a message on the wall of a facebook page as a user of the admin page using facebook php sdk v4 and api 2.x graph?

I spent many hours, but mostly the articles are old (examples with the old PHP PHP SDK) and are a bit confusing

I figured out the following steps

Step 1: redirect the application to Facebook to login admin
Step 2: Get the user's access token (short-lived)
Step 3: Get a user access token (long-lived) by exchanging a short-lived token
Step 4: Get Page Access Token

Can you explain what functions of the Facebook PHP SDK do I need to call for the above steps, or can you give me some sample code?

OR

If I'm wrong, please correct me. Thanks for the promotion.

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

  • I created an application for app_id and app_secret
  • I created a Facebook page
  • I am using PHP PHP SDK 4
  • Facebook PHP SDK 4 uses graph API 2.x (suppose)
+5
source share
2 answers

I have a tutorial that explains how to achieve publication on a page using PHP SDK v4.0.x and Graph API vx

Essentially, you can get the 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 send something to the given page using the access token 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 ); 
+2
source

I find another way to get an expired token:

  //get your access token for the app that you will use in //https://developers.facebook.com/tools/explorer/ //exchange it for long-term access token $accessToken='xxxxxxxxGet it above'; $session = new FacebookSession($accessToken); $response = (new FacebookRequest($session, 'POST', '/oauth/access_token', array( 'grant_type' => 'fb_exchange_token', 'client_id' => 'the client id for the app', 'client_secret' => 'the client secret for the app', 'fb_exchange_token' => $accessToken)))->execute()->getGraphObject()->asArray(); $LongTermUserAccessToken=$response['access_token']; $session = new FacebookSession($LongTermUserAccessToken); //get My User ID if you don't know $response = (new FacebookRequest( $session, 'GET', '/me' , array( 'fields' => 'id' ) )) ->execute()->getGraphObject()->asArray(); $meId=$response['id']; //get Tokes for all my pages the page that you are usig will have a never expire token //you can prove on https://developers.facebook.com/tools/debug/ $response = (new FacebookRequest( $session, 'GET', '/'.$meId.'/accounts' )) ->execute()->getGraphObject()->asArray(); var_dump($response);die; 
0
source

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


All Articles