Asked  7 Months ago    Answers:  5   Viewed   29 times

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?

 Answers

32

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)
Wednesday, March 31, 2021
 
relipse
answered 7 Months ago
80

So, I'm posting the answer to my original question. I was able to circumvent the 5000 row limit on FQL queries by chunking the array of UIDs (using the appropriately-named array_chunk() PHP function) and looping through the chunks to execute mini-queries, and then appending it all back into one array. The whole script averages 14 seconds for over 12,000 rows so that is a huge improvement. You can see the application at work here: givememydata.com

Oh, and Facebook should reconsider their (still undocumented) FQL row limit. What is more taxing on their servers? A single query that executes in 5 seconds or 500 queries that take 180 seconds? Sorry, had to vent. ;-)

Wednesday, March 31, 2021
 
Skipper
answered 7 Months ago
96

The only way to get the users e-mail address is to request extended permissions on the email field. The user must allow you to see this and you cannot get the e-mail addresses of the user's friends.

http://developers.facebook.com/docs/authentication/permissions

You can do this if you are using Facebook connect by passing scope=email in the get string of your call to the Auth Dialog.

I'd recommend using an SDK instead of file_get_contents as it makes it far easier to perform the Oauth authentication.

Sunday, June 6, 2021
 
Naveen
answered 5 Months ago
52

The exit code of last command is contained in $?.

Use below pseudo code:

python myPythonScript.py
ret=$?
if [ $ret -ne 0 ]; then
     #Handle failure
     #exit if required
fi
Thursday, July 29, 2021
 
Floris
answered 3 Months ago
37

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.

Monday, August 9, 2021
 
Thomas
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :