Search code examples
phpfacebookcurlfacebook-fql

FQL Query from PHP using cURL returning "Method Not Implemented"


I am using Facebook's Facebook Query Language (FQL) to get information on a user once I already know his user id. When I enter the following URL into Firefox, it returns the correct XML for the request:

http://api.facebook.com/method/fql.query?query=SELECT name FROM user WHERE uid= **'

Namely it returns the user's name.

Note: please don't suggest using Graph API as I will in fact be using FQL for things not implemented in Graph yet like networks, this is just a simple example.

BUT, when I go to the page on my website which executes the following PHP code:

        $url = 'http://api.facebook.com/method/fql.query?query=SELECT name FROM user WHERE uid=**********';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($ch);
        curl_close($ch);
        echo($response);

I see:

 Method Not Implemented

 Invalid method in request

This shouldn't be an authentication problem either, name is something that is publicly available.

For more insight, when I enter any malformed FQL request, the PHP page correctly returns an error XML, just as it would when you enter the URL in Firefox. It is only when I enter a CORRECTLY FORMED FQL request that it differs from what I see in Firefox, namely, it gives the error above, which is not even XML.

Any ideas? Thanks.


Solution

  • Are you properly encoding the query parameters? Try something like:

    $query = 'SELECT name FROM user WHERE uid=**********';
    $url = 'http://api.facebook.com/method/fql.query?query=' . rawurlencode($query);
    

    Or you can use the Facebook PHP SDK which automatically does this for you.