Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling big user IDs returned by FQL in PHP

Tags:

I'm using FQL to retrieve a list of users from Facebook. For consistency I get the result as JSON. This causes a problem - since the returned JSON encodes the user IDs as numbers, json_decode() converts these numbers to floating point values, because some are too big to fit in an int; of course, I need these IDs as strings.

Since json_decode() does its own thing without accepting any behavior flags, I'm at a loss. Any suggestions on how to resolve this?

like image 504
ggambett Avatar asked May 25 '10 19:05

ggambett


2 Answers

json_decode() can convert large integers to strings, if you specify a flag in the function call:

$array = json_decode($json, true, 512, JSON_BIGINT_AS_STRING)
like image 187
Björn Avatar answered Sep 24 '22 03:09

Björn


I've resolved the issue by adding &format=json-strings to my the FQL api call, like so:

$myQuery = "SELECT uid2 FROM friend WHERE uid1=me()";
$facebook->api("/fql?q=" . urlencode($myQuery) . "&format=json-strings")

This tells facebook to wrap all the numbers in quotes, which leads json_decode to use neither int-s not floats.

Because I was afraid this issue is not restricted to FQL but to all graph API calls that choose to represent some of the IDs as BIG-INTs I've went as far as patching facebook's PHP SDK a bit to force Facebook to return all of its numbers as strings.

I've added this one line to the _graph function. This would be line 738 in facebook_base.php, version 3.1.1

$params['format'] = 'json-strings';

Sure fix

like image 29
Michael Vain Avatar answered Sep 27 '22 03:09

Michael Vain