How to make a fql multyquery with the PHP SDK - facebook

I'm trying to make a fql multy query using the PHP SDK. This is my code
try {
$fql = array(
"query1" => "SELECT uid2 FROM friend WHERE uid1 = me()",
"query2" => "SELECT name, url, pic FROM profile WHERE id IN (SELECT uid2 FROM #query1)"
);
//$fql = "SELECT uid2 FROM friend WHERE uid1 = me()";
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api( array(
'method' => 'fql.multiquery',
'query' => $fql,
) );
} catch ( FacebookApiException $e ) {
error_log( print_r( $e->getMessage(), true ) );
$user = NULL;
}
And this always returns an exception with no message
[04-May-2012 20:22:26 UTC] PHP Notice: Undefined property:
FacebookApiException::$getMessage in C:\Program Files
(x86)\Zend\Apache2\htdocs\wordpress\wp-content\plugins\all-in-one-event-calendar\lib\plugins\A1ecFacebookConnectorPlugin.php
on line 120 [04-May-2012 20:22:26 UTC]
i'm obviously doing something wrong, what could that be?

Ok i found how to do this, you must use queries as a parameter, not query
try {
$fql = array(
"query1" => "SELECT uid2 FROM friend WHERE uid1 = me()",
"query2" => "SELECT name, url, pic FROM profile WHERE id IN (SELECT uid2 FROM #query1)"
);
//$fql = "SELECT uid2 FROM friend WHERE uid1 = me()";
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api( array(
'method' => 'fql.multiquery',
'queries' => $fql,
) );
} catch ( FacebookApiException $e ) {
error_log( print_r( $e->getMessage(), true ) );
$user = NULL;
}

Your problem is you are trying to call $e->getMessage which does not exist. Your code on line 120 should read:
error_log( print_r( $e, true ) );
If you have problems with the multiquery, you could do this as a single query:
$fql = "SELECT name, url, pic FROM profile WHERE id IN
(SELECT uid2 FROM friend WHERE uid1 = me() )";
$user_profile = $facebook->api( array(
'method' => 'fql.query',
'query' => $fql,
) );
I've been able to get a query running like this three SELECTs deep. Four SELECTs seems to be too much.

Related

facebook upcoming birthday fetching only this year

i want to fetch Facebook friend birthdays from current date to next year current day . todate is the date 364 days later to now in m/d. for this purpose first fql is to retrieve data from jan to current date and second one is for fetching data from current date to 31 dec. i want to apply pagination to upcoming birthdays for next 11 month and 29 days.the following queries are working and fetching the data.can anyone tell me the best way to do so. any suggestion will be highly appreciated.
$fromDate = date('m/d');
$past_birthdays = $facebook->api(array(
'method' => 'fql.query',
'query' => "SELECT uid, name, birthday, birthday_date,substr(birthday_date,0,3) FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) AND birthday_date<'$toDate' AND birthday_date>='01/01' order by birthday_date",
'access_token' => $accessToken
));
if ($toDate > $fromDate) {
$condition = " AND birthday_date<'$toDate'";
} else {
$condition = "";
}
$upcoming_birthdays = $facebook->api(array(
'method' => 'fql.query',
'query' => "SELECT uid, name, birthday, birthday_date,substr(birthday_date,0,3) FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) AND birthday_date>='$fromDate' $condition order by birthday_date",
'access_token' => $accessToken
));
as in your case i checked on facebook api explorer if you provide limit in your query, then its output in not consistent , the best way to solve your problem is load the whole birthday
list in array and show it in slots.
$fromDate = date('m/d');
$past_birthdays = "";
$upcoming_birthdays = $facebook->api(array(
'method' => 'fql.query',
'query' => "SELECT uid, name, birthday, birthday_date, substr(birthday_date, 0,3) from user where uid in (select uid2 from friend where uid1=me()) and birthday_date >='$fromDate' order by birthday_date Limit 0,500",
'access_token' => $accessToken
));
$no = count($upcoming_birthdays); //exit;
if ($no < 500) {
$Limit2 = 500 - $no;
$past_birthdays = $facebook->api(array(
'method' => 'fql.query',
'query' => "SELECT uid, name, birthday, birthday_date, substr(birthday_date, 0,3) from user where uid in (select uid2 from friend where uid1=me()) and birthday_date >='01/01' and birthday_date <= '$fromDate' order by birthday_date Limit 0,$Limit2",
'access_token' => $accessToken
));
//echo count($past_birthdays);exit;
$upcoming_birthdays = array_merge($upcoming_birthdays, $past_birthdays);
no need to take todate as you are fetching for whole year.this ccode is to fetch only 500 but run query for whole year.

fql query fails - suspected auth issue

In a page, if I make a test fql query it works:
$fql = 'SELECT name from user where uid = ' . $user_id;
$ret_obj = $facebook->api(array(
'method' => 'fql.query',
'query' => $fql,
));
echo '<pre>Name: ' . $ret_obj[0]['name'] . '</pre>';
However if I try to run this query, it fails:
$fql2 = 'SELECT uid, name, pic_square FROM user WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me()) AND sex =male';
$ret_obj2 = $facebook->api(array(
'method' => 'fql.query',
'query' => $fql2,
));
echo '<pre>Name: ' . $ret_obj2[0]['name'] . '</pre>';
I suspect it is an auth problem, because the same fql query works in the graph api test tool!
If you want to checkout the full php code, it's the second example on this page:
https://developers.facebook.com/docs/reference/php/facebook-api/
You need just basic permissions for this query, so this is not the issue. Try adding double quotes around "male":
$fql2 = 'SELECT uid, name, pic_square FROM user WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me()) AND sex="male"';
Thanks for the support guys, the query wasn't working for 2 reasons:
1) I forgot the ; after the end of statement
2) In my code I used
`SELECT uid, name, pic_square, FROM`
instead of
`SELECT uid, name, pic_square FROM`

