简体   繁体   English

cURL 包含无效的 cURL 选项

[英]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:我正在从 PHP 7.4 升级到 PHP 8 突然这个错误出现在我的 cURL 请求中:

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:我使用以下代码构建 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.目标文件始终是 php 扩展名。 'buildPostFields' returns an array of the data. 'buildPostFields' 返回一个数据数组。

Probably this errors accours because of my php upgrade to version 8, but I cannot find any hints in the documentation.由于我的 php 升级到版本 8,可能会出现此错误,但我在文档中找不到任何提示。 I tried removing every line and then tried again.我尝试删除每一行,然后再试一次。 But it didn't help.但这没有帮助。

The error is because you wrapped the constants with double quotes.错误是因为你用双引号包裹了常量。

curl_setopt_array ( CurlHandle $handle, array $options ): bool 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 .键应该是有效的 curl_setopt() 常量或它们的 integer 等效项

So it should be CONSTANT_NAME => value所以它应该是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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM