PHP cURL error HTTP/2 stream 0 was not closed cleanly

Submitted by Tom Thorp on Wednesday, January 24, 2024 - 16:59
Modified on Wednesday, January 24, 2024 - 17:13
PHP Logo

I came across this problem when I updated my version of PHP to version 8.2 on my website. Pages that normally worked with the cURL function would sometimes fail with the above error.

The solution is quite simple. 

Prior to cURL v7.62.0, the default API used to communicate to remote website was "HTTP 1.1" . From v7.62.0 onwards, the default API has been updated to "HTTP 2/TLS". As such, cURL requests to remote websites that have not upgraded their web server infrastructure to the new "HTTP 2/TLS" standard, will fail as a result.

To work around this issue, insert into your PHP code the following : 

<?php
$data = http_build_query(array('param1' => 'test','status' => 'Success'));
$curl = curl_init(); 
curl_setopt($curl, CURLOPT_URL, "server api link");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

// Insert This Line Into Your Code 
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
//

$server_output = curl_exec($curl);
if (curl_error($curl)) {
    $error_msg = curl_error($curl);
}
print_r($error_msg);
curl_close($curl);
?>

 

 

About the author

Tom Thorp
Tom Thorp is an IT Consultant living in Miami on Queensland's Gold Coast. With over 30+ years working in the IT industry, Tom's experience is a broad canvas. The IT services Tom provides to his clients, includes :
 
Website development and hosting
Database Administration
Server Administration (Windows, Linux, Apple)
PABX Hosting and Administration
Helpdesk Support (end-user & technical).
  If you like any of my content, consider a donation via Crypto by clicking on one of the payment methods :
 
Categories
PHPcURL