0

I have a curl request, which I can send from the cli like this:

curl -X 'PUT' \
  'https://XXX/endpiontY' \
  -H 'Authorization: Bearer <TOKEN>' \
  -H 'Content-Type: application/json' \
  -d '{
  "value": "TestValue"
}'

(curl --version returns curl 7.81.0, so default is CURL_HTTP_VERSION_2TLS)

I am trying to send the same information with PHP, but I get

HTTP/2 stream 0 was not closed cleanly: PROTOCOL_ERROR (err 1)

My Setup

$json = array('value' => 'TestValue');
$json_string = json_encode($json);

$headers = array(
    'Content-Type:application/json',
    'Content-Length: ' . strlen($json_string)
)

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_string);
curl_setopt($ch, CURLOPT_PUT, true);

$result = curl_exec($ch);
if ($result === false) {
    $error = curl_error($ch);
}

curl_close($ch);

$result is always false so the error triggers with the above message shown.

I tried

  • I found this https://stackoverflow.com/a/59955444/1092632 curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);

    • causes a timeout of the endpoint
  • Using curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); instead of curl_setopt($ch, CURLOPT_PUT, true);

    • no change
  • Adding $headers[] = 'Connection: close';

    • no change

Where am I wrong sending the PUT request? Any hint highly appreciated!

PrimuS
  • 2,505
  • 6
  • 33
  • 66
  • What version of curl is PHP using (`phpinfo()` or `curl_version()`)? It might be different than the cli binary you have. – Travis Britz Mar 28 '22 at 20:54
  • `cURL Information 7.80.0` from `phpinfo()` – PrimuS Mar 28 '22 at 20:56
  • Have you tried adding a space to `'Content-Type:application/json'` (low probability of cause), or omitting the `Content-Length` header from your request (higher probability)? – Travis Britz Mar 28 '22 at 21:18

0 Answers0