PHP- 从 curl 访问 json 响应数据
PHP- Access json response data from curl
我成功回复了。但我无法解析它:
"HTTP/1.1 200 OK\r\nDate: Mon, 16 Mar 2020 13:44:57 GMT\r\nContent-Type: application/json\r\nContent-Length: 369\r\nConnection: keep-alive\r\nx-amzn-RequestId: 9820238c-fe40-47dd-a848-3b2d01a929a2\r\nx-amz-apigw-id: JfFh7HBmrPEFrQw=\r\nX-Amzn-Trace-Id: Root=1-5e6f82d9-dff16fe84c4297a6aa2a3ce4;Sampled=0\r\n\r\n{\"ok\":true,\"request\":\"phone\",\"token\":\"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.\"}"
我在消息数据(令牌)中有我需要的内容。
json_decode doesn't work here, it returns {}
根据您发布的原始回复数据...
JSON 字符串一团糟。它是 JSON 字符串中的 JSON 字符串,该字符串还包含换行符分隔的字符串。看看乱七八糟的评论。
这会起作用,但它确实假设您每次都会在同一事件中始终获得内部隐藏 JSON
$s = '{
"success": true,
"data": null,
"message": "HTTP/1.1 200 OK\r\nDate: Mon, 16 Mar 2020 12:50:37 GMT\r\nContent-Type: application/json\r\nContent-Length: 369\r\nConnection: keep-alive\r\nx-amzn-RequestId: 8eb61daa-145b-4e44-b0d1-137ee0bb9203\r\nx-amz-apigw-id: Je9kkGxFLPEFRbA=\r\nX-Amzn-Trace-Id: Root=1-5e6f761d-9c71b10297e40326ec764c9c;Sampled=0\r\n\r\n{\"ok\":true,\"request\":\"phone\",\"token\":\"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9\"}"
}';
$j1 = json_decode($s);
$yuk = explode("\n", $j1->message);
$j2 = json_decode($yuk[9]);
echo $j2->token;
结果
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
<?php
$s = "HTTP/1.1 200 OK\r\nDate: Mon, 16 Mar 2020 13:44:57 GMT\r\nContent-Type: application/json\r\nContent-Length: 369\r\nConnection: keep-alive\r\nx-amzn-RequestId: 9820238c-fe40-47dd-a848-3b2d01a929a2\r\nx-amz-apigw-id: JfFh7HBmrPEFrQw=\r\nX-Amzn-Trace-Id: Root=1-5e6f82d9-dff16fe84c4297a6aa2a3ce4;Sampled=0\r\n\r\n{\"ok\":true,\"request\":\"phone\",\"token\":\"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.\"}";
$tmp = explode("\r\n", $s);
// output what we have
foreach($tmp as $row) {
echo "$row\n";
}
// Detect the row we want
if ( substr($row,0,1) == '{' ) {
$decoded = json_decode($row);
echo "token: {$decoded->token}\n";
}
结果:
HTTP/1.1 200 OK
Date: Mon, 16 Mar 2020 13:44:57 GMT
Content-Type: application/json
Content-Length: 369
Connection: keep-alive
x-amzn-RequestId: 9820238c-fe40-47dd-a848-3b2d01a929a2
x-amz-apigw-id: JfFh7HBmrPEFrQw=
X-Amzn-Trace-Id: Root=1-5e6f82d9-dff16fe84c4297a6aa2a3ce4;Sampled=0
{"ok":true,"request":"phone","token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9."}
token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.
我成功回复了。但我无法解析它:
"HTTP/1.1 200 OK\r\nDate: Mon, 16 Mar 2020 13:44:57 GMT\r\nContent-Type: application/json\r\nContent-Length: 369\r\nConnection: keep-alive\r\nx-amzn-RequestId: 9820238c-fe40-47dd-a848-3b2d01a929a2\r\nx-amz-apigw-id: JfFh7HBmrPEFrQw=\r\nX-Amzn-Trace-Id: Root=1-5e6f82d9-dff16fe84c4297a6aa2a3ce4;Sampled=0\r\n\r\n{\"ok\":true,\"request\":\"phone\",\"token\":\"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.\"}"
我在消息数据(令牌)中有我需要的内容。
json_decode doesn't work here, it returns {}
根据您发布的原始回复数据...
JSON 字符串一团糟。它是 JSON 字符串中的 JSON 字符串,该字符串还包含换行符分隔的字符串。看看乱七八糟的评论。
这会起作用,但它确实假设您每次都会在同一事件中始终获得内部隐藏 JSON
$s = '{
"success": true,
"data": null,
"message": "HTTP/1.1 200 OK\r\nDate: Mon, 16 Mar 2020 12:50:37 GMT\r\nContent-Type: application/json\r\nContent-Length: 369\r\nConnection: keep-alive\r\nx-amzn-RequestId: 8eb61daa-145b-4e44-b0d1-137ee0bb9203\r\nx-amz-apigw-id: Je9kkGxFLPEFRbA=\r\nX-Amzn-Trace-Id: Root=1-5e6f761d-9c71b10297e40326ec764c9c;Sampled=0\r\n\r\n{\"ok\":true,\"request\":\"phone\",\"token\":\"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9\"}"
}';
$j1 = json_decode($s);
$yuk = explode("\n", $j1->message);
$j2 = json_decode($yuk[9]);
echo $j2->token;
结果
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
<?php
$s = "HTTP/1.1 200 OK\r\nDate: Mon, 16 Mar 2020 13:44:57 GMT\r\nContent-Type: application/json\r\nContent-Length: 369\r\nConnection: keep-alive\r\nx-amzn-RequestId: 9820238c-fe40-47dd-a848-3b2d01a929a2\r\nx-amz-apigw-id: JfFh7HBmrPEFrQw=\r\nX-Amzn-Trace-Id: Root=1-5e6f82d9-dff16fe84c4297a6aa2a3ce4;Sampled=0\r\n\r\n{\"ok\":true,\"request\":\"phone\",\"token\":\"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.\"}";
$tmp = explode("\r\n", $s);
// output what we have
foreach($tmp as $row) {
echo "$row\n";
}
// Detect the row we want
if ( substr($row,0,1) == '{' ) {
$decoded = json_decode($row);
echo "token: {$decoded->token}\n";
}
结果:
HTTP/1.1 200 OK
Date: Mon, 16 Mar 2020 13:44:57 GMT
Content-Type: application/json
Content-Length: 369
Connection: keep-alive
x-amzn-RequestId: 9820238c-fe40-47dd-a848-3b2d01a929a2
x-amz-apigw-id: JfFh7HBmrPEFrQw=
X-Amzn-Trace-Id: Root=1-5e6f82d9-dff16fe84c4297a6aa2a3ce4;Sampled=0
{"ok":true,"request":"phone","token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9."}
token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.