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

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

问题描述



由于fql.query REST api将会是这样的,所以我在处理大型用户ID的Facebook并将其正确存储到我的数据库中时遇到问题。已弃用,我正在使用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资源管理器中尝试过上述结果,结果就是这样,

  {
data:[
{
uid:100003082853071,
名称:Sam jones,
sex:male,
ationship_status:null
}
]
}
<注意,uid被返回为 int ,所以每当我打印数组本身,它的值都是( 1.34234422 + E03)。所以即使是这个数组的json_encode也没有帮助。



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

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

请注意,该ID返回为字符串 ..



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



如何将它们转换成适当的uid并将其存储/处理。



我认为FQL和GRAPH API调用的不一致也应该被Facebook照顾..但是我迫不及待的想要了!



任何想法?

解决方案

我尝试了大多数方法,在Google之后,还有一些更多的论坛和Facebook代码列表,如果发现以下的工作就像一个魅力。



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

  $ friends = json_decode(preg_replace('/uid:( \d +)/','uid:$ 1 $结果),TRUE); 
//将$ result视为FQL查询呈现的结果。

当我使用file_get_contents进行FB调用时,您可能已经看到错误代码的错误,所以最好的方法是在必要时使用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 +朋友+ 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',
//用正确的crt文件路径替换上述路径
//以避免FB
//当我们尝试使用CURL而没有正确的认证文件时提供的异常。
);

$ 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'=>数组(
'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've a problem in handling the big userid's of facebook and properly storing them into my database..

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

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

The query i executed is

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

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

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.

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

Note the id is returned as string..

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

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

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

Any ideas on this?

解决方案

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.

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

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