CURL with API CRUD BY CI
User.php Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User extends CI_Controller {
function __construct()
{
parent::__construct();
}
public function createUser()
{
/* API URL */
$url = base_url('Api/usercreate');
// Create a new cURL resource
$ch = curl_init($url);
// Setup request to send json via POST
$data = array(
'username' => 'vishnu',
'surname' => 'prajapati',
'address' => 'ahmedabad',
);
$payload = json_encode($data);
// Attach encoded JSON string to the POST fields
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
// Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
// Return response instead of outputting
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute the POST request
$result = curl_exec($ch);
$resultdata = json_decode($result,true);
// Close cURL resource
curl_close($ch);
if($resultdata['Common']['Status'] === 'Success'){
echo "submited";
echo "<pre>";
print_r($resultdata['Response']['UserData']);
exit;
};
}
public function getUser()
{
$url = base_url('Api/get_user');
/* Init cURL resource */
$curl = curl_init($url);
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_TIMEOUT => 30000,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
// Set Here Your Requesred Headers
'Content-Type: application/json',
),
));
/* execute request */
$result = curl_exec($curl);
$resultdata = json_decode($result,true);
/* close cURL resource */
curl_close($curl);
if($resultdata['Common']['Status'] === 'Success'){
echo "Get data";
echo "<pre>";
print_r($resultdata['Response']['UserData']);
exit;
};
}
public function editUser()
{
/* API URL */
$url = base_url('Api/edit_user');
// Create a new cURL resource
$ch = curl_init($url);
// Setup request to send json via POST
$data = array(
'id' => 1,
'username' => 'edited username',
'surname' => 'edited surname',
'address' => 'edited address',
);
$payload = json_encode($data);
// Attach encoded JSON string to the POST fields
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
// Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
// Return response instead of outputting
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute the POST request
$result = curl_exec($ch);
$resultdata = json_decode($result,true);
// Close cURL resource
curl_close($ch);
if($resultdata['Common']['Status'] === 'Success'){
echo "Edit data";
echo "<pre>";
print_r($resultdata['Response']);
exit;
};
}
public function deleteUser()
{
/* API URL */
$url = base_url('Api/deleteuser');
// Create a new cURL resource
$ch = curl_init($url);
// Setup request to send json via POST
$data = array(
'id' => 1,
);
$payload = json_encode($data);
// Attach encoded JSON string to the POST fields
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
// Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
// Return response instead of outputting
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute the POST request
$result = curl_exec($ch);
$resultdata = json_decode($result,true);
/* close cURL resource */
curl_close($ch);
if($resultdata['Common']['Status'] === 'Success'){
echo "Get data";
echo "<pre>";
print_r($resultdata['Response']);
exit;
};
}
}
Api.php controller
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Api extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('Api_model');
}
public function usercreate()
{
if ($_SERVER['CONTENT_TYPE'] == 'application/json') {
$inputJSON = file_get_contents("php://input");
$input = json_decode($inputJSON, TRUE);
$inputData = array(
'username' => $input['username'],
'surname' => $input['surname'],
'address' => $input['address'],
);
//Insert data into database
$insertNewUserData = $this->Api_model->insertuser($inputData);
if ($insertNewUserData) {
$status = "Success";
$message = "User Insert Successfully.";
$data = array("Common" => array("Title" => "New User insert API", 'version' => '1.0', 'Description' => 'New User insert API', 'Method' => 'POST', 'Status' => $status, 'Message' => $message), "Response" => array("UserData" => $input));
print(json_encode($data, JSON_UNESCAPED_UNICODE));
} else {
$status = "Fail";
$message = "User Insert Failed.";
$data = array("Common" => array("Title" => "New User insert API", 'version' => '1.0', 'Description' => 'New User insert API', 'Method' => 'POST', 'Status' => $status, 'Message' => $message), "Response" => array("Value" => 'RECORD_NOT_INSERTED'));
print(json_encode($data, JSON_UNESCAPED_UNICODE));
}
} else {
$status = "Fail";
$message = "Invalid Request.";
$data = array("Common" => array("Title" => "New User insert API", 'version' => '1.0', 'Description' => 'New User insert API', 'Method' => 'POST', 'Status' => $status, 'Message' => $message), "Response" => "INVALID_REQUEST");
print(json_encode($data, JSON_UNESCAPED_UNICODE));
}
}
public function get_user()
{
if ($_SERVER['CONTENT_TYPE'] == 'application/json') {
$inputJSON = file_get_contents("php://input");
$input = json_decode($inputJSON, TRUE);
$inputData = $this->Api_model->getuser();
if ($inputData == TRUE) {
$status = "Success";
$message = "Get User.";
$data = array("Common" => array("Title" => "User List Get API", 'version' => '1.0', 'Description' => 'User List Get API', 'Method' => 'POST', 'Status' => $status, 'Message' => $message), "Response" => array('UserData' => $inputData));
print(json_encode($data, JSON_UNESCAPED_UNICODE));
} else {
$status = "Fail";
$message = "User not found.";
$data = array("Common" => array("Title" => "User List Get API", 'version' => '1.0', 'Description' => 'User List Get API', 'Method' => 'POST', 'Status' => $status, 'Message' => $message), "Response" => array('Value' => 'user not found'));
print(json_encode($data, JSON_UNESCAPED_UNICODE));
}
} else {
$status = "Fail";
$message = "Invalid request.";
$data = array("Common" => array("Title" => "User List Get API", 'version' => '1.0', 'Description' => 'User List Get API', 'Method' => 'POST', 'Status' => $status, 'Message' => $message), "Response" => array("Value" => "INVALID_REQUEST"));
print(json_encode($data, JSON_UNESCAPED_UNICODE));
}
}
public function edit_user()
{
if ($_SERVER['CONTENT_TYPE'] == 'application/json' && $_SERVER['REQUEST_METHOD'] == 'POST') {
$inputJSON = file_get_contents("php://input");
$input = json_decode($inputJSON, TRUE);
if (isset($input['id'])) {
$userdata = array(
'username' => $input['username'],
'surname' => $input['surname'],
'address' => $input['address'],
);
$inputData = $this->Api_model->update_user_data($input['id'],$userdata);
if ($inputData == TRUE) {
$status = "Success";
$message = "User edit.";
$data = array("Common" => array("Title" => "User Edit API", 'version' => '1.0', 'Description' => 'User Edit API', 'Method' => 'POST', 'Status' => $status, 'Message' => $message), "Response" => array('Value' =>'User edit Successfully'));
print(json_encode($data, JSON_UNESCAPED_UNICODE));
} else {
$status = "Fail";
$message = "User not edit.";
$data = array("Common" => array("Title" => "User Edit API", 'version' => '1.0', 'Description' => 'User Edit API', 'Method' => 'POST', 'Status' => $status, 'Message' => $message), "Response" => array('Value' => 'User not found'));
print(json_encode($data, JSON_UNESCAPED_UNICODE));
}
} else {
$status = "Fail";
$message = "Parameters should not be empty.";
$data = array("Common" => array("Title" => "User Edit API", 'version' => '1.0', 'Description' => 'User Edit API', 'Method' => 'POST', 'Status' => $status, 'Message' => $message), "Response" => array("Value" => "EMPTY_PARAMETERS"));
print(json_encode($data, JSON_UNESCAPED_UNICODE));
}
} else {
$status = "Fail";
$message = "Invalid request.";
$data = array("Common" => array("Title" => "User Edit API", 'version' => '1.0', 'Description' => 'User Edit API', 'Method' => 'POST', 'Status' => $status, 'Message' => $message), "Response" => array("Value" => "INVALID_REQUEST"));
print(json_encode($data, JSON_UNESCAPED_UNICODE));
}
}
public function deleteuser()
{
if ($_SERVER['CONTENT_TYPE'] == 'application/json' && $_SERVER['REQUEST_METHOD'] == 'POST') {
$inputJSON = file_get_contents("php://input");
$input = json_decode($inputJSON, TRUE);
$userId = $input['id'];
$deleteuser = $this->Api_model->delete_user($userId);
if(!empty($deleteuser)){
$status = "Success";
$message = "Deleted Successfully.";
$data = array("Common" => array("Title" => "Delete User API", 'version' => '1.0', 'Description' => 'Delete User API', 'Method' => 'POST', 'Status' => $status, 'Message' => $message), "Response" => array("Value" => "DELETED"));
print(json_encode($data, JSON_UNESCAPED_UNICODE));
}else{
$status = "Fail";
$message = "Not Deleted.";
$data = array("Common" => array("Title" => "Delete User API", 'version' => '1.0', 'Description' => 'Delete User API', 'Method' => 'POST', 'Status' => $status, 'Message' => $message), "Response" => array("Value" => "NOT_DELETED"));
print(json_encode($data, JSON_UNESCAPED_UNICODE));
}
} else {
$status = "Fail";
$message = "Invalid request.";
$data = array("Common" => array("Title" => "Delete User API", 'version' => '1.0', 'Description' => 'Delete User API', 'Method' => 'POST', 'Status' => $status, 'Message' => $message), "Response" => array("Value" => "INVALID_REQUEST"));
print(json_encode($data, JSON_UNESCAPED_UNICODE));
}
}
}
?>
Api_model.php Model
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Api_model extends CI_Model {
public function insertuser($userdata)
{
return $this->db->insert('tbl_user',$userdata);
}
public function getuser()
{
$this->db->select('*');
$this->db->from('tbl_user');
return $query = $this->db->get()->result_array();
}
public function update_user_data($id,$userdata)
{
$this->db->where('id', $id);
return $this->db->update('tbl_user',$userdata);
}
public function delete_user($id)
{
$this->db->where('id',$id);
return $this->db->delete('tbl_user');
}
}
SQL Script
CREATE TABLE IF NOT EXISTS `tbl_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(100) NOT NULL,
`surname` varchar(100) NOT NULL,
`address` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
)
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User extends CI_Controller {
function __construct()
{
parent::__construct();
}
public function createUser()
{
/* API URL */
$url = base_url('Api/usercreate');
// Create a new cURL resource
$ch = curl_init($url);
// Setup request to send json via POST
$data = array(
'username' => 'vishnu',
'surname' => 'prajapati',
'address' => 'ahmedabad',
);
$payload = json_encode($data);
// Attach encoded JSON string to the POST fields
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
// Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
// Return response instead of outputting
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute the POST request
$result = curl_exec($ch);
$resultdata = json_decode($result,true);
// Close cURL resource
curl_close($ch);
if($resultdata['Common']['Status'] === 'Success'){
echo "submited";
echo "<pre>";
print_r($resultdata['Response']['UserData']);
exit;
};
}
public function getUser()
{
$url = base_url('Api/get_user');
/* Init cURL resource */
$curl = curl_init($url);
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_TIMEOUT => 30000,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
// Set Here Your Requesred Headers
'Content-Type: application/json',
),
));
/* execute request */
$result = curl_exec($curl);
$resultdata = json_decode($result,true);
/* close cURL resource */
curl_close($curl);
if($resultdata['Common']['Status'] === 'Success'){
echo "Get data";
echo "<pre>";
print_r($resultdata['Response']['UserData']);
exit;
};
}
public function editUser()
{
/* API URL */
$url = base_url('Api/edit_user');
// Create a new cURL resource
$ch = curl_init($url);
// Setup request to send json via POST
$data = array(
'id' => 1,
'username' => 'edited username',
'surname' => 'edited surname',
'address' => 'edited address',
);
$payload = json_encode($data);
// Attach encoded JSON string to the POST fields
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
// Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
// Return response instead of outputting
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute the POST request
$result = curl_exec($ch);
$resultdata = json_decode($result,true);
// Close cURL resource
curl_close($ch);
if($resultdata['Common']['Status'] === 'Success'){
echo "Edit data";
echo "<pre>";
print_r($resultdata['Response']);
exit;
};
}
public function deleteUser()
{
/* API URL */
$url = base_url('Api/deleteuser');
// Create a new cURL resource
$ch = curl_init($url);
// Setup request to send json via POST
$data = array(
'id' => 1,
);
$payload = json_encode($data);
// Attach encoded JSON string to the POST fields
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
// Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
// Return response instead of outputting
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute the POST request
$result = curl_exec($ch);
$resultdata = json_decode($result,true);
/* close cURL resource */
curl_close($ch);
if($resultdata['Common']['Status'] === 'Success'){
echo "Get data";
echo "<pre>";
print_r($resultdata['Response']);
exit;
};
}
}
Api.php controller
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Api extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('Api_model');
}
public function usercreate()
{
if ($_SERVER['CONTENT_TYPE'] == 'application/json') {
$inputJSON = file_get_contents("php://input");
$input = json_decode($inputJSON, TRUE);
$inputData = array(
'username' => $input['username'],
'surname' => $input['surname'],
'address' => $input['address'],
);
//Insert data into database
$insertNewUserData = $this->Api_model->insertuser($inputData);
if ($insertNewUserData) {
$status = "Success";
$message = "User Insert Successfully.";
$data = array("Common" => array("Title" => "New User insert API", 'version' => '1.0', 'Description' => 'New User insert API', 'Method' => 'POST', 'Status' => $status, 'Message' => $message), "Response" => array("UserData" => $input));
print(json_encode($data, JSON_UNESCAPED_UNICODE));
} else {
$status = "Fail";
$message = "User Insert Failed.";
$data = array("Common" => array("Title" => "New User insert API", 'version' => '1.0', 'Description' => 'New User insert API', 'Method' => 'POST', 'Status' => $status, 'Message' => $message), "Response" => array("Value" => 'RECORD_NOT_INSERTED'));
print(json_encode($data, JSON_UNESCAPED_UNICODE));
}
} else {
$status = "Fail";
$message = "Invalid Request.";
$data = array("Common" => array("Title" => "New User insert API", 'version' => '1.0', 'Description' => 'New User insert API', 'Method' => 'POST', 'Status' => $status, 'Message' => $message), "Response" => "INVALID_REQUEST");
print(json_encode($data, JSON_UNESCAPED_UNICODE));
}
}
public function get_user()
{
if ($_SERVER['CONTENT_TYPE'] == 'application/json') {
$inputJSON = file_get_contents("php://input");
$input = json_decode($inputJSON, TRUE);
$inputData = $this->Api_model->getuser();
if ($inputData == TRUE) {
$status = "Success";
$message = "Get User.";
$data = array("Common" => array("Title" => "User List Get API", 'version' => '1.0', 'Description' => 'User List Get API', 'Method' => 'POST', 'Status' => $status, 'Message' => $message), "Response" => array('UserData' => $inputData));
print(json_encode($data, JSON_UNESCAPED_UNICODE));
} else {
$status = "Fail";
$message = "User not found.";
$data = array("Common" => array("Title" => "User List Get API", 'version' => '1.0', 'Description' => 'User List Get API', 'Method' => 'POST', 'Status' => $status, 'Message' => $message), "Response" => array('Value' => 'user not found'));
print(json_encode($data, JSON_UNESCAPED_UNICODE));
}
} else {
$status = "Fail";
$message = "Invalid request.";
$data = array("Common" => array("Title" => "User List Get API", 'version' => '1.0', 'Description' => 'User List Get API', 'Method' => 'POST', 'Status' => $status, 'Message' => $message), "Response" => array("Value" => "INVALID_REQUEST"));
print(json_encode($data, JSON_UNESCAPED_UNICODE));
}
}
public function edit_user()
{
if ($_SERVER['CONTENT_TYPE'] == 'application/json' && $_SERVER['REQUEST_METHOD'] == 'POST') {
$inputJSON = file_get_contents("php://input");
$input = json_decode($inputJSON, TRUE);
if (isset($input['id'])) {
$userdata = array(
'username' => $input['username'],
'surname' => $input['surname'],
'address' => $input['address'],
);
$inputData = $this->Api_model->update_user_data($input['id'],$userdata);
if ($inputData == TRUE) {
$status = "Success";
$message = "User edit.";
$data = array("Common" => array("Title" => "User Edit API", 'version' => '1.0', 'Description' => 'User Edit API', 'Method' => 'POST', 'Status' => $status, 'Message' => $message), "Response" => array('Value' =>'User edit Successfully'));
print(json_encode($data, JSON_UNESCAPED_UNICODE));
} else {
$status = "Fail";
$message = "User not edit.";
$data = array("Common" => array("Title" => "User Edit API", 'version' => '1.0', 'Description' => 'User Edit API', 'Method' => 'POST', 'Status' => $status, 'Message' => $message), "Response" => array('Value' => 'User not found'));
print(json_encode($data, JSON_UNESCAPED_UNICODE));
}
} else {
$status = "Fail";
$message = "Parameters should not be empty.";
$data = array("Common" => array("Title" => "User Edit API", 'version' => '1.0', 'Description' => 'User Edit API', 'Method' => 'POST', 'Status' => $status, 'Message' => $message), "Response" => array("Value" => "EMPTY_PARAMETERS"));
print(json_encode($data, JSON_UNESCAPED_UNICODE));
}
} else {
$status = "Fail";
$message = "Invalid request.";
$data = array("Common" => array("Title" => "User Edit API", 'version' => '1.0', 'Description' => 'User Edit API', 'Method' => 'POST', 'Status' => $status, 'Message' => $message), "Response" => array("Value" => "INVALID_REQUEST"));
print(json_encode($data, JSON_UNESCAPED_UNICODE));
}
}
public function deleteuser()
{
if ($_SERVER['CONTENT_TYPE'] == 'application/json' && $_SERVER['REQUEST_METHOD'] == 'POST') {
$inputJSON = file_get_contents("php://input");
$input = json_decode($inputJSON, TRUE);
$userId = $input['id'];
$deleteuser = $this->Api_model->delete_user($userId);
if(!empty($deleteuser)){
$status = "Success";
$message = "Deleted Successfully.";
$data = array("Common" => array("Title" => "Delete User API", 'version' => '1.0', 'Description' => 'Delete User API', 'Method' => 'POST', 'Status' => $status, 'Message' => $message), "Response" => array("Value" => "DELETED"));
print(json_encode($data, JSON_UNESCAPED_UNICODE));
}else{
$status = "Fail";
$message = "Not Deleted.";
$data = array("Common" => array("Title" => "Delete User API", 'version' => '1.0', 'Description' => 'Delete User API', 'Method' => 'POST', 'Status' => $status, 'Message' => $message), "Response" => array("Value" => "NOT_DELETED"));
print(json_encode($data, JSON_UNESCAPED_UNICODE));
}
} else {
$status = "Fail";
$message = "Invalid request.";
$data = array("Common" => array("Title" => "Delete User API", 'version' => '1.0', 'Description' => 'Delete User API', 'Method' => 'POST', 'Status' => $status, 'Message' => $message), "Response" => array("Value" => "INVALID_REQUEST"));
print(json_encode($data, JSON_UNESCAPED_UNICODE));
}
}
}
?>
Api_model.php Model
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Api_model extends CI_Model {
public function insertuser($userdata)
{
return $this->db->insert('tbl_user',$userdata);
}
public function getuser()
{
$this->db->select('*');
$this->db->from('tbl_user');
return $query = $this->db->get()->result_array();
}
public function update_user_data($id,$userdata)
{
$this->db->where('id', $id);
return $this->db->update('tbl_user',$userdata);
}
public function delete_user($id)
{
$this->db->where('id',$id);
return $this->db->delete('tbl_user');
}
}
SQL Script
CREATE TABLE IF NOT EXISTS `tbl_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(100) NOT NULL,
`surname` varchar(100) NOT NULL,
`address` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
)
Comments
Post a Comment