How to use PHP cURL with HTTP/1.1
Asked Answered
M

1

13

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)

Mcclain answered 18/3, 2019 at 5:19 Comment(7)
"curl stops working" what is the error message? Any log?Deflocculate
Thanks for your reply, Sorry but no log is generated when I am using // curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); // curl_setopt($ch, CURLOPT_HTTP_VERSION, '1.1'); It stops and donot give output with $info = curl_getinfo($ch);Mcclain
Looks as curl works but the server ignores it. Have you tried a raw request in telnet terminal?Deflocculate
Yes this works in command prompt. : [ curl.exe -ivk --http1.1 -u USERNAME:PASSWORD server.com/stream ] client's teach team : your Version of curl is newer than mine and apparently automatically tries to switch to http/2 which doesn't workMcclain
set CURLOPT_VERBOSE to ON, It will help the debugging. More info: curl.haxx.se/libcurl/c/CURLOPT_VERBOSE.htmlDeviate
Thanks for reply. Yes I've tested that. This is output for that. prnt.sc/mze0i4Mcclain
Whats your desired output to this? You used to see data earlier which is not coming now or what? Have you considered downgrading cURL version?Deviate
A
5

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);
Accompany answered 12/8, 2021 at 10:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.