Subscribing to Facebook Graph API’s real time updates using CURL.

Facebook allows you to subscribe to an application user’s activity on Facebook, so that you can subscribe to them, and use that information for various things. This is great because (the other alternative is) having to run a daemon process in order to find out a user’s Facebook activity is painful.

However, as awesome as this service is, understanding their documentation on this topic is a whole another matter. I found it almost impossible to decipher, as a lot of things were unclear or left unexplained. Look here: http://developers.facebook.com/docs/reference/api/realtime/

Luckily I found a blog post explaining how to do this, here it is.

I’ll run down the steps again below, as a few of the steps are now easier:

1. Get access token: https://developers.facebook.com/tools/access_token/ gives you access tokens to all your apps on facebook.

2. Check if access token works:

curl "https://graph.facebook.com/<appid>/subscriptions?access_token=<access token from  step (1)>"

Since you originally have no subscriptions, you get:

{“data”:[]} or some kind of error.

3. If you got empty JSON back in step 2, now you can subscribe to updates(here I want to subscribe to user checkins):

curl -F 'object=user' \
-F 'callback_url=<callback url you have to implement>' \
-F 'fields=checkins' \
-F 'verify_token=<secret token>' \
"https://graph.facebook.com/<your-app-id>/subscriptions?access_token=<access token from (1)"

The callback_url should be something along the lines of: https://github.com/facebook/php-sdk/blob/master/examples/example.php

This call should return “null” if successful. Once you have subscribed to updates, Facebook starts posting activities of all your application users to the callback_url above.

4. To verify step 3 was successful, run step 2 again, and this time, it should return something like this:

{"data":[{"object":"user","callback_url":"http:\/\/yourdomain.com\/callback.php","fields":["checkins"],"active":true}]}

The above response means the subscription to updates about user checkins are now active. Hopefully this helps people who are confused about how to get real time updates working.

About these ads

4 thoughts on “Subscribing to Facebook Graph API’s real time updates using CURL.

  1. for step 2, where do i paste this code? I’m just confused as to why everyone is using curl when you can just use either javascript sdk or php sdk?

    • Vanessa, you paste that code on the command line(unix terminal), like the other steps outlined. The JavaScript and php steps outlined by the facebook documentation were extremely confusing, atleast for me, so I chose to go this route and it worked out great.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s