Reputation: 1337
My server cURL version is newer then client's version when doing remote call, client's server automatically switches to http/2, Is there any way i can force to use curl with http/1.1
How to set cURL HTTP version to 1.1, I tested by adding following settings in code, but after adding that curl stops working. All kind of helps are appreciable. Thanks in advance.
curl_setopt($ch, CURLOPT_HTTP_VERSION, '1.1');
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
Is there any way i can use curl with HTTP 1.1 ?
My server versions are
PHP Version 7.2.16
cURL Information 7.62.0
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
//curl_setopt($ch, CURLOPT_HTTP_VERSION, '1.1');
//curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_USERPWD, $user . ':' . $pass);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)');
curl_setopt($ch, CURLOPT_TIMEOUT, 2400); */
curl_setopt($ch,CURLOPT_HTTPHEADER, array(
// "http:1.1",
'http' => array(
'timeout' => 5,
'protocol_version' => 1.1,
'header' => 'Connection: close'
),
"Content-Type: text/event-stream",
"cache-control:no-cache",
"Access-Control-Allow-Origin:*",
'Authorization: Basic '. base64_encode($user.':'.$pass)
));
$error = 0;
$result = curl_exec($ch);
$info = curl_getinfo($ch);
$err = curl_errno($ch);
Client's tech team reply : Problem is -> You starts with version 1.1 then it changes to version 2
Using HTTP2, server supports multi-use
Connection state changed (HTTP/2 confirmed)
Upvotes: 14
Views: 14257
Reputation: 16045
According to the documentation of curl_setopt() the code
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
should be correct. What did you mean with "curl stops working"? What's the actual error you get? You can also try extracting extra more details about the error:
# before curl_exec():
error_reporting(~0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_STDERR, $filehandle);
# maybe also: curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
# maybe also: curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
# after curl_exec():
$curl_error = curl_error($ch);
$curl_error_number = curl_errno($ch);
$http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
Upvotes: 7