我正在使用此代码输出到我的错误日志:
ob_start();
var_dump($_COOKIE['agl-values']);
error_log(ob_get_clean());
输出为:
[02-APR-2018 16:12:58 UTC] String(321( " {" latitude ":" 42.2470259 ","经度":" - 71.1755274 ",","高度": " nan " nan ",", "准确"准确":, " altitudeaccuracy ":" nan "," heading ":" nan "," speed ":" nan ", " error_code ":error_message ":" "," php_time ":1522684274," php_date ":" 2018-04-02 15:51:14 "," php_date_format ":" y-m-d H:i:i:s "," user_id ":0}"
[02-APR-2018 16:12:58 UTC] String(321( " {" latitude ":" 42.2470259 ","经度":" - 71.1755274 ",","高度": " nan " nan ",", "准确"准确":, " altitudeaccuracy ":" nan "," heading ":" nan "," speed ":" nan ", " error_code ":error_message ":" "," php_time ":1522684274," php_date ":" 2018-04-02 15:51:14 "," php_date_format ":" y-m-d H:i:i:s "," user_id ":0}"
[02-APR-2018 16:12:58 UTC] String(321( " {" latitude ":" 42.2470259 ","经度":" - 71.1755274 ",","高度": " nan " nan ",", "准确"准确":, " altitudeaccuracy ":" nan "," heading ":" nan "," speed ":" nan ", " error_code ":error_message ":" "," php_time ":1522684274," php_date ":" 2018-04-02 15:51:14 "," php_date_format ":" y-m-d H:i:i:s "," user_id ":0}"
[02-APR-2018 16:12:58 UTC] String(321( " {" latitude ":" 42.2470259 ","经度":" - 71.1755274 ",","高度": " nan " nan ",", "准确"准确":, " altitudeaccuracy ":" nan "," heading ":" nan "," speed ":" nan ", " error_code ":error_message ":" "," php_time ":1522684274," php_date ":" 2018-04-02 15:51:14 "," php_date_format ":" y-m-d H:i:i:s "," user_id ":0}"
我只需要访问纬度和经度变量。我如何隔离其中一个cookie,然后如何将其变成JSON?
update
我检查了json_last_error()
,它告诉我字符串有语法错误 - 是否是逃脱的双引号?
看起来JSON("
(中有一些逃脱的引号,这意味着json_decode
将在语法错误中失败。尝试使用str_replace
将其更改为常规报价("
(:
json_decode(str_replace('"', '"', $_COOKIE['agl-values']))
,要获得纬度和经度,请执行这样的事情:
$aglValues = json_decode(str_replace('"', '"', $_COOKIE['agl-values']));
var_dump($aglValues->latitude);
var_dump($aglValues->longitude);
cookie已经处于JSON格式,您需要正确解码。
使用:
print_r(json_decode($_COOKIE['agl-values'], true));
或单个值:
echo json_decode($_COOKIE['agl-values'], true)['latitude'];
使用默认JSON DEDODE对象:
echo json_decode($_COOKIE['agl-values'])->latitude;
看起来它是作为字符串存储的。转换为JSON对象,然后参考:
$value = json_decode($_COOKIE['agl-values']);
echo $value->longitude . ' ' . $value->latitude;
update
这是您的字符串的直接示例:
php > $var = json_decode("{"latitude":"42.2470259","longitude":"-71.1755274","altitude":"NaN","accuracy":"29","altitudeAccuracy":"NaN","heading":"NaN","speed":"NaN","error_code":"","error_message":"","php_time":1522684274,"php_date":"2018-04-02 15:51:14","php_date_format":"Y-m-d H:i:s","user_id":0}");
php > echo $var->longitude;
-71.1755274