Using the Facebook PHP-SDK to run FQL

As of Facebook’s migration to the new Graph API and its OAuth 2.0 protocols, I can say that their Developer documentation has become a confusing, misleading and generally unstrung pile of semi-deprecated articles, and I’m being optimistic in doing so. Problem is lots of old articles are still in there pointing to old practices and recommendations and most of these do not come with disclaimers pointing to new recommendations, and in some extremes old soon-to-be-deprecated methods do not even have equivalents in the new APIs.

The new SDKs have not, as you can say, fallen far from the tree. While they are really great new and shiny, documentation on how to use them is still vague, missing or spread out in the internet in blogs like these, in posts from us users trying to share the information with other soon-to-be-suffering developers.

So this is an example of this, i have been using these new resources and the new PHP-SDK and have ran into various walls, so I decided to start putting some of this on my blog, for 2 reasons: to spread the word, and to have notes for myself when I come back to this.

How can I run FQL queries using the new SDK?

FQL is Facebook’s SQL alernative, allowing you to query information as you would from a huge database, this documentation is thankfully still up to date and you can see a list of “tables” you can query from, and the rest of the little tweaks you will pick up on a trial and error basis.

The old Facebook library, based on their REST API had a fairly easily and accessible method to run FQL

 $facebook->api\_client->fql\_query("SELECT uid, pic, pic\_square, name FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = 111111111)"); 

However this (and many other methods) is no longer available in the new PHP SDK so it took me a while to figure out how to get a FQL call into the library, since the only method available in this new library was api() a polymorphic and non-documented method. I finally was able to find the answer somewhere on a similar blog that ran into the same issue. This is how its done:

 <?php //Get Facebook SDK Object $config = array( 'appId' => APP\_ID, 'secret' => API\_SECRET, 'cookie' => true, );

$facebook = new Facebook($config);

//Create Query $params = array( 'method' => 'fql.query', 'query' => "SELECT uid, pic, pic\_square, name FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = 111111111)", );

//Run Query $result = $facebook->api($params); ?> 

So once you do figure out that you can pass in the “method” parameter to define a API method to be called and then figure out that the query should be in a “query” parameter, you are set. Needless to say this might take a little longer to figure out then you would expect.

So this piece of code actually looks pretty horrid from my point of view, I don’t like having to create the array all the time to be passed to the function, so I decided to instead of using facebook’s SDK straight as is, I was going to extend it and push in some helper methods, namely a FQL helper method, this is what it looks like:


namespace App\\Facebook;

include\_once \\ROOT\_PATH . 'library/vendor/facebook/facebook.php';

class Client extends \\Facebook { private $config; private $reqPerms;

/\*\* \* Runs a FQL query agains the API \* \* @param string $query \* @return array \*/ public function fql($query) { $params = array( 'method' => 'fql.query', 'query' =>$query, );

return $this->api($params); }

} $config = array(/\* insert config here \*/);

//Using client $fb = new App\\Facebook\\Client($config);

$query = "SELECT uid, pic, pic\_square, name FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = 111111111)"; $result = $fb->fql($query);


So this makes it now nice and clean to use FQL as well as having the upside that the class is now nicely packed inside my namespace and as an added bonus, this class is now much easier to mock for unit testing, since i have a direct method to test for and not have to filter out params sent to the method.

I did go one step further with this and submitted this change to Facebook’s SDK on github as a pull request . It has not yet been replied to but i hope that soon it will make it into the actual code and more people will be able to benefit from it.

Hope you enjoy this and it gives you ideas on how to improve your usage of the Facebook PHP SDK and i’ll come back with more in the future.

comments powered by Disqus

Related Posts

Estendendo o Smarty: TinyMCE

Estendendo o Smarty: TinyMCE

  • March 12, 2007

(Nota: sim estender não extender)

Uso, e recomendo, a utilização do Smarty em qualquer projeto PHP. Porque?

Bem tenho inúmeros motivos para fazer esta recomendação, mas neste post vou me limitar a apenas citar alguns, pois não é o objetivo.

  1. Separar PHP e HTML: código limpo, manutenção fácil
  2. Code e Layout em paralelo: agilidade no trabalho em equipe
  3. Funções de maquiagem: formatar data, wordwrap, funções que dizem respeito a visualização, ficam na visualização
  4. Funções customizadas: estender o Smarty é simples e útil

É neste ponto, o número 4 que vou focar este post, utilizando o tinyMCE como exemplo.

Read More
AJAX: What is it?

AJAX: What is it?

  • October 12, 2006

Author’s note: This article was first published in my AJAX Column ( ) in Portuguese where my intended audience is a group of beginners learning about the AJAX initiative. Since it turned out to be a really interesting article I decided to translate it and share it with a wider audience.

I wondered where I should begin explaining AJAX and finally decided I should begin explaining what’s behind it and how it came around, so everyone knows where we are coming from.

Ajax, web 2.0, RSS, if you’ve never heard any of these terms than something is wrong, you are either way behind in web technologies or not serious enough about web development. The concept of Web 2.0 is what we may call the “father” of the latest trends in web applications, where various technologies are used to improve the user’s experience, but what is the concept?

Read More
We are all PHP Evangelists

We are all PHP Evangelists

  • January 4, 2013

Note: This article was originally published on the april/2012 issue of php-architect .

Read More