Reputation: 3606
Completely new to most of this stuff, but basically Im playing around with the Facebook API on a rainy Saturday afernoon for a bit of a challenge.
I've taken the code at the bottom of this page:-
http://developers.facebook.com/docs/reference/fql/
And changed it around a little. Basically what I'm trying to do at this point is just to print up on screen the total number of friends, which I assume is as easy as returning the number of records returned by the JSON query, but I can't get it right. I know I'm doing something wrong but need a pointer as to what. At the moment my code only returns 1, where it should be 160 or so.
My code is here (I've taken out my app id/secret):-
<?php
//Very basic code to pull out details about the logged in user.
$app_id = 'myid';
$app_secret = 'mysecret';
$my_url = 'http://lab.2toria.com/facebook/test1.php';
$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);
//Now need to find a way to count the number of results returned in the array to get the number of facebook friends.
for($a=0;$a<count($fql_query_obj);$a++)
{
$theCount++;
}
echo '<pre>';
print_r("Number of friends:");
print_r($theCount);
echo '</pre>';
?>
Thanks in advance. Mat
Upvotes: 1
Views: 1081
Reputation: 5987
Im not sure, but couldn't you just print the count($fql_query_obj)
since it is an array now this should return the same value,shouldn't it?
EDIT: and have you checked if facebook is givin you a valid json_object back?
Upvotes: 1
Reputation: 7279
You should do this
for($a=0;$a<count($fql_query_obj['data']);$a++)
as Facebook response will have a data property which contains the original data.
Upvotes: 1