Search code examples
phppostcurlput

$_POST array is null while using PUT in cURL


I have this code:

$curl = curl_init("localhost/curlexample/index.php/users/$id");
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $temp);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
echo $result;

I'm trying to access the variables in $temp using $_POST super global but it returns null. I want to perform a PUT operation using cURL which I'm trying to achieve using CURLOPT_CUSTOMREQUEST. Now, whenever I remove CURLOPT_CUSTOMREQUEST from the code, it works fine. Otherwise, $_POST returns null.

What am I doing wrong? Or is there an alternate way to perform PUT?

I used this for reference: https://developer.sugarcrm.com/2013/08/30/doing-put-and-delete-with-curl-in-php/


Solution

  • Look at this post. You need to access the data using php://input, like this:

    parse_str(file_get_contents("php://input"), $post_vars); // $post_vars will get populated with the PUT variables.