PHP CURL DELETE request
I finally solved this myself. If anyone else is having this problem, here is my solution:
I created a new method:
public function curl_del($path)
{
$url = $this->__url.$path;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $result;
}
Update 2
Since this seems to help some people, here is my final curl DELETE method, which returns the HTTP response in JSON decoded object:
/**
* @desc Do a DELETE request with cURL
*
* @param string $path path that goes after the URL fx. "/user/login"
* @param array $json If you need to send some json with your request.
* For me delete requests are always blank
* @return Obj $result HTTP response from REST interface in JSON decoded.
*/
public function curl_del($path, $json = '')
{
$url = $this->__url.$path;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$result = json_decode($result);
curl_close($ch);
return $result;
}
How to write PHP curl DELETE request
Try This
Change the CUTLOPT_CUSTOMREQUEST to "DELETE" Find More
$ch = curl_init('https://api.digitalocean.com/v2/droplets/18160706');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer MY_API_TOKEN')
);
$result = curl_exec($ch);
echo $result;
PHP: How to use DELETE request in curl?
You need to add CURLOPT_CUSTOMREQUEST
for DELETE request as:
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
From PHP Manual:
A custom request method to use instead of "GET" or "HEAD" when doing a
HTTP request. This is useful for doing "DELETE" or other, more obscure
HTTP requests. Valid values are things like "GET", "POST", "CONNECT"
and so on; i.e. Do not enter a whole HTTP request line here. For
instance, entering "GET /index.html HTTP/1.0\r\n\r\n" would be
incorrect.
PHP cURL as DELETE using POSTFIELDS
You should be able to get the body of the request with
$data = file_get_contents('php://input');
curl delete in codeigniter
There is nothing wrong with your code.
Your function works well (I've tested it). There are only three possible problems:
- the script on the page you are calling (
$hostURL
) is broken, - your input data (
$data
) has wrong format, - you are not reading the right post data on your $hostURL page.
What does the $data
you are passing to function3
looks like? It should be something like this:
$data = 'param=value&anotherparam=anothervalue';
You can use http_build_query function.
In the file that is handling your DELETE request, you have to use this code to read post data:
$post_data = file_get_contents("php://input");
Related Topics
How to Keep Whitespace Formatting Using PHP/Html
File_Get_Contents Throws 400 Bad Request Error PHP
Which Is Faster? Constants, Variables or Variable Arrays
How to Use an Object Method as a Callback Function
How to Protect My Source Code When Deployed
Parse Error: Syntax Error, Unexpected '' (T_Encapsed_And_Whitespace)
Protocol Https Not Supported or Disabled in Libcurl
How to Use PHP to Connect to SQL Server
Php's Preg_Replace Regex That Matches Multiple Lines
How to Fix 'Creating Default Object from Empty Value' Warning in PHP
How to Bind Decimal/Double/Float Values with Pdo in PHP
How to Increment Count in the Replacement String When Using Preg_Replace
How to Enable Enums in Symfony 2/Doctrine