Thursday, June 28, 2012

Facebook Query Language (FQL)

The FQL object enables running FQL queries using the Graph API. Facebook Query Language, or FQL, enables you to use a SQL-style interface to query the data exposed by the Graph API. It provides for some advanced features not available in the Graph API, including batching multiple queries into a single call.

Example

Runs the query "SELECT uid2 FROM friend WHERE uid1=me()"
GET /fql?q=SELECT+uid2+FROM+friend+WHERE+uid1=me()&access_token=...

Read

You can issue a HTTP GET request to /fql?q=query where query can be a single fql query or a JSON-encoded dictionary of queries.

Query

Queries are of the form SELECT [fields] FROM [table] WHERE [conditions]. Unlike SQL, the FQL FROM clause can contain only a single table. You can use the IN keyword in SELECT or WHERE clauses to do subqueries, but the subqueries cannot reference variables in the outer query's scope. Your query must also be indexable, meaning that it queries properties that are marked as indexable in the documentation below.
FQL can handle simple math, basic boolean operators, AND or NOT logical operators, and ORDER BY and LIMIT clauses.
For any query that takes a uid, you can pass me() to return the logged-in user. For example:
SELECT name FROM user WHERE uid = me() 
Other functions that are available are now(), strlen(), substr() and strpos().
Here's an example of a subquery that fetches all user information for the active user and friends:
SELECT uid, name, pic_square FROM user WHERE uid = me()
OR uid IN (SELECT uid2 FROM friend WHERE uid1 = me())

Multi-query

Evaluates a series of FQL (Facebook Query Language) queries in one call and returns the data at one time.
This method takes a JSON-encoded dictionary called ''queries'' where the individual queries use the exact same syntax as a simple query. However, this method allows for more complex queries to be made. You can fetch data from one query and use it in another query within the same call. The WHERE clause is optional in the latter query, since it references data that’s already been fetched. To reference the results of one query in another query within the same call, specify its name in the FROM clause, preceded by #.
For example, say you want to get some data about a user attending an event. Normally, you’d have to perform two queries in a row, waiting for the results of the first query before running the second query, since the second query depends on data from the first one. But with fql.multiquery, you can run them at the same time, and get all the results you need, giving you better performance than running a series of fql.query calls. First, you need to get the user ID and RSVP status of each attendee, so you’d formulate the first query – query1 – like this:
"query1":"SELECT uid, rsvp_status FROM event_member WHERE eid=12345678"
Then to get each attendee’s profile data (name, URL, and picture in this instance), you’d make a second query – query2 – which references the results from query1. You formulate query2 like this:
"query2":"SELECT name, url, pic FROM profile WHERE id IN (SELECT uid FROM #query1)"
This method also has better performance than running a series of fql.query calls with batch.run.

Sample

Here's a sample for running fql query
<?php
  $app_id = 'YOUR_APP_ID';
  $app_secret = 'YOUR_APP_SECRET';
  $my_url = 'POST_AUTH_URL';

  $code = $_REQUEST["code"];
 
 //auth user
 if(empty($code)) {
    $dialog_url = 'https://www.facebook.com/dialog/oauth?client_id=' 
    . $app_id . '&redirect_uri=' . urlencode($my_url) ;
    echo("<script>top.location.href='" . $dialog_url . "'</script>");
  }

  //get user access_token
  $token_url = 'https://graph.facebook.com/oauth/access_token?client_id='
    . $app_id . '&redirect_uri=' . urlencode($my_url) 
    . '&client_secret=' . $app_secret 
    . '&code=' . $code;
  $access_token = file_get_contents($token_url);
 
  // Run fql query
  $fql_query_url = 'https://graph.facebook.com/'
    . '/fql?q=SELECT+uid2+FROM+friend+WHERE+uid1=me()'
    . '&' . $access_token;
  $fql_query_result = file_get_contents($fql_query_url);
  $fql_query_obj = json_decode($fql_query_result, true);

  //display results of fql query
  echo '<pre>';
  print_r("query results:");
  print_r($fql_query_obj);
  echo '</pre>';

  // Run fql multiquery
  $fql_multiquery_url = 'https://graph.facebook.com/'
    . 'fql?q={"all+friends":"SELECT+uid2+FROM+friend+WHERE+uid1=me()",'
    . '"my+name":"SELECT+name+FROM+user+WHERE+uid=me()"}'
    . '&' . $access_token;
  $fql_multiquery_result = file_get_contents($fql_multiquery_url);
  $fql_multiquery_obj = json_decode($fql_multiquery_result, true);

  //display results of fql multiquery
  echo '<pre>';
  print_r("multi query results:");
  print_r($fql_multiquery_obj);
  echo '</pre>';
?>

Tables

Query this table to return information about a photo album.
Query this table to return read-only properties about an application.
An FQL table containing the requests sent via an app to a user.
Query this table to return information about a checkin. By default, this query returns the last 20 checkins and returns a maximum of 500 checkins.
Query this table to obtain comments associated with one or more [fb:comments]
The comments_info FQL table. Query this table to obtain XIDs for fb:comments objects associated with an application ID.
Query this table to return a user's friends and the Facebook Pages to which the user is connected.
Query this table to return information about a cookie.
Query this table to return the application IDs for which the specified user is listed as a developer in the Developer application.
The domain table provides a read-only mapping between domain names and ids.
Query this table to return information about the admin of a domain.
Query this table to return information about an event.
Query this table to return information about a user's status for an event or see a list of events for a user.
Query this table to return detailed information about a user's family.
Query this table to determine whether two users are linked together as friends.
Query this table either to determine which users have sent friend requests to the logged-in user or to query whether a friend request has been sent from the logged-in user to a specific user.
Query this table to return any friend lists owned by the specified user.
Query this table to determine which users are members of a friend list.
Query this table to return information about a group.
Query this table to return information about the members of a group, or retrieve a list of groups of which a user is a member
The insights table contains statistics about Applications, Pages and Domains
Query this table to return the user IDs of users who like a given Facebook object (video, note, link, photo, or album).
Query this table to return the links a user has posted.
The link_stat table contains counts that show how users on Facebook are interacting with a given link.
An FQL table representing posts that are tagged with a location.
The mailbox_folder table contains information about a user's mailbox folders.
Query this table to return information about messages in a thread.
Query this table to return the notes the current user has written or to return details for a particular note.
Query this table to get the notifications for the current session user, that is, any notification that appears on http://www.facebook.com/notifications.php.
Query this table to return information about a URL in the Open Graph
Query this table to return information about an offer.
Query this table to return information about a Facebook Page.
Query this table to return information about which Facebook Pages the user Admins.
An FQL table that can be used to return a list of a users that are blocked from a Facebook Page.
Query this table to return information about the user who likes a Facebook Page.
Query this table to return information about a Page's milestones.
Query this table to return the permissions the current user has granted to the app.
Query this table to return more descriptive information about extended permissions.
Query this table to return information about a photo.
Query this table to return information about photo source url.
Query this table to return information about a photo tag.
Query this table to return information about a place.
Query this table to return a user's privacy setting for a given object_id.
Query default privacy settings for a user for a particular app
Query this table to return certain (typically publicly) viewable information for a profile.
Query this table to return information a Page's apps.
A Question as represented in FQL.
An option for a question, as represented in FQL.
The votes on a particular option for a question, as represented in FQL.
Query this table to obtain reviews associated with an application, a user or both.
Query this table to determine whether two users are linked together as friends.
Query this table to return standard information about a user, for use when you need analytic information only.
Query this table to return one or more of a user's statuses.
Query this table to return posts from a user's stream or the user's profile.
Query this table to return a filter_key that can be used to query the stream FQL table, as seen through any content filters the user has available on Facebook.
Query this table to return associations between users or Facebook Pages and the items they tag in status posts.
Query this table to determine whether two users are linked together in a subscriber/subscribee relationship and to retrieve a list of users another user subscribes to or is subscribed to by.
Query this table to return information about message threads in a user's Inbox.
Query this table to return the native strings (original, untranslated text in your application interface) and the translated strings for your application.
This table can be used to access information about messages in the new Facebook messaging system.
This table can be used to access information about threads in the new Facebook messaging system.
This table should be used to access information about subscribe and unsubscribe actions performed on a thread in the new Facebook messaging system.
This table should be used to access information about the number of threads in a folder in the new Facebook messaging system.
An FQL table containing the Open Graph URLs that the current session user has Liked.
Query this table to return detailed information from a user's profile.
The video table contains information about videos.
The video_tag table contains information about users tagged in videos.

No comments:

Post a Comment