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.

GNU readline in Racket

My main operating system is Gentoo (2.6.32-gentoo-r7), which doesn't have the Racket programming language in its package repository, so I compiled it myself from the sources available on the racket website (another side note: Racket v5.0.1 won't compile if you have a newer version of libpng installed, but Racket v5.0.2 fixes the out-of-date function call). However, I ran into a slight problem when trying to install GNU Readline support by evaluating (require readline) in the racket prompt, where I was greeted by the following error:
ffi-lib: couldn't open "libreadline.so.5" (libreadline.so.5: cannot open shared object file: No such file or directory)
Which sounds rather foreboding, but fortunately has an easy fix. The problem is caused by a tendency of later GNU Readline installations to lack the shared object file named "libreadline.so.5", instead having a file named "libreadline.so.6", a newer version of the library. The simple (and as far as I have been able to tell, correct) fix is to modify line 11 in $RACKET_DIR/lib/racket/collects/readline/mzrl.rkt from:
(define libreadline (ffi-lib "libreadline" '("5" "4" ""))) 
to
(define libreadline (ffi-lib "libreadline" '("6" "5" "4" ""))).
Hope that keeps at least one Schemer from pulling their hair out in the near future.

Racket, a new Scheme

So over this Thanksgiving holiday I took the time to read through some of the Racket documentation, something I've been meaning to do for some time now. Racket, some of you may know, is the newest incarnation of PLT Scheme, the Scheme behind the old DrScheme Scheme IDE. As a LISP aficionado and sometime web developer, I was especially interested in the web server tutorial (found here: http://docs.racket-lang.org/more/), which doesn't take that long to go through and leaves you feeling accomplished with a simple, functional webserver. I personally worked through it and I plan on making it a little more fully featured during my spare time, perhaps even adding some elements of the MVC paradigm. I highly encourage anyone, regardless of their experience with LISP, Scheme, or other functional languages, to check out Racket (racket-lang.org) and broaden their programming horizons.

For another simple server exercise (this one in C), check out my friend Jeremy's blog at http://www.cores2.com/blog/?p=81.