Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the importance of setting CURLOPT_HTTP_VERSION in an application?

Tags:

http

php

curl

In someone else's code I came across this option setting for cURL:

curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);

The PHP documentation says that by default this option is set to CURL_HTTP_VERSION_NONE which lets cURL decide which HTTP version to use. Otherwise, you can force HTTP 1.0 or HTTP 1.1. Someday there will be the option to force HTTP 2.0 (see this thread on the cURL mailing list: http://curl.haxx.se/mail/lib-2013-09/0020.html)

I am still trying to understand the differences between HTTP 1.0 vs 1.1 from the question HTTP 1.0 vs 1.1 and now I'm wondering what kind of considerations are needed for the future with HTTP 2.0.

My questions are:

  • Is setting the CURLOPT_HTTP_VERSION in an app a good idea if I can't always be sure what HTTP version the server is capable of? Or should I detect the version using $_SERVER['SERVER_PROTOCOL'] and change the CURLOPT_HTTP_VERSION based on that?

  • If I do know the server is capable of HTTP 1.1 (or someday HTTP 2.0) is there any reason to think cURL won't be able to figure this out?

  • Is there a case in which it's better to use HTTP 1.0 rather than HTTP 1.1?

like image 291
Sean Fahey Avatar asked Sep 25 '13 22:09

Sean Fahey


People also ask

What is Curlopt_http_version?

CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE. Issue non-TLS HTTP requests using HTTP/2 without HTTP/1.1 Upgrade. It requires prior knowledge that the server supports HTTP/2 straight away. HTTPS requests will still do HTTP/2 the standard way with negotiated protocol version in the TLS handshake. ( Added in 7.49.0)

What is the use of cURL in PHP?

cURL 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. Obtaining a copy of a website's material. Submission of forms automatically, authentication and cookie use.

What is Curlopt_encoding?

CURLOPT_ENCODING only tells the server what types of encoding it will accept. So either way the data will only be decoded if needed. But by giving CURLOPT_ENCODING a value you are limiting your call to only accept one type of encoding.

What is Curlopt_returntransfer?

CURLOPT_RETURNTRANSFER. true to return the transfer as a string of the return value of curl_exec() instead of outputting it directly.


1 Answers

  1. I don't see any benefit. Let curl deal with that.

  2. Curl will do that for you. And I'm pretty sure the future HTTP 2.0 will not break backward compatibility.

  3. As stated in HTTP 1.0 vs 1.1, the only valid reason to prefer HTTP 1.0 is when you cannot send a Host header to the server. But honestly I can't imagine a real world situation.

like image 177
voondo Avatar answered Sep 19 '22 20:09

voondo