How to handle the big int facebook uid returned by FQL? - facebook

I've a problem in handling the big userid's of facebook and properly storing them into my database..
As the fql.query REST api is going to be deprecated ,I'm using the GRAPH API for getting the results of the FQL.
I want to get the list of my friends with sex,relationship_status .
The query i executed is
$allFriends = $facebook->api("/fql
?q=SELECT+uid,+name,+sex,+relationship_status+FROM+user+where+uid+in+
(SELECT+uid2+FROM+friend+WHERE+uid1+=$fbuid)"
);
I tried the above in the Graph API explorer and the result is something like this,
{
"data": [
{
"uid": 100003082853071,
"name": "Sam jones",
"sex": "male",
"relationship_status": null
}
]
}
Note the uid is returned as int, so whenever i print the array itself it has values like (1.34234422 +E03). So even json_encode for that array doesn't help.
But when i call the GRAPH API directly something like 'graph.facebook.com/1585213/friends' that returns the data as
{
"data": [
{
"name": "Vijay Kannan",
"id": "102937142343"
}
]
}
Note the id is returned as string..
Whenever I'm using the graph API call for FQL query, it returns the whole data as an 'Array' ,so the long big facebook uid's are transformed to a float like (1.34234422 +E03) .
How can i convert them into proper uid's and store/process them back.
I think the inconsistency of FQL and GRAPH API call should be also taken care by Facebook .. But i could not wait for that!!
Any ideas on this?

I tried most methods and after Google some more forums and facebook code list if found the following worked like a charm for me.
After i get the results from a FQL query i used the following line of code,
$friends = json_decode(preg_replace('/"uid":(\d+)/', '"uid":"$1"', $result),true);
// consider $result as the result rendered by the FQL query.
When i use the file_get_contents for a FB call you could have seen the error with error codes, so the best way to go with that is using CURL for all the FB API calls whenever necessary.
Please find the complete code i've used to get proper results,
$access_token = $facebook->getAccessToken();
$request_url ="https://graph.facebook.com/fql
?q=SELECT+uid,+name,+sex+FROM+user+where+uid+in+
(SELECT+uid2+FROM+friend+WHERE+uid1+=$fbuid)".
"&access_token=".$access_token;
$opts = array(
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 60,
CURLOPT_USERAGENT => 'facebook-php-3.1',
CURLOPT_CAINFO => /lib/fb_ca_chain_bundle.crt',
//replace the above path with proper path of the crt file
//in order to avoid the exceptions rendered by FB
//when we try to use CURL without proper certification file.
);
$opts[CURLOPT_URL] = $request_url;
if (isset($opts[CURLOPT_HTTPHEADER])) {
$existing_headers = $opts[CURLOPT_HTTPHEADER];
$existing_headers[] = 'Expect:';
$opts[CURLOPT_HTTPHEADER] = $existing_headers;
} else {
$opts[CURLOPT_HTTPHEADER] = array('Expect:');
}
$ch = curl_init();
curl_setopt_array($ch, $opts);
$result = curl_exec($ch);
if ($result === false) {
$e = new FacebookApiException(array(
'error_code' => curl_errno($ch),
'error' => array(
'message' => curl_error($ch),
'type' => 'CurlException',
),
));
curl_close($ch);
throw $e;
}
curl_close($ch);
$friends = json_decode(preg_replace('/"uid":(\d+)/','"uid":"$1"',$result));
I just to post this answers so it may help others until Facebook resolve this inconsistency.

There are two things very consistent about Facebook. They are: 1) changing their APIs at their whim without any headsup. 2) Inconsistency between graph and fql objects.
As you have indicated, the unquoted values returned from Facebook are always long's (aka big int, aka Int64). And the quoted values are string representations of the long value.
What it appears to me is that the $facebook->api call is munging the longs into floats. I'd suggest logging it as a bug with the $facebook->api team.
In the interim while they fix that bug, you can code your own code to do the HTTP post to the graph and parse the returned results. I don't encounter this issue with the C# API, nor with the Javascript API.

