如何处理FQL返回的大int facebook uid?

Vij*_*jay 1 facebook facebook-fql facebook-graph-api

我在处理facebook的大用户标识并将其正确存储到我的数据库中时遇到了问题.

由于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)"
);
Run Code Online (Sandbox Code Playgroud)

我在Graph API资源管理器中尝试了以上操作,结果是这样的,

  {
  "data": [
     {
      "uid": 100003082853071, 
      "name": "Sam jones", 
      "sex": "male", 
      "relationship_status": null
     }  
   ]
  }
Run Code Online (Sandbox Code Playgroud)

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

但是,当我直接调用GRAPH API之类的graph.facebook.com/1585213/friends"返回数据"时

{
  "data": [
    {
      "name": "Vijay Kannan", 
      "id": "102937142343"
    }
  ]  
}
Run Code Online (Sandbox Code Playgroud)

请注意,id以字符串形式返回..

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

我如何将它们转换为适当的uid并存储/处理它们.

我认为FQL和GRAPH API调用的不一致也应该由Facebook照顾.但我不能等待!

有什么想法吗?

Vij*_*jay 6

我尝试了大多数方法,并在谷歌之后更多的论坛和Facebook代码列表如果发现以下对我来说就像一个魅力.

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

$friends = json_decode(preg_replace('/"uid":(\d+)/', '"uid":"$1"', $result),true); 
// consider $result as the result rendered by the FQL query.
Run Code Online (Sandbox Code Playgroud)

当我使用了FB的的file_get_contents叫你能看到错误代码的错误,所以最好的方式来与走的是使用curl所有FB API调用必要的时候.

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

$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));
Run Code Online (Sandbox Code Playgroud)

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