Showing posts with label Facebook. Show all posts
Showing posts with label Facebook. Show all posts

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.