从PHP curl获取空响应

时间:2019-04-16 04:17:35

标签: php-7 php-curl

获取空消息作为响应PHP CURL

我尝试了print_r,http代码是200,即使CURLOPT_VERBOSE也没有显示错误。但是,它没有返回任何值。

curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "http://dummy.com/xxx",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_VERBOSE=>true,
  CURLOPT_TIMEOUT => 50,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => $payload,
    CURLOPT_HTTPHEADER => array(
      "Authorization: Basic xxx",
    ),
  ));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "error:" . $err;
} else {
  echo "response: ".$response;
}

我得到

response: 
when I do CURLOPT_VERBOSE, this returned:
*   Trying xxx.xx.xx.xxx...
* TCP_NODELAY set
* Connected to xxx.xx.xx.xxx (xxx.xx.xx.xxx) port xxxx (#0)
> POST /jw/web/json/plugin/org.joget.webservices.JsonRegistrationApiService/service HTTP/1.1
Host: xxx.xx.xxx.xxx
Accept: */*
Authorization: Basic Yxxxxxx
Content-Length: 775
Expect: 100-continue
Content-Type: application/json; boundary=------------------------916024cad4258a00

< HTTP/1.1 100 
< HTTP/1.1 200 
< Set-Cookie: JSESSIONID=4ED88EE9FD9746ED9C5F345F713FDB69; Path=/jw; HttpOnly
< X-Content-Type-Options: nosniff
< Content-Type: application/json;charset=utf-8
< Content-Length: 0
< Date: Tue, 16 Apr 2019 06:19:00 GMT
< 
* Connection #0 to host xx.xx.xx.xxx left intact

有任何PHP大师需要帮助吗?

2 个答案:

答案 0 :(得分:0)

Content-Length: 0表示您内容的长度为零。

实际上,在POST请求中不返回任何内容是一个好习惯。因此,您的curl代码可能工作正常。

此响应的目的只是为您设置会话cookie。使用此Cookie,您可以发送其他已被授权的请求。

答案 1 :(得分:0)

我找到了答案,有效载荷必须是json_encode()-ed。这很糟糕