简体   繁体   English

对 php curl 请求的 Json 响应

[英]Json response on php curl request

I have sent a curl request, and I'm outputing the response this way.我已经发送了一个 curl 请求,我正在以这种方式输出响应。

$output = curl_exec($ch);
$json = json_decode($output, true);
echo $output;
echo $json;

This is the output.这是输出。

HTTP/1.1 201 Created Server: nginx/1.15.8 Date: Wed, 02 Sep 2020 23:53:01 GMT Content-Type: application/json Transfer-Encoding: chunked Connection: keep-alive X-Content-Type-Options: nosniff X-XSS-Protection: 1; mode=block Cache-Control: no-cache, no-store, max-age=0, must-revalidate Pragma: no-cache Expires: 0 X-Frame-Options: DENY Strict-Transport-Security: max-age=31536000; includeSubdomains { "orderRef" : "4352fa96-05cc-400e-854c-808e5da7dfd7", "autoStartToken" : "84ead0b7-1ee8-40ed-be82-1168e7cbb16b", "message" : "" }

I'm trying to decode the json part, but $json is just null.我正在尝试解码 json 部分,但 $json 只是 null。 I'd like to be able to read $json["autoStartToken"].我希望能够读取 $json["autoStartToken"]。 How would I achieve this?我将如何实现这一目标?

As catcon mentions :正如catcon 提到的

Right now your curl request also return the header so you can't directly use json_decode with the respond, try putting curl_setopt($ch, CURLOPT_HEADER, 0);现在您的 curl 请求也返回标头,因此您不能直接将 json_decode 与响应一起使用,请尝试放置curl_setopt($ch, CURLOPT_HEADER, 0); before the curl_exec($ch)curl_exec($ch)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM