【问题标题】:How to handle the big int facebook uid returned by FQL?如何处理 FQL 返回的大 int facebook uid?
【发布时间】:2012-03-01 15:48:58
【问题描述】:

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

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

我想获取我的有性朋友的列表,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 中尝试了上述方法,结果是这样的,

  {
  "data": [
     {
      "uid": 100003082853071, 
      "name": "Sam jones", 
      "sex": "male", 
      "relationship_status": null
     }  
   ]
  }

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

但是当我直接调用 GRAPH API 时,像“graph.facebook.com/1585213/friends”这样将数据返回为

{
  "data": [
    {
      "name": "Vijay Kannan", 
      "id": "102937142343"
    }
  ]  
}

注意 id 作为 string..

返回

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

如何将它们转换为正确的 uid 并存储/处理它们。

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

对此有什么想法吗?

【问题讨论】:

    标签: facebook facebook-graph-api facebook-fql


    【解决方案1】:

    我尝试了大多数方法,并在 Google 之后找到更多论坛和 facebook 代码列表,如果发现以下对我来说就像一个魅力。

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

    $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。

    请找到我用来获得正确结果的完整代码,

    $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 解决此不一致之前对其他人有所帮助。

    【讨论】:

      【解决方案2】:

      Facebook 有两点非常一致。它们是:1) 随心所欲地更改 API,无需任何提示。 2)graph和fql对象不一致。

      正如您所指出的,从 Facebook 返回的不带引号的值始终是 long(又名 big int,又名 Int64)。并且引用的值是 long 值的字符串表示形式。

      在我看来,$facebook->api 调用正在将多头转换为浮点数。我建议将其记录为 $facebook->api 团队的错误。

      在他们修复该错误期间,您可以编写自己的代码以将 HTTP 发布到图形并解析返回的结果。我在 C# API 和 Javascript API 中都没有遇到这个问题。

      【讨论】:

        【解决方案3】:

        如果您使用的是 php (http://php.net/manual/en/function.sprintf.php):

        printf("%14.0f", 1.00000145202E+14);
        

        输出:

        100000145202000
        

        Javascript:

        parseFloat('1.00000145202E+14')
        

        【讨论】:

        • 我会立即结交很多朋友(通常是 100,甚至可能是 5000).. 对于每个返回的 uid,我无法检查指数,然后让他们工作或转换..
        • 为什么不能检查?您需要知道是否需要解析它。
        • 然后我应该编写代码来找到指数部分,然后为每个元素应用srinf...我认为除了这一轮之外,肯定还有其他简单的解决方案...
        • 如果您没有找到任何其他解决方案,您可以在 javascript 中使用indexOf。祝你好运
        猜你喜欢
        • 1970-01-01
        • 2011-02-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多