1

I tried to write a script to let php do PUT request for me via CURLOPT_CUSTOMREQUEST, 'PUT' for me but I kept receiving 422 error (it means the data is formatted wrong and cannot be processed).

First of, I tried direct sending out the PUT request in the terminal and postman, both works fine. The body of the request is a simple JSON format:

{"status":"online"}

Now, here are the code I tried:

  $ch = curl_init($url);
  curl_setopt_array($ch, array(        
    CURLOPT_HTTPHEADER => array( 'Authorization: "Panda is cute"' . "\r\n" . 'application/json; charset=utf-8' . "\r\n" . 'Content-Length: 19' . "\r\n" ),
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => 'PUT',
    CURLOPT_POST => false,
    CURLOPT_POSTFIELDS => http_build_query( array ("status" => "online") )
  ));
  $out = curl_exec($ch);
  curl_close($ch);
  echo $out;

I have tried a few modification of the code above.

  1. I tried curl_setopt everything instead of putting them all in curl_setopt_array
  2. I tried setting CURLOPT_POST to true.
  3. I tried CURLOPT_POSTFIELDS => json_encode( array ("status" => "online") )
  4. I tried CURLOPT_POSTFIELDS => array ("status" => "online")
  5. I tried CURLOPT_POSTFIELDS => '{"status":"online"}'
  6. I tried CURLOPT_POSTFIELDS => 'http_build_query({"status":"online"})'
  7. I tried CURLOPT_POSTFIELDS => 'http_build_query("status":"online")'
  8. I tried json_encode with the above two.
  9. I also tried all of the above with curl_setopt.
  10. I also tried to use " instead of my favorite ' everywhere I could, like where I wrote 'PUT'

Now, when I use non-custom PUT request and upload a file it does work, but this is posting a challenge because my production environment won't allow me to do fopen().

P.S. Thanks for the answer below, and I know why it didn't work now:

CURLOPT_HTTPHEADER => array( 'Authorization: "Panda is cute"' . "\r\n" . 'application/json; charset=utf-8' . "\r\n" . 'Content-Length: 19' . "\r\n" )

This line will put an additional "\r\n" at the end of the header (but without it php won't accept my original syntax and will post an error message, not rendering the header at all), making the content of my body "\r\n"{"status":"online"} instead.

4
  • Postman has feature to generate code, so for php you can generate code and see what are you doing wrong :) Commented Oct 7, 2016 at 15:12
  • @AlwaysSunny YES please tell me how to generate code with php, I was trying to use packet capture but it didn't work. Commented Oct 7, 2016 at 15:13
  • Want see Generate Code link at right upper corner getpostman.com/docs/code_snippets Commented Oct 7, 2016 at 15:16
  • @AlwaysSunny that's for postman. How do I compare it to the code my php script had generated? Commented Oct 7, 2016 at 15:23

1 Answer 1

2

PUT request generated from POSTMAN for PHP

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "http://www.stackoverflow.com/", //put your url here
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "PUT",
  CURLOPT_POSTFIELDS => "{\"status\":\"online\"}",
  CURLOPT_HTTPHEADER => array(
    "authorization: Panda is cute",
    "cache-control: no-cache",
    "content-type: application/json",
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Yes but make sure you change the url, authorization header & body content as per your data params. :)
It did work. Let me compare this line by line until I figure out why it doesn't work and then I will let everyone know.
just updated my question to reflect the problem. Thanks man. Sometimes you spend six hours for a very tiny mistake.
@AeroWang, thanks for updating question, yeah this also happened to me and other, so your not alone buddy. :) best of luck
Postman is the best, always test with postman when you get the result you can copy the revelant code from postman!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.