PHP CURL DELETE request
To send a DELETE request with PHP using cURL, you can use the following code:
To send a DELETE request with PHP using cURL, you can use the following code:
Example of PHP cURL DELETE request
<?php
// Prepare data as query parameters (standard REST practice for DELETE)
$data = ['key1' => 'value1', 'key2' => 'value2'];
$url = 'http://example.com/api/resource?' . http_build_query($data);
// Set up cURL
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
// Set up DELETE request
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
// Send the request
$response = curl_exec($ch);
// Check for errors
if ($response === false) {
die(curl_error($ch));
}
// Decode the response
$responseData = json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE) {
die('JSON decode error: ' . json_last_error_msg());
}
// Print the response data
print_r($responseData);
// Free resources
curl_close($ch);
<div class="alert alert-info flex not-prose">![]()
<span class="hidden md:block">Watch a video course</span>Learn object oriented PHP</div>
This sends a DELETE request to the specified URL, passing data as query parameters to follow standard REST practices. The response from the server will be saved in the `$response` variable, decoded, and printed as shown in the example.
Note that the server you are sending the request to must be set up to handle DELETE requests and process the data correctly.