How to handle the big int facebook uid returned by

2020-02-23 05:00发布

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?

3条回答
地球回转人心会变
2楼-- · 2020-02-23 05:09

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')
查看更多
家丑人穷心不美
3楼-- · 2020-02-23 05:25

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.

查看更多
贼婆χ
4楼-- · 2020-02-23 05:27

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.

查看更多
登录 后发表回答