Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use PHP cURL with HTTP/1.1

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)

like image 748
Viral Avatar asked Mar 18 '19 05:03

Viral


People also ask

How cURL URL in PHP?

curl_setopt( $ch , CURLOPT_RETURNTRANSFER, 1); //grab URL and pass it to the variable. curl_setopt( $ch , CURLOPT_URL, $url ); $result = curl_exec( $ch );

Can I use cURL in PHP?

Uses of cURL in PHPcURL is a PHP extension that allows you to use the URL syntax to receive and submit data. cURL makes it simple to connect between various websites and domains.

How do I enable http2 on cURL?

11.4. To tell curl to use http2, either plain text or over TLS, you use the --http2 option (that is “dash dash http2”). curl defaults to HTTP/1.1 for HTTP: URLs so the extra option is necessary when you want http2 for that. For HTTPS URLs, curl will attempt to use http2.

How can get cURL value in PHP?

PHP cURL GET requestphp $ch = curl_init('http://webcode.me'); curl_exec($ch); curl_close($ch); In the example, we send a GET request to a small website. The output is directly shown in the standard output.


1 Answers

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);
like image 111
Mikko Rantalainen Avatar answered Oct 06 '22 01:10

Mikko Rantalainen