dqj5046 2017-12-14 03:11
浏览 200
已采纳

为什么json_decode不起作用?

I am trying to get value of c_user, xs, fr and date value through JSON from below code and save it to database but it is not working here is my code which I used.

$data = $_GET["user"];
$data = json_decode($data);
$datr = $data->datr;
$xs = $data->xs;
$fr = $data->fr;
$c_user = $data->c_user;
$token2 = $data->access_token;
$id = $data->uid;

Here is the Json code

{
    "session_key" : "5.rWdMmED7nybZ1w.1513220229.45-100007001746590",
    "uid" : 100007001746590,
    "secret" : "b3f2dac4a948407864ff2e4e2d8feebf",
    "access_token" : "EAAAAAYsX7TsBAGSNv9YET43NxPk7PZBt1gTP0ipqQpb7ojzgn9pFf8hOLTg6V1R8IAv0y5TYgMhDlbCA0eHUN5aPOYw1DgA4c7vTgscbzY0tZALxpBk1tvIXPAisZBBZBzkyimkBurkU3iz0KmXoLQU1KY7tjxrgl7Wvxf5GwZBQbDFX4m5kMsYYDZB9UwIyl6YJA12Ac2ZBmcQBKAfOotc",
    "machine_id" : "hegxWhFD-5BKWnhTe3exWvAG",
    "session_cookies" : [
        {
            "name" : "c_user",
            "value" : "100007001746590",
            "expires" : "Fri,
             14 Dec 2018 02 : 57 : 09 GMT",
             "expires_timestamp" : 1544756229,
            "domain" : ".facebook.com",
            "path" : "\/",
            "secure" : true
        },
        {
            "name" : "xs",
            "value" : "45 : rWdMmED7nybZ1w : 2 : 1513220229 : 13473 : 4832",
            "expires" : "Fri,
             14 Dec 2018 02 : 57 : 09 GMT",
             "expires_timestamp" : 1544756229,
            "domain" : ".facebook.com",
            "path" : "\/",
            "secure" : true,
            "httponly" : true
        },
        {
            "name" : "fr",
            "value" : "0DfY4nJ3NUUeFKIUG.AWX4RKAQ0wBcl677F8jXJcCZ7Dc.BZj9JZ.fD.Fox.0.0.BaMeiF.AWXzlwrU",
            "expires" : "Fri,
             14 Dec 2018 02 : 57 : 09 GMT",
             "expires_timestamp" : 1544756229,
            "domain" : ".facebook.com",
            "path" : "\/",
            "secure" : true,
            "httponly" : true
        },
        {
            "name" : "datr",
            "value" : "hegxWhFD-5BKWnhTe3exWvAG",
            "expires" : "Sat,
             14 Dec 2019 02 : 57 : 09 GMT",
             "expires_timestamp" : 1576292229,
            "domain" : ".facebook.com",
            "path" : "\/",
            "secure" : true,
            "httponly" : true
        }
    ],
    "confirmed" : true,
    "identifier" : "7250492401"
}

NOTE: I have success in getting $token2 and $id. How to get the values of xs, fr, date, c_user?

  • 写回答

3条回答 默认 最新

  • doumu2172 2017-12-14 03:19
    关注

    Oh, I see. The values you're asking about are elements of a JSON array, with "name" values like "xs" and "fr". You will need to iterate through the array in order to unpack it.

    $cookies = [];
    foreach ($data->session_cookies as $cookie) {
        $cookies[$cookie->name] = $cookie->value;
    }
    $xs = $cookies['xs'];
    // etc.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。