Skip to content

Instantly share code, notes, and snippets.

@erayakartuna
Created October 15, 2016 14:44
Show Gist options
  • Save erayakartuna/8cd859ab815ab293b21fa6bdc6d0027a to your computer and use it in GitHub Desktop.
Save erayakartuna/8cd859ab815ab293b21fa6bdc6d0027a to your computer and use it in GitHub Desktop.
Codeigniter Rest Server Delete Method Example
<?php
public function users_delete()
{
$id = (int) $this->get('id');
$group = (int) $this->delete('group');
$group_2 = (int) $this->delete('group_2');
echo "id : ".$id.'<BR/>';
echo "group : ".$group.'<BR/>';
echo "group 2 : ".$group_2.'<BR/>';
exit;
// Validate the id.
if ($id <= 0)
{
// Set the response and exit
$this->response(NULL, REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
}
// $this->some_model->delete_something($id);
$message = [
'id' => $id,
'message' => 'Deleted the resource'
];
$this->set_response($message, REST_Controller::HTTP_NO_CONTENT); // NO_CONTENT (204) being the HTTP response code
}
<?php
public function test()
{
$this->load->helper('url');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,site_url('api/example/users/id/2'));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_POSTFIELDS,
"group=3&group_2=1"); // look at this section its important
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
echo $server_output;
}
@erayakartuna
Copy link
Author

Write for this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment