Facebook Query Language( FQL)

Posted: December 28, 2012 in Facebook
Tags:

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.

You can execute FQL queries by fetching https://api.facebook.com/method/fql.query?query=QUERY. You can specify a response format as either XML or JSON with the format query parameter.

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.

For any query that takes a uid, you can pass me() to return the logged-in user. For example:

FQL to get details of the current logged in user by me() function

SELECT name FROM user WHERE uid = me() 
SELECT uid, name, pic_square FROM user WHERE uid = me()
OR uid IN (SELECT uid2 FROM friend WHERE uid1 = me())

Fql to get album id and details by album name

function get_album_id_by_album_name($album_name){//return array of data
        $fql            =   'SELECT aid, owner, name, object_id FROM album WHERE owner=me() and name="'.$album_name.'"';

                $param  =   array(
                'method'    => 'fql.query',
                'query'     => $fql,
                'callback'  => ''
            );
            $fqlResult   =   $this->facebook->api($param);
	return $fqlResult;
}

FQL to get facebook user profile details

function get_user_profile_details($user_facebook_id){

		$fql    =   "SELECT name,pic_square,pic,uid 
FROM user  where	
 uid  =".$user_facebook_id;

                $param  =   array(
                'method'    => 'fql.query',
                'query'     => $fql

            );
            $fqlResult   =   $this->facebook->api($param);
		return $fqlResult[0];
}

FQL to check the user is facebook application user

function check_is_app_user($facebook_user_id){//return 1 if the user is using the current application
	try{
	$user=$facebook_user_id;
        $fql    =   "select is_app_user from user where uid=" . $user;
            $param  =   array(
                'method'    => 'fql.query',
                'query'     => $fql,
                'callback'  => ''
            );

            $fqlResult   =   $this->facebook->api($param);

	if($fqlResult)
		return $fqlResult['0']['is_app_user'];
       else
                return 0;
        }
	 catch(Exception $o){
            d($o);
        }

}

FQL to get the tagged photos of facebook user and tag details,tag co-ordinates of user photos

function get_user_tagged_photos_details($user_facebook_id){

		$fql    =   "SELECT pid,text,xcoord,ycoord,created FROM photo_tag WHERE subject =".$user_facebook_id;

                $param  =   array(
                'method'    => 'fql.query',
                'query'     => $fql

            );
            $fqlResult   =   $this->facebook->api($param);
		return $fqlResult[0];
}
function get_tagged_users_id_by_photoid($photo_id){

		$fql    =   "SELECT text FROM photo_tag WHERE pid =".$photo_id;

                $param  =   array(
                'method'    => 'fql.query',
                'query'     => $fql

            );
            $fqlResult   =   $this->facebook->api($param);
		return $fqlResult[0];
}

pid-The ID of the photo being queried.
xcoord-The center of the tag’s horizontal position, measured as a floating-point percentage from 0 to 100, from the left edge of the photo.
ycoord-The center of the tag’s vertical position, measured as a floating-point percentage from 0 to 100, from the top edge of the photo.

FQL query to get user facebook notification filter by unread

function get_fb_notification_by_uid($user_id){

		$fql    =   "SELECT notification_id, sender_id,app_id,icon_url, title_html, body_html, href
FROM notification
WHERE recipient_id=".$user_id."
AND is_unread = 1
AND is_hidden = 0";
                $param  =   array(
                'method'    => 'fql.query',
                'query'     => $fql

            );
            $fqlResult   =   $this->facebook->api($param);
		return $fqlResult[0];
}

sender_id-The user ID of the sender of the notification.
href-The URL associated with the notification. This is usually a location where the user can interact with the subject of the notification.
app_id-The ID of the application associated with the notification. This may be a third-party application or a Facebook application (for example, Wall).
is_unread-Indicates whether the notification has been marked as read. Use notifications.markRead to mark a notification as read.

FQL to get photos of my friends posted by date or week

function get_friends_photo_details($date){

		$fql    =   "SELECT pid, caption, aid, owner, link, src_big, src_small, created, modified FROM photo WHERE aid IN 
   (SELECT aid FROM album WHERE owner IN 
      (SELECT uid2 FROM friend WHERE uid1=me()) 
   ) 
AND created ."$date;

                $param  =   array(
                'method'    => 'fql.query',
                'query'     => $fql

            );
            $fqlResult   =   $this->facebook->api($param);
		return $fqlResult[0];
}

function get_friends_photo_details_last_week(){

		$fql    =   "SELECT pid, caption, aid, owner, link, src_big, src_small, created, modified FROM photo WHERE aid IN 
   (SELECT aid FROM album WHERE owner IN 
      (SELECT uid2 FROM friend WHERE uid1=me()) 
   ) 
AND created > One_week_ago_in_unix_time ORDER BY created DESC";

                $param  =   array(
                'method'    => 'fql.query',
                'query'     => $fql

            );
            $fqlResult   =   $this->facebook->api($param);
		return $fqlResult[0];
}

FQL to get photos by facebook albums

function get_photos_by_album_id($album_id){

if($album_id)
        $fql            =   'SELECT pid,src_big,owner,link,position,created,caption,src FROM photo WHERE aid="'.$album_id.'" ORDER BY created DESC LIMIT 0,6';

                $param  =   array(
                'method'    => 'fql.query',
                'query'     => $fql,
                'callback'  => ''
            );
            $fqlResult   =   $this->facebook->api($param);
		return $fqlResult;

}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s