Facebook: Anyone knows why I can't get the event's I'm attending?

I'm trying to get a list of facebook events I'm attending. I have these codes:
$fql = "SELECT eid FROM event_member WHERE uid = 100000266996446 AND rsvp_status = 'attending'";
$param = array(
'method' => 'fql.query',
'query' => $fql,
'callback' => ''
);
$fqlResult = $facebook->api($param);
foreach( $fqlResult as $keys => $values ){
echo "<div>" . $values['eid'] . "[][]</div>";
}
Many thanks for any help! :)
With your code, please note that you cannot do it if you're using a profile uid. It will only work with fan pages.

fql.get method return empty Array in Facebook Application with facebook-php-sdk-v2.1.2-4

Recently I am trying to develop a facebook application that shows the STATUSES of loged in facebook user.
I am succeeded to retrieve information from user table using FQL. But right now I am using this following code to get data from status table.
****BEGIN :: CODE SECTION***********
$sql = "SELECT message,time FROM status WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = ".$myLoginId.")";
$param = array(
// 'method' => 'fql.query',
'method' => 'status.get',
'query' => $sql,
'callback' => ''
);
$fqlResult = $facebook->api($param);
print_r($fqlResult);
***END:: CODE SECTION********
But everytimes it returns Empty Array.
Right Now I am using Library of facebook-php-sdk-v2.1.2-4-g2343fca.tar which I collect from github.com.
can anybody please give me a solution.
Thank in Advance.
You code is working just fine, just a small note, you can use me() to get the current user id:
$fql = "SELECT uid,message,time FROM status WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me())";
$fqlResult = $this->facebook->api(array(
'method' => 'fql.query',
'query' => $fql
));
AND once again, the most important part is acquiring the correct permissions, which are user_status (if you want to get your statuses) AND friends_status (to get your friends' statuses).

fql.multiquery new sdk

I cannot figure out what is wrong with this fql.multiquery and cannot seem to find any examples of the new sdk with fql.multiquery.
Ultimately I want to get the page name and page id(s) of the visiting user pages which they both administrator and are fans of.
$queries = '{
"page_admin_ids" :
"SELECT page_id FROM page_admin WHERE uid = ' . $afid . ' LIMIT 5",
"page_fan_ids" :
"SELECT page_id FROM page_fan WHERE page_id IN (SELECT page_id FROM #page_admin_ids)",
"page_name_and_id" :
"SELECT name, page_id FROM page WHERE page_id IN (SELECT page_id FROM #page_fan_ids)"
}';
$attachment = array("method"=>"fql.multiquery","query"=>$queries,'access_token'=>$access_token);
$ret_code = $facebook->api($attachment);
print_r($ret_code); die();
$multiQuery = array(
'likes' =>
' SELECT ... FROM like ...',
'users' =>
' SELECT uid, name ' .
' FROM user ' .
' WHERE uid IN (SELECT user_id FROM #likes) ',
);
$multiQueryResult = $facebook->api(array(
'method' => 'fql.multiquery',
'queries' => $multiQuery
));
I think you should replace "query" parameter in $attachment with "queries".