The Facebook API lets you post to Pages you administrate via the API. With the release of Graph API v2.x and the new Facebook PHP SDK v4.0.x, I’m updating my old tutorial to show how to achieve the same functionality using the newer Graph API and PHP SDK.
First off, you need to add the manage_pages and publish_actions permissions to your app. This will let the application access the pages you own, and give the application permission to post to these pages. Note that this permission will give an application access to all the pages you own – not just the one you want to use.
Next, you need the access_token for the page you want to post to. This can be done by making a simple API call, as follows:
The API call with return the page
access_token, which will use later on in the tutorial to post to the page.
Once we have the page token, we can then construct a new API call to publish the post:
Along with the API call, you must post at lease one of the following parameters:
- message – the text for the status update, e.g. “Thanks for helping us reach 1.000 fans”.
- link – A URL to a website you want to link to
- picture – URL of an image you want to include in the update
- name – The name of the link attachment
- caption – The caption of the link (appears beneath the link name)
- description – The description of the link (appears beneath the link caption)
Once the API call has been made, Facebook will return the post_id for the update, so you can later get information back on it. If no post_id is included, an error message will be returned letting you know what went wrong. The return data will look like:
The status update on the page itself will look something like.
A full list of properties to use with the /feed API call can be found on Facebook’s Documentation for Feeds.
If you see a error message like the following when attempting to post to the page, you are missing the
publish_actions permission. You won’t be able to publish to the page without this permission. Note that accepting this permission will also give the application access to publish posts to your own timeline.