Using curl for codeigniter REST

http://philsturgeon.co.uk/code/codeigniter-curl

1. copy curl file to library file
2. There would be two controller in ci. One for sending data in JSON format and one for receiving data in JSON format by json_decode.
To insert data into MySQL db from data from android,
In one controller(eg. client_test controller),
function client_register()
{
$username = ‘admin’;
$password = ‘1234’;

$this->load->library(‘curl’);

$this->curl->create(‘http://hsbc.local/api/client/register/format/json’);

// Optional, delete this line if your API is open
$this->curl->http_login($username, $password);

$data = array (
“type_id” => 1,
“agent_id” => 2,
“first_name” => “Joanna”,
“last_name” => “Lee”,
“phone” => “234555”,
“address” => “Mandalay”,
“city” => “Mandalay”,
“mobile” => “095053453”,
“email” => “lee@gmail.com”,
“createddate” => “2013-10-04”,
“modifieddate” => “2013-10-0”
);

$json = json_encode($data);
$this->curl->post(array(
‘postdata’ => $json
));
$result = json_decode($this->curl->execute());
print_r($result);
}
In the other controller(client controller) that might extends REST_Controller(https://github.com/philsturgeon/codeigniter-restserver),
function create_post()
{
try
{
$json = $this->post(‘postdata’);
$data = json_decode($json);
$this->db->insert(‘client’, $data);
$message = json_encode(array(“last_insert_id” => $this->db->insert_id()));
$this->response($message, 200);
}
catch (Exception $e)
{
$this->response(array(‘error’ => $e->getMessage()), 500);
}
}
Then, in the url type client_register, then if it is successfully inserted it will display last insert id.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s