49

I've done a lot of searching and I've found outdated tutorials that don't work...

I have a site made with PHP and when I submit a particular form in my admin area, I want to publish to my Facebook "fan page"

There is no RSS available, so do you have any example to directly post to the Facebook fan page (not user wall) using php sdk?

Thank you!

Ali Sheikhpour
  • 10,475
  • 5
  • 41
  • 82
nmarti
  • 1,565
  • 3
  • 14
  • 19

4 Answers4

95

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!

dikirill
  • 1,873
  • 1
  • 19
  • 21
nmarti
  • 1,565
  • 3
  • 14
  • 19
  • 7
    If I inspect the details of the token it says that it lasts only 60 minutes. How do I get a one time token so my php script can always post automatically to a fan page? – enbits Jan 03 '13 at 12:03
  • 1
    same problem with token expiration. how can i automatically post to a fan page? – AldoB May 02 '13 at 19:24
  • This is working on hosted website but not from local is any solutions for that? – Nikunj Dhimar Sep 04 '13 at 06:32
  • Great tutorial. It works! Although mine timed out also. I had to re-request the page access token. How can we avoid constantly having to re-authorize? – square_eyes Dec 18 '13 at 02:40
  • 4
    I got an answer to the extended token [here](http://stackoverflow.com/a/20894578/1901781) if anyone is interested. – square_eyes Jan 03 '14 at 12:58
  • Is there a way to check whether the post is succesfull? ie if the token has gotten out of date? – maxisme Jun 19 '14 at 12:24
  • 1
    in answer to my question: https://developers.facebook.com/blog/post/2011/05/13/how-to--handle-expired-access-tokens/ – maxisme Jun 19 '14 at 15:04
  • How can you get the access token programatically? – Cristhian Boujon Nov 17 '15 at 02:38
  • Perfect but.. post generated on fb page has a by default title `Home` how to change that title? – Hirdesh Vishwdewa Mar 08 '16 at 17:52
  • I am trying to follow this example but do not understand what I am to do in this step: "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." - What am I to replace with what? All I see is the GET with me?fields=id,name... – Mike Kogan Apr 12 '16 at 18:01
  • You may need to add these: `curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);` `curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);` – Nabi K.A.Z. Oct 26 '16 at 23:37
  • I was so confused to get access tokens with permissions, but your 1st step really helped me understand things easily and I achieved what I wanted. Thanks. – yuri1000 Apr 12 '17 at 22:47
  • i am not getting passed "picture" parameter correctly. its giving me default image of the shared link. can anyone suggest me what to do now? – prashant Aug 16 '17 at 07:16
  • 1
    You deserve glory. I've wasted 5 hours till i found your post. The documentation out there is either terrible or outdated. Thank you for this evergreen solution –  Oct 07 '17 at 22:51
  • Is this guide still valid/working at today, march 2020? – itajackass Mar 12 '20 at 21:26
4

You can test tokens using Facebook Access Token Debugger

Working solution for API v.2.5

  1. Get code for app_id as parameter of response_uri

https://www.facebook.com/dialog/oauth?client_id=".$app_id."&redirect_uri=".$response_uri."&response_type=code&scope=manage_pages,publish_pages

  1. Get access_token based on code, app_id and app_secret as result of response_uri

https://graph.facebook.com/oauth/access_token?grant_type=authorization_code&client_id=".$app_id."&client_secret=".$app_secret."&code=".$code."&redirect_uri=".$response_uri

  1. Get never expiring page_access_token for page_id based on access_token

https://graph.facebook.com/v2.5/".$page_id."?fields=access_token&access_token=".$access_token

bq23
  • 41
  • 1
  • 5
  • Hey @bq23, I just tried your solution but I keep getting `Error validating verification code. Please make sure your redirect_uri is identical to the one you used in the OAuth dialog request` on step 2. Any ideas what I would be doing wrong ? The code response has a lot of special characters, it might come from there. – Jonathan Taws Mar 02 '16 at 21:54
  • 1
    @bq23 Can you explain, what response_uri exactly means ? – Wolfgang Blessen Mar 18 '17 at 09:22
2

As an addition to nmarti answer. Valid for API v.2.4.

If you don't want to go Facebook API console, rather do API calls, there are some instructions.

First of all, you have to have Facebook user, being admin on the page you want to post, also you have to create Facebook App in order to proceed.

  1. Do login request, to get user token:

https://www.facebook.com/dialog/oauth?client_id=%app-id%&redirect_uri=%your-site-url%&response_type=token&scope=manage_pages,publish_pages

In response, you should get %user-token%, save it, you will need in the next step.

Read More

  1. Ask for long lived token:

https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id=%app-id%&client_secret=%app-secret%&fb_exchange_token=%user-token%

Now you will have %long-lived-token%, required to get long lived page token.

Read More

  1. Now, get a list of your Facebook Pages,

https://graph.facebook.com/v2.4/%page-admin-user-id%/accounts/?access_token=%long-lived-token%

Find in the list your Page, and a page token, now you can continue with posting to page using nmarti example.

Also Facebook says:

The resulting page access token will not have any expiry time.

Read More

Fabio Poloni
  • 8,219
  • 5
  • 44
  • 74
dikirill
  • 1,873
  • 1
  • 19
  • 21
0

Here is the resource you are looking for. Scroll down to Page Login and read from there. You have to get an access token for your page and then use that token when posting. This is assuming that you want your post to appear "from the page". IE - posting as if you were the page.

the actual call to the graph api to create a post object, and how to do it, can be found at this url from the facebook documentation.

Lix
  • 47,311
  • 12
  • 103
  • 131
  • I have the page token but i can not post to the wall, i'm using this code: require_once 'src/facebook.php'; $facebook = new Facebook(array( 'appId' => 'XXXXXXX', 'secret' => 'XXXXXXXX', )); $page_token = 'XXXXXXX'; $page_id = 'XXXXXXX'; $message = 'test'; $facebook->setAccessToken($page_token); $ret_obj = $facebook->api('/XXXXXXX', 'POST', array('message' => $message,)); – nmarti Oct 19 '11 at 10:49
  • maybe i should have asked first :P but have you requested the `publish_stream` permission for the user? [Facebook Permissions](https://developers.facebook.com/docs/reference/api/permissions/) – Lix Oct 19 '11 at 11:01
  • Also any idea how to extend the token? This way I have to recreate one with the graph explorer every 60 minutes or so. – square_eyes Jan 03 '14 at 00:20