Reputation: 1219
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 ( )
Upvotes: 0
Views: 376
Reputation: 15847
You should be able to get the body of the request with
$data = file_get_contents('php://input');
Upvotes: 1
Reputation: 1219
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.
Upvotes: 0