1

EDIT: For some reason, this server is expecting a header 'Accept: application/json' . When I added that, it worked fine.

I have tried several different ways to do a PUT request with a JSON request body. I always get an error saying that they didn't receive the data. I don't know if it's my code or their server. This is what I have now. Also, I have tried every combination of curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT'); and curl_setopt($curl, CURLOPT_POSTFIELDS, $data); and nothing works.

However I get an error sending the exact same data using the following PHP

$curl = curl_init();

$data = json_encode($data_array);

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

$headers[]= 'Authorization: Basic '.AUTHENTICATION_TOKEN;


curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

curl_setopt($curl, CURLOPT_URL, $url);

$result = curl_exec($curl);
dougd_in_nc
  • 371
  • 5
  • 20
  • Are you sure you're meant to have two `Authorization` headers? If so, you should have a space between `Authorization:` and the value; your first one is missing that space – Phil Dec 16 '19 at 05:01
  • @Phil That was a copy/paste error. Should only be the one above. – dougd_in_nc Dec 16 '19 at 17:41
  • Just saw your update. Glad you got it working – Phil Dec 16 '19 at 20:18

1 Answers1

1

Use the curl_errno in order to get the specific error as follow:

$error_msg = NULL;
if (curl_errno($ch)) {
    $error_msg = curl_error($ch);
}
curl_close($ch);
if ($error_msg !== NULL) {
    print_r($error_msg);
}
e-israel
  • 623
  • 10
  • 30