How to post to a Facebook page with PHP

Lari13 picture Lari13 · Feb 10, 2012 · Viewed 11.8k times · Source

I would like to post to my own Facebook page's wall from my website using PHP.

I have the following:

  • Facebook Application with AppID, AppSecret, ApiKey
  • Facebook Page with PageID
  • my own Facebook account - I'm the admin and the creator of the application and page mentioned above.

E.g. I write a blog post, and I'd like to get the name, the short description and a picture on my Facebook page's wall. Or I would like to publish some articles on the Facebook page every day automatically as a cron job.

Could you provide a step-by-step tutorial how to accomplish this?

I've read this article about Facebook Login:
https://developers.facebook.com/docs/facebook-login/
but I still don't know what to write in my code.


UPDATE 1

This is how I send a request for an App Access Token:

$url = 'https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id='
        .Yii::app()->params['FacebookAppID']
        .'&client_secret='
        .Yii::app()->params['FacebookSecret'];

The response is similar to this (fake symbols):

access_token=326584076429|ax3-D39YbpDcR9rMRQn_fMvNu_s

What access_token is it? Application Access Token? How to get a User Access Token?

I tried to use the access token from here:
https://developers.facebook.com/tools/explorer?method=GET&path=me%2Faccounts but I got the following error message:

An active access token must be used to query information about the current user

So how should I obtain the right access token?

UPDATE 2:

How can I get the right Facebook tokens in my application without any client interaction?
I'm the admin and the creator of the Facebook Application and the Facebook Page.

Answer

DMCS picture DMCS · Feb 10, 2012

Step by step

  1. Authenticate a user that is a page admin (yourself)
  2. Request an extended access token (to get a 60 day variety as offline_access is gone). See https://developers.facebook.com/docs/offline-access-deprecation/
  3. Call Graph API me/accounts and search thru the resulting list to find the page you're interested in
  4. Take the page access token from the page and start using that for the calls to post
  5. It might be possible to get an extended access token for a page access token like described in step 2, please try and let us know if that can be done for page access token too.

You can experiment with the above at https://developers.facebook.com/tools/explorer

Happy Coding!

EDIT

For getting an access token without dialogs for any user, you can use https://developers.facebook.com/tools/access_token/ to get an access token.