Site icon Web Niraj

Facebook API: Posting As A Page using Graph API v2.x and PHP SDK 4.0.x

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:

See the gist on github.

The API call with return the page ID and 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:

See the gist on github.

Along with the API call, you must post at lease one of the following parameters:

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:

See the gist on github.

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.

See the gist on github.

Exit mobile version