Skip to content
Advertisement

cURL contains an invalid cURL option

I was upgrading from PHP 7.4 to PHP 8 and suddenly this errors appears in my cURL request:

Fatal error: Uncaught ValueError: curl_setopt_array(): Argument #2 ($options) contains an invalid cURL option in ...

I use the following code to build the curl:

$options = array (
    "CURLOPT_POST" => true,
    "CURLOPT_HEADER" => true,
    "CURLOPT_URL" => "https://example.example.com/api/example.php",
    "CURLOPT_FRESH_CONNECT" => true,
    "CURLOPT_RETURNTRANSFER" => true,
    "CURLOPT_FORBID_REUSE" => true,
    "CURLOPT_TIMEOUT" => 10,
    "CURLOPT_FAILONERROR" => true,
    "CURLOPT_POSTFIELDS" => $this->buildPostFields($postData),        
    "CURLOPT_HTTPAUTH" => "CURLAUTH_BASIC",
    "CURLOPT_SSL_VERIFYPEER" => false //REMOVE IN PRODUCTION, IGNORES SELFSIGNED SSL            
);            
$ch = curl_init();
curl_setopt_array($ch, $options);

The targeted file is always an php extensions. ‘buildPostFields’ returns an array of the data.

Probably this errors accours because of my php upgrade to version 8, but I cannot find any hints in the documentation. I tried removing every line and then tried again. But it didn’t help.

Advertisement

Answer

The error is because you wrapped the constants with double quotes.

curl_setopt_array ( CurlHandle $handle , array $options ) : bool

options
An array specifying which options to set and their values. The keys should be valid curl_setopt() constants or their integer equivalents.

So it should be CONSTANT_NAME => value

$options = array (
    CURLOPT_POST => true,
    CURLOPT_HEADER => true,
    CURLOPT_URL => "https://example.example.com/api/example.php",
    CURLOPT_FRESH_CONNECT => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_FORBID_REUSE => true,
    CURLOPT_TIMEOUT => 10,
    CURLOPT_FAILONERROR => true,
    CURLOPT_POSTFIELDS => $this->buildPostFields($postData),           
    CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
    CURLOPT_SSL_VERIFYPEER => false //REMOVE IN PRODUCTION, IGNORES SELFSIGNED SSL            
);            
$ch = curl_init();
curl_setopt_array($ch, $options);
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement