Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

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!

share|improve this question

2 Answers 2

up vote 34 down vote accepted

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 garant 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.

  1. 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!

share|improve this answer
4  
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? –  modernclix Jan 3 '13 at 12:03
    
same problem with token expiration. how can i automatically post to a fan page? –  AldoB May 2 '13 at 19:24
    
This is working on hosted website but not from local is any solutions for that? –  Nikunj Dhimar Sep 4 '13 at 6: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 2:40
1  
I got an answer to the extended token here if anyone is interested. –  square_eyes Jan 3 '14 at 12:58

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.

share|improve this answer
    
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 –  Lix Oct 19 '11 at 11:01
    
yes, i already asked for it –  nmarti Oct 19 '11 at 11:10
    
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 3 '14 at 0:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.