To perform a cURL DELETE request in PHP, you can use the following code:
// URL of the endpoint you want to send the DELETE request to
$url = "https://api.example.com/endpoint";
// Initialize cURL session
$curl = curl_init();
// Set the cURL options
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// Execute cURL request
$response = curl_exec($curl);
// Get the HTTP response code
$responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
// Close cURL session
curl_close($curl);
// Handle the response
if ($responseCode == 200) {
// Request was successful
echo "DELETE request successful!";
} else {
// Request failed
echo "DELETE request failed with response code: " . $responseCode;
}
In the above code, you set the URL of the endpoint you want to send the DELETE request to using curl_setopt()
function with CURLOPT_URL
. You then set the request type to DELETE using curl_setopt()
function with CURLOPT_CUSTOMREQUEST
. Next, you set CURLOPT_RETURNTRANSFER
to true
to retrieve the response instead of printing it directly.
After executing the cURL request using curl_exec()
, you can extract the HTTP response code using curl_getinfo()
with CURLINFO_HTTP_CODE
.
Finally, you can close the cURL session with curl_close()
and handle the response according to the HTTP response code.