The Facebook Documentation for PHP SDK 4.0 is basic at the moment, and doesn’t fully document common uses of the API. Therefore, I’m rewriting old tutorials to work with the new PHP SDK 4.0 and Graph API 2.0. This tutorial covers how to post a status update to the user’s timeline in the background using the API.
First off, you need to add the publish_actions
permission to your app to allow automatic posting of text, links, or photos to the timeline. This permission will need to be approved by Facebook before your users will be able to approve it. Once approved, your users will see a dialog similar to the one below:
In order to post a status update to the timeline, we must make a POST
call to the /{user_id}/feed
API endpoint. This endpoint remains unchanged from Graph API v1.0. The user must be logged in and have a valid access_token
for the API call to work.
For the logged in user, the endpoint we need is below. At the minimum, we need to include the message
parameter and access_token
. At the minimum, the message
parameter should be passed.
If successful, Facebook will return an ID for the post, which is made up of your User ID and the ID for the post.
Array ( [id] => 60506094_10100387108404056 )
If you want to post images, links etc, you can include additional parameters in your API call. The following are just a few parameters you can use:
message
– the text for the status update, e.g. “TGI Friday!”.link
– A URL to a website you want to link topicture
– URL of an image you want to include in the updatename
– The name of the link attachmentcaption
– The caption of the link (appears beneath the link name)description
– The description of the link (appears beneath the link caption)
For example, if I wanted to post a link to this article, my data would look like:
The status update 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.