【问题标题】:Decoding json in php and facebook api在 php 和 facebook api 中解码 json
【发布时间】:2014-01-11 16:04:31
【问题描述】:

我对 json 有点陌生。我从 facebook oauth 得到以下回复。而且我无法获得将数据放入 php 中的变量/对象的正确方法。

{
   "data":{"app_id":xxxxxxxxxx,
           "is_valid":true,
           "application":"Opensource Education Dot in",
           "user_id":xxxxxxxxxx,
           "issued_at":1389448259,
           "expires_at":1394632259,
           "scopes":["basic_info","email","public_profile","user_friends"]
          }

}  

这是我卡住的地方。不知道如何得到每个响应。我也只得到这个作为回应。但是范围内的确切值(如电子邮件、基本信息等)我如何获得它们?

$userdata=json_decode($response);

【问题讨论】:

    标签: php json facebook


    【解决方案1】:

    使用

    $userdata=json_decode($response,true);
    

    对于长用户 ID 问题,您可以使用以下 -

    $userdata= = json_decode(preg_replace('/"uid":(\d+)/', '"uid":"$1"', $response),true);
    

    也可以用作

    $userdata=  json_decode($response, true, 512, JSON_BIGINT_AS_STRING)
    

    它会给你一个数组。

    此外,您可以检查线程以了解更多变化

    How to get around or make PHP json_decode not alter my very large integer values?

    【讨论】:

    • 您好,应用程序 ID 和用户 ID 太长,因此以指数形式获取。我该如何解决这个问题?
    • 我现在已经更新了答案检查。你可以用你的替换 preg_replace 中的 uid,我认为它的 user_id 在你的情况下。
    • 你可以在这里查看我在stackoverflow.com/questions/20931751/…之前回答过的类似的长用户ID问题
    • 我有两个很长的数据。 app_id 和 user_id。所以 user_id 工作正常。但上述格式不接受 4 个参数。那么 app_id 呢
    • 然后使用 json_decode($response, true, 512, JSON_BIGINT_AS_STRING);这会将所有大整数转换为字符串。让我更新答案。
    猜你喜欢
    • 1970-01-01
    • 2013-07-18
    • 2011-11-10
    • 2013-05-01
    • 2012-03-22
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    • 2017-05-31
    相关资源
    最近更新 更多