PHP curl_exec returns false & curl_error returns 空字符串

PHP curl_exec returns false & curl_error returns empty string

当尝试执行获取请求并获取答案正文时,curl_exec returns false 和 curl_error returns 一个空字符串。

代码如下:

$curl = curl_init();

$opts = [
    CURLOPT_URL => '
    https://example.com'. $scope.'&item_value[]='.$param.'&api_key=XXXX',
    CURLOPT_RETURNTRANSFER => true, 
    CURLOPT_HEADER => false,
    CURLOPT_VERBOSE => true,
    CURLOPT_AUTOREFERER => true,
    CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17'];

curl_setopt_array($curl, $opts);

var_dump($opts);

$response = curl_exec($curl);

var_dump($response);

if ($response === false) {
    $response = curl_error($curl);
    var_dump(curl_getinfo($curl));
}

var_dump($response);

curl_close($curl);

return($response);

请注意,我已尝试添加 CURLOPT_AUTOREFERER 以尝试调试请求,同样将 CURLOPT_USERAGENT 设置为 firefox。没有这些选项我得到了相同的结果。

另请注意,我尝试请求的 CURLOPT_URL 是 100% 有效的(我有 var_dumped opts 数组以确保计算出的 URL 是正确的,工作和返回数据,它是)。

输出如下:

# var_dump $response

xxx.php:24:boolean false

# var_dump curl_error($curl) since $response == false

xxx.php:31:string '' (length=0)

# var_dump curl_getinfo($curl)

array (size=37)
  'url' => string '
        https://100%_valid_url.com' (length=180)
  'content_type' => null
  'http_code' => int 0
  'header_size' => int 0
  'request_size' => int 0
  'filetime' => int -1
  'ssl_verify_result' => int 0
  'redirect_count' => int 0
  'total_time' => float 0
  'namelookup_time' => float 0
  'connect_time' => float 0
  'pretransfer_time' => float 0
  'size_upload' => float 0
  'size_download' => float 0
  'speed_download' => float 0
  'speed_upload' => float 0
  'download_content_length' => float -1
  'upload_content_length' => float -1
  'starttransfer_time' => float 0
  'redirect_time' => float 0
  'redirect_url' => string '' (length=0)
  'primary_ip' => string '' (length=0)
  'certinfo' => 
    array (size=0)
      empty
  'primary_port' => int 0
  'local_ip' => string '' (length=0)
  'local_port' => int 0
  'http_version' => int 0
  'protocol' => int 0
  'ssl_verifyresult' => int 0
  'scheme' => string '' (length=0)
  'appconnect_time_us' => int 0
  'connect_time_us' => int 0
  'namelookup_time_us' => int 0
  'pretransfer_time_us' => int 0
  'redirect_time_us' => int 0
  'starttransfer_time_us' => int 0
  'total_time_us' => int 0

显然请求出了点问题,但我似乎无法获得错误状态。也许服务器配置可能不允许 Curl?

从发布的代码和 "var dump" 看来你在 URL 中有一个换行符,试试

$opts = [
    CURLOPT_URL => 'https://example.com'.$scope.'&item_value[]='.$param.'&api_key=XXXX',
    // ...