PHP cURL选项CURLOPT_标头和CURLOPT_RETURNTRANSFER是否冲突

PHP cURL选项CURLOPT_标头和CURLOPT_RETURNTRANSFER是否冲突,php,curl,Php,Curl,我使用cURL和php对API进行身份验证。 像这样: $ch = curl_init(); $headers = []; $headers[] = 'Content-Type: application/json'; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_URL, $this->url); curl_setopt($ch, CURLOPT_POST, 1); curl_set

我使用cURL和php对API进行身份验证。 像这样:

$ch = curl_init();
$headers    = [];
$headers[]  = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $this->body ));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$this->response = json_decode( curl_exec($ch) );
curl_close ($ch);
请求响应的主体包含状态代码,如果请求已成功,则包含用户对象。这是一个匿名请求,它在响应头中返回令牌

我的问题是: 脚本的上述响应为null

如果我注释掉CURLOPT_RETURNTRANSFER选项,那么我得到了我所需要的,但它得到了响应,响应为1

如果我注释掉CURLOPT_HEADER选项,那么我只得到响应的主体

我尝试过在http和https之间切换


我使用的是PHP-5.5.27。

你在盲目解码,却不确定你得到的是JSON。简单回答:不是。它是包含标题的文本,后面可能还有一些JSON

做一些类似于:

$ch = curl_init();
$headers    = [];
$headers[]  = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $this->body ));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
$actualResponseHeaders = (isset($info["header_size"]))?substr($response,0,$info["header_size"]):"";
$actualResponse = (isset($info["header_size"]))?substr($response,$info["header_size"]):"";

$this->response = json_decode( $actualResponse );
curl_close ($ch);

更改$this->response=json_decode(curl_exec($ch));到$this->response=curl\u exec($ch);然后让我知道$this->响应是什么。您可能返回了一个无效的json对象。