Facebook ID整数被奇怪地返回(Facebook ID integers being returned oddly)

系统教程 行业动态 更新时间:2024-06-14 17:02:17
Facebook ID整数被奇怪地返回(Facebook ID integers being returned oddly)

我正在使用以下URL进行FQL调用,然后进行卷曲调用。

$ url ='https://api.facebook.com/method/fql.query?access_token='.$access_token.'&query='.rawurlencode($query).'&format=JSON';

然后我通过json_decode调用传递返回的数据

我有这个查询:

SELECT name,page_id,page_url FROM page WHERE page_id IN(SELECT page_id FROM page_admin WHERE uid = $ uid)

它返回指定的UID是管理员的名称和页面的列表。

在一些PHP安装中(我还没有能够缩小它),page_id从一个长整型变成了一个科学记数法 - 所以174311849258492返回为1.7431184925849E 14,这当然会破坏事情。

由于我无法在我的服务器上重现此问题,所以我不确定转换的发生位置。 挖掘我发现一个建议,做到这一点:

json_decode(preg_replace('/:(\ d +,)/',':“$ {1}”,',$ response));

将解决它

但为什么一些json_decodes没有明显的理由投入科学记数法?

I'm making FQL calls using the following url and then making a curl call.

$url = 'https://api.facebook.com/method/fql.query?access_token='.$access_token.'&query='.rawurlencode($query).'&format=JSON';

and I then pass the returned data through a json_decode call

I've got this query:

SELECT name,page_id,page_url FROM page WHERE page_id IN (SELECT page_id FROM page_admin WHERE uid= $uid )

which returns a list of the names and pages for which the specified UID is an administrator.

On some PHP installs (and I've not been able to narrow it down) the page_id is turned from a long integer into a scientific notation - so 174311849258492 is returned as 1.7431184925849E 14 which of course breaks things.

As I can't reproduce this on my server I'm not sure where the conversion is happening. Digging around I've found a suggestion that doing this:

json_decode( preg_replace('/:(\d+,)/', ':"${1}",', $response ) );

will fix it

But why do some json_decodes cast into scientific notation for no apparent reason?

最满意答案

如果您正在使用自己的curl调用,那么您可以简单地将&format = JSON-STRINGS附加到URL的末尾,将所有项目作为字符串返回。

As Danny pointed out its a 32/64 bit issue. FB assume that everything is 64 bit and pass back an integer for this value rather than a string.

So what you need to do is take the integers and convert them to strings BEFORE pulling them from the JSON array. Page IDs and Group IDs are passed as integers (Facebook user IDs are passed as strings)

The code to do this is:

$response = curl_exec($ch); $err_no=curl_errno($ch); curl_close($ch); $response=preg_replace('/"gid":(\d+)/', '"gid":"$1"', $response ); $response=json_decode( preg_replace('/"page_id":(\d+)/', '"page_id":"$1"', $response ) ); if (isset($response->message)) { throw new Exception ($response->message); } return( $response);

更多推荐

本文发布于:2023-04-21 18:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/7062d86042f8619dc49c142347877070.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:整数   奇怪   ID   Facebook   returned

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!