cURL REQUEST DELETE with PhP
Get the solution ↓↓↓I'm consuming an api via cURL in php, but when I need to do a "delete" action I'm having problems
callingcurl_exec ($ ch)
is returning an empty string. I am testing this function via postman and swagger and it is correct. All the other actions I use in curl_api like 'post', 'get', 'patch' are working but 'delete' is not.
curl_api call:$this->curl->curl_api(API_RESTAURANT, 'Menu/' . $this->session->usuario_id, $image, 'delete');
curl_api:
public static function curl_api($api, $url, $json, $action)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_URL, $api . $url);
//curl_setopt($ch, CURLOPT_USERPWD, USER . "/token:" . APIKEY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $action);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 40);
$output = curl_exec($ch);
if(empty($output)){
die(curl_error($ch));
curl_close($ch);
}
else{
$info = curl_getinfo($ch);
curl_close($ch);
}
if (empty($info['http_code'])) {
die();
}
$return = [
'http_code' => $info['http_code'],
'output' => $output
];
return $return;
}
Answer
Solution:
You tried passing the string "DELETE" directly instead as a variable?
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
This user was having the same problem and the main difference on his solution was that.
Hope it helps!
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: uncaught error: call to undefined function mysqli_connect()
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.