Tim
Tim

Reputation: 424

PHP curl_exec returns false & curl_error returns empty string

When attemtping to execute a get requests and fetch the answer body, curl_exec returns false and curl_error returns an empty string.

The code is as follows :

$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);

Please note that I have tried adding the CURLOPT_AUTOREFERER as an attempt to debug the request, same for CURLOPT_USERAGENT set to firefox. I had the same result without these options.

Also please note that the CURLOPT_URL I am attempting to request is 100% valid (I have var_dumped the opts array to make sure that the calculated URL would be correct, working and returning data, and it is).

The output goes as follows :

# 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

Obviously something is going very wrong with the request but I can not seem to get an error status. Perhaps the server configuration could disallow Curl ?

Upvotes: 3

Views: 2688

Answers (1)

Paolo
Paolo

Reputation: 15847

From both the code posted and the "var dump" it looks like you have a newline in the URL, try

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

Upvotes: 1

Related Questions