如何处理 FQL 返回的 big int facebook uid? [英] How to handle the big int facebook uid returned by FQL?

查看:27
本文介绍了如何处理 FQL 返回的 big int facebook uid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在处理 Facebook 的大用户 ID 并将它们正确存储到我的数据库中时遇到问题..

I've a problem in handling the big userid's of facebook and properly storing them into my database..

由于 fql.query REST api 将被弃用,我使用 GRAPH API 来获取 FQL 的结果.

As the fql.query REST api is going to be deprecated ,I'm using the GRAPH API for getting the results of the FQL.

我想得到我的同性好友列表,relationship_status.

I want to get the list of my friends with sex,relationship_status .

我执行的查询是

$allFriends = $facebook->api("/fql
    ?q=SELECT+uid,+name,+sex,+relationship_status+FROM+user+where+uid+in+
    (SELECT+uid2+FROM+friend+WHERE+uid1+=$fbuid)"
);

我在 Graph API explorer 中尝试了上面的方法,结果是这样的,

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
     }  
   ]
  }

注意 uid 返回为 int,所以每当我打印数组本身时,它的值类似于 (1.34234422 +E03).因此,即使是该数组的 json_encode 也无济于事.

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.

但是当我直接调用 GRAPH API 时,类似 'graph.facebook.com/1585213/friends' 的东西将数据返回为

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"
    }
  ]  
}

注意 id 返回为 string..

Note the id is returned as string..

每当我使用图形 API 调用 FQL 查询时,它都会将整个数据作为 'Array' 返回,因此长的大 Facebook uid 被转换为浮点数 (1.34234422 +E03).

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) .

我如何将它们转换为正确的 uid 并将它们存储/处理回来.

How can i convert them into proper uid's and store/process them back.

我认为 FQL 和 GRAPH API 调用的不一致也应该由 Facebook 处理.但我等不及了!!

I think the inconsistency of FQL and GRAPH API call should be also taken care by Facebook .. But i could not wait for that!!

对此有什么想法吗?

推荐答案

我尝试了大多数方法,然后在 Google 之后搜索了更多论坛和 facebook 代码列表,如果发现以下方法对我来说很有吸引力.

I tried most methods and after Google some more forums and facebook code list if found the following worked like a charm for me.

从 FQL 查询获得结果后,我使用了以下代码行,

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.

当我使用 file_get_contents 进行 FB 调用时,您可能已经看到错误代码的错误,因此最好的方法是在必要时对所有 FB API 调用使用 CURL.

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));

我只是发布这个答案,所以它可以帮助其他人,直到 Facebook 解决这个不一致.

I just to post this answers so it may help others until Facebook resolve this inconsistency.

这篇关于如何处理 FQL 返回的 big int facebook uid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