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

ZendCon '08 - Day One

ZendCon '08 - Day One

  • September 16, 2008

So its end of Day One here at ZendCon, let’s do a quick re-cap.

Read More
Escrevendo testes com PHPT e contribuindo com o PHP

Escrevendo testes com PHPT e contribuindo com o PHP

  • August 19, 2009

Este ano esteve em destaque a PHPTestFest09, sendo realizada em vários cantos do mundo e destacando-se a participação do PHPSP que contribuiu com o maior número de testes. Ficou de fora? Então já comece a se preparar para ano que vem.

O PHPT é um framework extremamente simples de testes criado e usado internamente pelos desenvolvedores do core. Ele é extremamente atômico e centrado em testes como os que esperamos do PHP, como testar funções e bugs específicos.

O que preciso saber?

A grande vantagem do PHPT é que para poder escrever um teste, tudo que você precisa saber é: como escrever código PHP. Claro que um pouco de conhecimento interno do funcionamento do PHP vai te ajudar a procurar detalhes para testar, mas sabendo escrever PHP você já pode contribuir com uma grande percentagem dos testes que precisamos.

Vamos então atacar por partes:

  1. Preparando o ambiente
  2. Escolhendo um teste
  3. Escrevendo um teste
  4. Rodando um teste
  5. Enviando seu teste para o PHP

Read More
Back to the Old Continent

Back to the Old Continent

  • November 8, 2011

“Your life begins where your comfort zone ends” (@elizabethN’s mother )

Read More