0

I need to send a PUT request with cUrl in PHP. However, I don't get the request body to work.

I need to send the following JSON as PUT request.

{
    "orders": [
        {
            "order_id": "TBRQCH"
        }
    ]
}

Here is what I tried:

$data['orders'][] = array("order_id" => $orderid);
$curl = curl_init();
curl_setopt($curl, CURLOPT_PUT, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "XXXX");
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);

However, it does not work correctly because I get the message from the endpoint that orders is missing. When copy the encoded string to Postman and send the request manually to the API then it works fine.

What am I doing wrong?

Marvin Klein
  • 381
  • 1
  • 13
  • 3
    Try to remove `CURLOPT_PUT` (used _"to HTTP PUT a file"_, and requires CURLOPT_INFILE and CURLOPT_INFILESIZE). Also, `$orderid` contains the right value ? – Syscall Mar 01 '21 at 10:14
  • Does this answer your question? [PHP + curl, HTTP POST sample code?](https://stackoverflow.com/questions/2138527/php-curl-http-post-sample-code) – Giacomo M Mar 01 '21 at 10:23
  • 1
    @Syscall removing CURLOPT_PUT does the trick. Thank you. – Marvin Klein Mar 01 '21 at 10:25

0 Answers0