0

I'm consuming an api via cURL in php, but when I need to do a "delete" action I'm having problems

calling curl_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;
        }


1 Answers1

0

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!

MNN
  • 134
  • 1
  • 6