How to update your Facebook Status with PHP

Believe it or not, this is actually one of the trickiest things you can do with PHP.

There are a couple of reasons for this.

1. Facebook is pretty anal about security, and really, really, really wants only logged in users to update user accounts. This is the complete opposite of Twitter, who really, really, really want all sorts of apps and 3rds parties to able to update Twitter statuses.

2. The Facebook Documentation Glop: Facebook has moved through various iterations of its API over the last 5 years, but they’ve left all the documentation for the old APIs in place, so its pretty easy to get confused. This in turn has led to lots of people offering solutions on blogs etc, but quite often these refer to the old APIs, so finding any sort of a solution via a Search Engine is all but impossible.

Anyway, here’s the meat.

The key to this is to use the Facebook Graph API. This is a really unfortunate name for the API, as it suggests something to do with dynamic graphing. In fact, its a really simple API that allows you do all sorts of stuff with FB by sending HTTP POST requests, which is the method us developers love the most.

Step 1:

Add the Developer Application to your FB account

Step 2:

Create a new application, called anything you want. There’s a bundle of info you can add for your app, but the basic stuff you need is:

Profile->Tab URL

eg http://apps.facebook.com/garrethsstatus/

Canvas->Canvas Page URL

eg http://apps.facebook.com/garrethsstatus/

Canvas->Canvas Callback URL

(This can be anything, provided it is a valid URL eg http://www.nightbluefruit.com/; you must include trailing slash)

Connect->Connect URL

(This can be anything, provided it is a valid URL eg http://www.nightbluefruit.com/; you must include trailing slash)

Save and you are done with the application bit.

Step 3

We now need to tell Facebook that we want to allow this application do stuff with our account, and obtain an access token that we can use in our PHP code to send HTTP POST requests off to FB to update our FB account. This is where the Graph API comes in.

First off, give the application permission to update your account as described in the

“Authenticating Users in a Web Application” and “Requesting Extended Permissions” sections of this page:

http://developers.facebook.com/docs/authentication/

The extra scope permissions are: publish_stream,offline_access

(These will give you an access token that doesn’t expire, for a while at least anyway)

At the end of this process, you should have an access token, which you lift from a URL to which FB will redirect you. Be careful when copying and pasting, as the access token is very long, and you want to make sure you are only copying the access token and not other parts of the URL string.

When you have your access token, you can then plug it into a cURL request to update your status, as described here:

http://developers.facebook.com/docs/api#publishing

You’re going to have to use cURL for this, as the required HTTP method is POST.

Important Note: using cURL is this way is not to be confused with using cURL to login to Facebook and make updates. There are lots of scripts out on the Internet that will do this for you, but use of these scripts is in contravention of the FB T&Cs, so if you use them, Facebook could close your account.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>