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条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效