I'm trying to do a DELETE http request using PHP and cURL.
I have read how to do it many places, but nothing seems to work for me.
This is how I do it:
public function curl_req($path,$json,$req) { $ch = curl_init($this->__url.$path); $data = json_encode($json); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $req); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data))); $result = curl_exec($ch); $result = json_decode($result); return $result; }
I then go ahead and use my function:
public function deleteUser($extid) { $path = "/rest/user/".$extid."/;token=".$this->__token; $result = $this->curl_req($path,"","DELETE"); return $result; }
This gives me HTTP internal server ERROR. In my other functions using the same curl_req method with GET and POST, everything goes well.
So what am I doing wrong?
To make a DELETE request using Curl, you need to use the -X DELETE command-line option followed by the target URL. To pass additional headers to the HTTP server, use the -H command-line option. The "Accept: application/json" header tells the server that the client expects JSON data in response.
CURLOPT_POSTFIELDS. The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. The filetype can be explicitly specified by following the filename with the type in the format ';type=mimetype'.
[PHP Code] To make a DELETE request using Curl, you need to use the -X DELETE command-line option followed by the target URL. To pass additional headers to the HTTP server, use the -H command-line option. The "Accept: application/json" header tells the server that the client expects JSON data in response.
The API calls and functions I’m using in this post are all working examples on PHP -v 5.6. cURL stands for ‘Client URL Library’ and it allows you to connect and communicate with different types of servers with many different types of protocols (HTTP, https, FTP, proxy, cookies, …).
Because for posting a cURL request you definitely need an URL where the API is installed, but not to reply to that request because the call can come from various sources or webpages. I have successfully used cURL to request data from different APIs but of course, those APIs were already existing.
cURL (client URL) is a command-line utility for transferring data to and from a server. The tool allows communication with a web or application server and sending method requests directly from the terminal. The HTTP DELETE method request sends a signal to the originating server to delete a resource.
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; }
To call GET,POST,DELETE,PUT All kind of request, i have created one common function
function CallAPI($method, $api, $data) { $url = "http://localhost:82/slimdemo/RESTAPI/" . $api; $curl = curl_init($url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); switch ($method) { case "GET": curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET"); break; case "POST": curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST"); break; case "PUT": curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT"); break; case "DELETE": curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE"); curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); break; } $response = curl_exec($curl); $data = json_decode($response); /* Check for 404 (file not found). */ $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); // Check the HTTP Status code switch ($httpCode) { case 200: $error_status = "200: Success"; return ($data); break; case 404: $error_status = "404: API Not found"; break; case 500: $error_status = "500: servers replied with an error."; break; case 502: $error_status = "502: servers may be down or being upgraded. Hopefully they'll be OK soon!"; break; case 503: $error_status = "503: service unavailable. Hopefully they'll be OK soon!"; break; default: $error_status = "Undocumented error: " . $httpCode . " : " . curl_error($curl); break; } curl_close($curl); echo $error_status; die; }
CALL Delete Method
$data = array('id'=>$_GET['did']); $result = CallAPI('DELETE', "DeleteCategory", $data);
CALL Post Method
$data = array('title'=>$_POST['txtcategory'],'description'=>$_POST['txtdesc']); $result = CallAPI('POST', "InsertCategory", $data);
CALL Get Method
$data = array('id'=>$_GET['eid']); $result = CallAPI('GET', "GetCategoryById", $data);
CALL Put Method
$data = array('id'=>$_REQUEST['eid'],'title'=>$_REQUEST['txtcategory'],'description'=>$_REQUEST['txtdesc']); $result = CallAPI('POST', "UpdateCategory", $data);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With