Saturday, November 27, 2010

Retrieving tagged Facebook photos using the Facebook PHP SDK.

Lately I've had some experience using the Facebook PHP SDK to retrieve information from the Facebook Graph API, and I thought I'd share something I found that wasn't immediately obvious (to me, at least).

When retrieving tagged photos, Facebook responds with an array of the following form:
array(2) {
  ["data"]=>array(25) {...}
  ["paging"]=>
    array(2) {
      ["previous"]=>string(87) "https://graph.facebook.com/00000000/photos?limit=25&since=2010-09-16T20%3A33%3A34%2B0000"
      ["next"]=>sring(87) "https://graph.facebook.com/00000000/photos?limit=25&until=2010-09-05T01%3A11%3A23%2B0000"
    }
From the paging information provided as urls, it's not immediately apparent how to use the PHP SDK provided to get the next and previous pages of pictures. At first I tried subclassing from the provided Facebook class (in facebook-php-sdk/src/facebook.php) in order to make use of the protected makeRequest(), _oauthRequest(), or _graph() functions directly, since I believed that the graph() function provided would strip the HTTP GET params from the provided query string. However, this assumption proved erroneous, and the easiest way to retrieve the next page is to use the graph() function directly as per
$facebook->api(substr($photos['paging']['next'], 26));
since the https://graph.facebook.com is automatically prepended.

2 comments:

  1. Hey Zach,

    Nice blog - but quick question since I've never done any serious web development. Are all function calls / queries to facebook through HTTP post, and if so, how in the world do they keep it secure? Does it actually get sent through HTTPS or is it encrypted then sent through regular HTTP or...? Just curious!

    ReplyDelete
  2. Although the graph interface at graph.facebook.com responds to both HTTP and HTTPS requests, to keep the requests and responses secure developers are encouraged to use HTTPS. The PHP SDK uses HTTPS, as I stated above.

    ReplyDelete