Facebook API – Part 4
In the last article of Facebook API I show you more about the PHP SDK.
Initialization
Like the JavaScript SDK, the first thing for the PHP SDK is also to initialize the SDK. So we need to create a new Facebook object. This object needs two things, the app id and the app secret.
//get a Facebook object $facebook = new Facebook( array( 'appId' => $fb_appid, 'secret' => $fb_secret ) );
Get data from Facebook
After initialization we can request data from Facebook for the logged in user. The following line of code gets an array with all information that is available for the user. If you are not logged in, you will get an empty array:
$data = $facebook->api('/me');
FQL
FQL is a language which looks like the SQL language and is needed to get information from Facebook. With SQL like statements you can make detailed requests. The above example would look like the following with FQL:
$query = 'SELECT name, username FROM user WHERE uid = '.$user_id; $response = $facebook->api(array('method' => 'fql.query', 'query' => $query));
For this we have to decide which columns we want to get returned. So we only want the name and the username for the logged in user. This also works for friends of the user. For all other users on Facebook this does not work, if they set the name or username not to display to public.
The last thing I want to show is a more complex example, where I get the id of the sender and the id of the recipient of an app request. These app requests can be sent from users to other users to invite them to an app.
//get apprequest (sender_uid) $query = 'SELECT sender_uid, request_id FROM apprequest WHERE recipient_uid = '.$recipient_id.' AND app_id = '.$fb_appid; $result = $facebook->api(array('method' => 'fql.query','query' => $query));
Hello, I followed your exact instructions to use FQL with Graph API but when i log out and log in again, i get the following error:
Fatal error: Uncaught Exception: Requires user session
I am already logged in with facebook. API call to $user_profile = $facebook->api(‚me‘,’GET‘); work. But i get this exception when i try to run an FQL query. Would really appreciate some help in this. Hope to hear from you soon. 🙂