0

In my case the user is logged in and would like to delete his account. Therfore he triggers a POST form, revalidating the process with a confirmation of his password.

Now I would like to send a cURL DELETE to the api/server and at the same time deliver the confirmed password as well.

My approach is:

$ch = curl_init();
curl_setopt_array($ch, array(
    CURLOPT_URL => "https://www.myurl.com/v1/users",
    CURLOPT_USERAGENT => $_SERVER['HTTP_USER_AGENT],
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POSTFIELDS => json_encode(['cnfpwd' => $_POST['cnfpwd']]), //confirmed password
    CURLOPT_HTTPHEADER => array('X-API-KEY: '$apiKey),
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "DELETE",
));

$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, true);

//For testing purposes only
print_r($response);
print_r($data);

If this approach is correct, how would I get the POSTFIELDS on the API/server side? As this is a DELETE request, $_GET, $_POST, $_REQUEST are empty.

EDIT: The api code for testing purposes:

<?php
if($_SERVER['REQUEST_METHOD'] == "DELETE"){
   echo"POST: ";print_r($_POST);
}
?>

The result is:

POST: Array ( )

1
  • If you do an DELETE request, you will find your data in $_POST Commented Jul 21, 2020 at 13:57

2 Answers 2

1

You should be able to get the body of the request with

$data = file_get_contents('php://input');
Sign up to request clarification or add additional context in comments.

2 Comments

I am deleting the account, it is just that I would like to pass the confirmed password, like: "Do you really want to delete your account, please enter your password again", so DELETE is correct in this case.
@daniel in this case, yes, DELETE is the proper verb
0

My final solution is:

$parameters = file_get_contents("php://input");
$parameters = json_decode($parameters, true);

@Paolos Solution is correct, I just needed to add the second line in order to access the parameters as an Array.

Comments

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.