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

为什么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.
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dthyxna3894 2017-12-14 03:18

    The error seems to be consistent for all of your missing keys. Take xs for example. The code you wrote looks for a child element labeled xs:

    $xs = $data->xs;
    

    But this doesn't exist in your JSON document. Instead, you need to parse through the session_cookies array to find an element where the name key is set to xs. This method is described in this previous answer.

    评论
    解决 无用
    打赏 举报
  • douza19870617 2017-12-14 03:23

    Approximately this is what you are looking for. I guess.

    $data = $_GET["user"];
    $data = json_decode($data);
    $c_user = [];
    foreach($data->session_cookies as $key => $val)
    {
        $c_user[] = $val['name']; 
    }
    

    now in the c_user array you have the value you require.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题