PHP, 原创,

使用CodeIgniter4(CI4)遇到的BUG(2) curl_setopt_array cannot represent a stream of type Output as a STDIO FILE

使用CodeIgniter 的 curl

$this->curlClient = \Config\Services::curlrequest();
$result = $this->curlClient->request('GET', $list_domain, [
            CURLOPT_SSL_VERIFYHOST  =>  2,
            CURLOPT_SSL_VERIFYPEER  =>  1,
            CURLOPT_TRANSFERTEXT    =>  false,
        ]);

返回的报错信息是:

ErrorException
curl_setopt_array(): cannot represent a stream of type Output as a STDIO FILE*

导致此问题的 curlopt 是

define ('CURLOPT_STDERR', 10037);

找到 sendRequest函数, 在 system/HTTP/CURLRequest.php的805行

$ch = curl_init();
unset($curl_options[CURLOPT_STDERR]);  //添加此行
curl_setopt_array($ch, $curl_options);

这样就可以解决 cannot represent a stream of type Output as a STDIO FILE* 的错误了。

使用CURL的实例如下

$list_domain = str_replace('https', 'http', $list_domain);
$options = [
	'allow_redirects' => [
		'max'       => 15,
	],
	CURLOPT_SSL_VERIFYPEER  =>  false,
	CURLOPT_SSL_VERIFYHOST  =>  false,
	'headers' => [
		'User-Agent'    =>  'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36',
		'Accept'        =>  'text/html',
		'Referer'       =>  $list_domain,
	],
];
try {
	$result = $this->curlClient->request('GET', $list_domain, $options);
	$content = $result->getBody();
} catch (\Exception $e) {
	log_message_file('curl', $e->getMessage());
	//var_dump($e->getMessage());
	$context = null;
	if( substr($list_domain, 0, 5) == 'https') {
		$list_domain = str_replace('https://', 'http://', $list_domain);
		$options = array(
			'http' => array(
				'method' => 'GET',
				'header' => [
					'Content-type: text/html;charset=UTF-8',
					'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36',
					'Referer: ' . $list_domain,
				],
			),
			// 解决SSL证书验证失败的问题
			"ssl"=>array(
				"verify_peer"=>false,
				"verify_peer_name"=>false,
			)
		);
		$context = stream_context_create($options);
	}
	$content = file_get_contents($list_domain, false, $context);
}
return $content;

(203)

Author Since: Jul 05, 2018

Related Post