Viewed   52 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

2

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, December 21, 2022
2

You can get that data in one request, by using the fields parameter:

/me/posts?fields=message,full_picture

(If you want more additional fields, you have to list them as well.)

Thursday, October 27, 2022
1

As you can see in the JSON output, the answer is wrapped in an array:

array(1) {
  [0]=>
  array(9) {
    ["url"]=>
    string(33) "http://www.facebook.com/549585444"
    ["normalized_url"]=>
    string(33) "http://www.facebook.com/549585444"
    ["share_count"]=>
    int(0)
    ["like_count"]=>
    int(0)
    ["comment_count"]=>
    int(0)
    ["total_count"]=>
    int(0)
    ["click_count"]=>
    int(0)
    ["comments_fbid"]=>
    NULL
    ["commentsbox_count"]=>
    int(0)
  }
}

To get the number output, you'll have to get the first item in the array by changing this row:

echo $json_returned['like_count'];

To this:

echo $json_returned[0]['like_count'];
Wednesday, November 16, 2022
2

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
Friday, September 30, 2022
 
tsm
 
tsm
3

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.

Friday, September 9, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :