Facebook ID 整数返回的方式很奇怪

发布于 2024-12-08 22:22:38 字数 681 浏览 1 评论 0原文

我使用以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情泪▽动烟 2024-12-15 22:22:39

如果您使用自己的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.

谈场末日恋爱 2024-12-15 22:22:39

正如 Danny 指出的那样,这是一个 32/64 位问题。 FB 假设一切都是 64 位,并传回该值的整数而不是字符串。

因此,您需要做的就是获取整数并将其转换为字符串,然后再从 JSON 数组中提取它们。页面 ID 和组 ID 作为整数传递(Facebook 用户 ID 作为字符串传递)

执行此操作的代码是:

    $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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文