理解curl调用 - curl_exec返回false

时间:2016-04-25 12:09:32

标签: php curl php-curl

我写了下面的代码并且无法获得任何卷曲响应我已经提到了下面的调试信息:

有人可以帮忙找出为什么这个问题curl_exec返回false。

$jwtToken       = getToken();
$service_url    = "https://example.com/api/Demo/GetProjectAttributes/1484";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $service_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt( $ch, CURLOPT_HTTPHEADER, array (
                "Content-Type: application/x-www-form-urlencoded",
                "Authorization: Bearer $jwtToken"
) );

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/mycert.crt");

$response = curl_exec($ch);

if ($response === false ) {
    $info = curl_getinfo($ch);
    echo '<pre>';
    die('error occured during curl exec. Additioanl info: ' . var_export($info));
    curl_close($ch);
}

我的卷曲失败了,我收到了以下信息:

array (
  'url' => 'https://example.com/api/Demo/GetProjectAttributes/1484',
  'content_type' => NULL,
  'http_code' => 0,
  'header_size' => 0,
  'request_size' => 0,
  'filetime' => -1,
  'ssl_verify_result' => 0,
  'redirect_count' => 0,
  'total_time' => 0.57699999999999996,
  'namelookup_time' => 0,
  'connect_time' => 0.28100000000000003,
  'pretransfer_time' => 0,
  'size_upload' => 0,
  'size_download' => 0,
  'speed_download' => 0,
  'speed_upload' => 0,
  'download_content_length' => -1,
  'upload_content_length' => -1,
  'starttransfer_time' => 0,
  'redirect_time' => 0,
  'redirect_url' => '',
  'primary_ip' => '104.209.135.21',
  'certinfo' => 
  array (
  ),
  'primary_port' => 443,
  'local_ip' => '192.168.1.4',
  'local_port' => 56491,
)

1 个答案:

答案 0 :(得分:0)

PHP cURL扩展提供了在请求期间显示错误的函数(如果失败)。

尝试使用它们:

$response = curl_exec($ch);

if (!$response || curl_errno($ch) !== CURLE_OK)
{
    echo 'cURL error (' . curl_errno($ch) . '): ' . curl_error($ch);
}