Created
October 15, 2016 14:44
-
-
Save erayakartuna/8cd859ab815ab293b21fa6bdc6d0027a to your computer and use it in GitHub Desktop.
Codeigniter Rest Server Delete Method Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Write for this issue