If youre using php (http://php.net/manual/en/function.sprintf.php):
printf("%14.0f", 1.00000145202E+14);
outputs:
100000145202000
Javascript:
parseFloat('1.00000145202E+14')

Related

Create charts for a facebook page via open graph and php

I have created a FB App that grants the following permissions from the user:
1. manage_pages
2. read_insights
By using the app, the user can create a new tab on a page and delete it when this is necessary.
Apart from that, I want to give the option to the user to see some basic stats regarding the page(s) that she creates the tab(s).
For example, I want to retrieve the page views for a certain page for a specific period.
In order to do this I used the following code:
$today = date("Y-m-d");
$until = strtotime($today);
$since = strtotime("2013-08-01");
$pageID = "123";
$page_info = $facebook->api("/$pageID?fields=access_token");
$access_token = $page_info['access_token'];
$params = array(
'access_token' => $access_token,
'since' => $since,
'until' => $until,
);
$insights = $facebook->api("/{$pageID}/insights/page_views/",
"GET",
$params
);
print_r($insights);
The problem is that the result is...somehow empty. More precisely, I receive the following:
Array
(
[data] => Array
(
)
[paging] => Array
(
[previous]=>https://graph.facebook.com/123/insights/page_views/since=1370059200&until=1375329600
[next]=>https://graph.facebook.com/123/insights/page_views/since=1380600000&until=1385870400
)
)
When I use, the same logic to receive insights for my app (without using $token in the $params array), I receive the right data.
In addition to that, I was wondering if there is a way to create charts with this data (directly from FB).
Thx,
Antonis
I managed to solve the problem.
The returning array was empty, because I forgot to check if the user is logged in through facebook...
Regarding the charts, I didn't find any solution directly from facebook, so I used the Charts.js plugin.

Add coordinate data to a Facebook Event

Right now, as far as I know, the Facebook Graph API lets you add a location to an event as a string value. Users are able to add location data to events manually though, by specifying a Facebook place or an address, and it looks like Facebook natively uses Bing maps to display the location. Does anyone know if there are any plans to update the API to let developers add location data properly to events?
The API already supports location data.
Facebook will display the map correctly if you specify a location_id which could be the ID of a place page
Here is a code example...
$facebook->setFileUploadSupport(true);
$event = array(
'name' => stripslashes ($name),
'description' => stripslashes ($description),
'start_time' => $start_time,
'end_time' => $end_time,
'privacy_type' => $privacy,
'location_id' => $location_id
);
$event[basename($flyer_file)] = '#' . realpath($flyer_file);
try {
$result = $facebook->api('me/events','post',$event);
} catch (FacebookApiException $e) {
$error = "Facebook Error: $e";
}

problems with multiples invitations to a event on facebook

I'm tried use the multiples invitations method of facebook-graph API.
of according the documentation,this is the syntax to send multiples invitations:
/EVENT_ID/invited?users=USER_ID1,USER_ID2,USER_ID3
I wrote this code:
$ids = 'id123,id12345';
$ch = curl_init("https://graph.facebook.com/$e_id/invited?users=$ids?access_token={$token}");
curl_setopt_array($ch,
array(CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_CUSTOMREQUEST => 'POST'
)
);
I'm getting the following error:
{"error":{"message":"(#114) An id must be a valid ID string (e.g., \"123\")","type":"OAuthException"}}
How I fix this? Thanks in advance. :)
As it is written in error message it should be numeric strings i.e. "12345" and not "id12345", try real uids and don't invite your own uid.
hope this helps

Facebook Graph API - Same access token for all users

I have been madly trying to get access token for my users since long. I read at many sites to get it somehow by using getSession() and getting access_token from it... it gives me undefined function error.. I googled that too and all solutions said to use the updated SDK but mine is updated and it still won't work... so I finally got another solution to getting access token but this seems to be giving same access token for all users... any idea where the problem lies? All users certainly can't have same token right?
$app_id = $facebook->getAppId();
$app_secret = $facebook->getApiSecret();
function callFb($url, $params)
{
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_POSTFIELDS => http_build_query($params),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_VERBOSE => true
));
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$params=array('client_id'=>$app_id, 'type'=>'client_cred', 'client_secret'=>$app_secret);
$url = "https://graph.facebook.com/oauth/access_token";
$access_token = callFb($url, $params);
$access_token = substr($access_token, strpos($access_token, "=")+1, strlen($access_token));
The issue is that you are asking for type=client_cred, which tells Facebook that you don't want access token for a user, but an access token for the app. This is used for doing things like accessing insights, the realtime updates API, and public data. If you want to get user data, you should not be passing that flag.
If you really do want to roll your own access to the Graph API, you can certainly do that, following the instructions at https://developers.facebook.com/docs/authentication/ .
You say you're using the PHP SDK but I don't see any mention of it anywhere in your code.
The proper way is this:
<?php
require('facebook.php');
$fb = new Facebook(array('appId' => APP_ID, 'secret' => SECRET));
$user = $fb->getUser();
// if we have a valid user, we're logged in
if ($user)
{
// do stuff with the API using the $fb object
}
else
{
// redirect the user to the authentication page
header("Location: ".$fb->getLoginUrl());
}
Example largely paraphrased from Facebook's SDK Github.

Facebook Graph API: How to read the "likes" of a page?

I try to read the list of people who likes a page, but it looks like it is not possible with the Graph API or FQL. I found lots of entries of people trying it, but nobody found a solution. Is it possible?
Here I get the profile info, I see the field "likes" with the number 200
https://graph.facebook.com/162253007120080/
But when I try to read it, I get an empty data array?
https://graph.facebook.com/162253007120080/likes
Anybody an idea how to handle this?
HI,
here you go, streight out my tool box:
<?php
require_once('../library/Facebook.php');
$facebook = new Facebook(array(
'appId' => '123456789000000',
'secret' => 'asdf',
'cookie' => true,
));
$result = $facebook->api(array(
'method' => 'fql.query',
'query' => 'select fan_count from page where page_id = 1234567890;'
));
echo $result[0]['fan_count'];
?>
Call the graph api link. Extract the contents using json_decode.
The resulting data array isn't empty when you go there, it is:
{
"error": {
"message": "An access token is required to request this resource.",
"type": "OAuthException",
"code": 104
}
}
Which means you need an access token in order to access that LIKE data..