Facebook ID 整数返回的方式很奇怪
我使用以下 url 进行 FQL 调用,然后进行curl 调用。
$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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用自己的curl 调用,则只需将 &format=JSON-STRINGS 附加到 url 的末尾,该 URL 将所有项目作为字符串返回。
If you are using your own curl calls then you can simply append &format=JSON-STRINGS onto the end of the url which returns all items as strings.
正如 Danny 指出的那样,这是一个 32/64 位问题。 FB 假设一切都是 64 位,并传回该值的整数而不是字符串。
因此,您需要做的就是获取整数并将其转换为字符串,然后再从 JSON 数组中提取它们。页面 ID 和组 ID 作为整数传递(Facebook 用户 ID 作为字符串传递)
执行此操作的代码是:
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: