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 aotablesa 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: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 aomethoda parameter to define a API method to be called and then figure out that the query should be in a aoquerya 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:'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.
Truncated by Planet PHP, read more at the original (another 1664 bytes)