Simple Example to Post to a Facebook Fan Page Via PHP

Simple example to post to a Facebook fan page via PHP?

Finally, after a lot of tests, it worked, without the PHP SDK. This is the step by step guide:

1. Get permissions and the page token

Go to https://developers.facebook.com/tools/explorer/ and select your app from the first drop down menu, in the left.

Click on the button "Get access token", and in the "Select Permissions" window, click in "Extended Permissions" and check manage_pages and publish_stream, and click in "Get Access Token" blue button.

You may be asked in this step to grant permissions to your app to access to your Facebook account, accept.

Next, click at the end of the text field next to the "GET" drop down, and replace the numbers for: me/accounts, and click in the blue button next to this text field.

You'll get the tokens for all your pages, including your app page. Find your page name in the list, will look like this: "name": "Your page name"

When you located your page, copy the access token for the page (will be really long), that can look like this: "access_token": "XXXXXXXX". Also copy the id of the page: "id": "XXXXX".

That's all for this step, we can start coding now.

2. Post to your page wall via PHP

First, for this script, you'll need a server supporting curl.

We start the PHP document defining the page access token and the page id that we've get in the 1st step:

<?php
$page_access_token = 'XXXXXXX';
$page_id = 'YYYYYYYY';

After that, we create an array with the info to post to our page wall:

$data['picture'] = "http://www.example.com/image.jpg";
$data['link'] = "http://www.example.com/";
$data['message'] = "Your message";
$data['caption'] = "Caption";
$data['description'] = "Description";

You can of course, use any other post parameter described in https://developers.facebook.com/docs/reference/api/post/ and if you don't need one or many of the parameters above you can simply delete it.

Ok, At this point we add to the array the access token:

$data['access_token'] = $page_access_token;

And we set our post URL, to post in our page:

$post_url = 'https://graph.facebook.com/'.$page_id.'/feed';

And the last step, we'll use a curl to post our message in our page wall:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$return = curl_exec($ch);
curl_close($ch);
?>

After that, we can save our PHP document, and try to execute it. The post may appear in our Facebook page.

Hope this code helps to other people with the same problem!

How to Post to My Facebook Fan Page using PHP and Graph API?

After becoming familiar with the Graph API check out Facebook's documentation for Pages
http://developers.facebook.com/docs/reference/api/page/

Here is the main page for Facebook's Graph API documentation
http://developers.facebook.com/docs/api

For PHP Development, check out this SDK, It will make your life much easier for interacting with Facebook using PHP
https://github.com/facebook/php-sdk/

Here is a link for how to configure your Facebook APP to be able to post on a Facebook Page
http://blog.theunical.com/facebook-integration/5-steps-to-publish-on-a-facebook-wall-using-php/
(I did not follow this exact tutorial but slaved to figure it out myself, so let me know how it works for you)

note As I mentioned in a below comment make special note of the permanent session key.

Get latest Facebook posts of page with PHP SDK

You need to use a long-lived page access-token.

Page Access Token


These access tokens are similar to user access
tokens, except that they provide permission to APIs that read, write
or modify the data belonging to a Facebook Page. To obtain a page
access token you need to start by obtaining a user access token and
asking for the manage_pages permission. Once you have the user access
token you then get the page access token via the Graph API.

As @CBroe said, you should not use that access token in client-side code as it is secret/private and you don't want anyone to get it.

So for what you want to do, Javascript is not the right choice. You will have to use some server-side code, like PHP, Python or Ruby to get the posts. If that is clear, here is how you can create it.


  1. Create a Facebook app:

    • keep the app id (1) and app secret (2) aside,
    • in the "advanced" settings, activate OAuth in order to avoid The application has disabled OAuth client flow.
  2. You need to create a user access token.

    • go on the Graph API Explorer and select the application you just created,
    • generate an access token: click "Get access token" and tick manage_pages in the "Extended Permissions" tab.
  3. Get your short-lived page access token.

    • still in the Graph API explorer, query me/accounts (GET),
    • find your page and get its access token (3).
  4. Get your long-lived page access token.

    • in your browser, paste https://graph.facebook.com/oauth/access_token?client_id=(1)&client_secret=(2)&grant_type=fb_exchange_token&fb_exchange_token=(3) in the address bar,
    • replace (1), (2) and (3) by your app id, your app secret and your page access token,
    • get your new long-lived access token from the result: access_token=FAKECAALBygJ4juoBAJyb8Cbq9bvwPYQwIaX53fLTMAWZCmDan1netI30khjITZAqcw9uE0lRT4ayWGm2ZCS7s7aZCVhF3ei6m0fuy2AkTkwmzUiJI4NUOZAyZAzuL,
    • use the Access Token Debugger to verify that your access token will never expire.

Now you can use that new access token to retrieve the posts of your page:

$session = new FacebookSession('FAKECAALBygJ4juoBAJyb8Cbq9bvwPYQwIaX53fLTMAWZCmDan1netI30khjITZAqcw9uE0lRT4ayWGm2ZCS7s7aZCVhF3ei6m0fuy2AkTkwmzUiJI4NUOZAyZAzuL');

try {
$data = (new FacebookRequest(
$session, 'GET', '/me/posts'
))->execute()->getGraphObject()->getPropertyAsArray("data");

foreach ($data as $post){
$postId = $post->getProperty('id');
$postMessage = $post->getProperty('message');
print "$postId - $postMessage <br />";
}
} catch (FacebookRequestException $e) {
// The Graph API returned an error
} catch (\Exception $e) {
// Some other error occurred
}


Related Topics



Leave a reply



Submit