10 May, 2011

REST client library using CodeIgniter

Using REST in applications makes life easier to some extent. And using REST in codeigniter can be easier. There exist some REST libraries in codeigniter but somehow they seem to have lots of code and features which is not required in my case. I'm not degrading existing codeigniter REST libraries, they are excellent codes
developed by experts, but i wanted a simple REST client using Codeigniter, so thought of coding my own.
Following is the code for REST client library in codeIgniter, that suits simple needs.


class Rest_Lib {

private $useCurl;
private $userAgent = "Rest";

/*
* constructor function
*/
function Rest_Lib() {
//check if curl function exists
if(function_exists('curl_init')) {
$this->useCurl = TRUE;
}
else {
$this->useCurl = FALSE;
}
}

/*
* GET method
* @param string $url : this is the service url
* @param array $arr: this is the key value
* @return string : resulting response of GET method
*/
public function get($url, $arr) {
$str = '?';
if(is_array($arr)) {
foreach($arr as $key => $value) {
$str .= urlencode($key) . '=' . urlencode($value) . '&';
}
}
else {
$str .= $arr;
}
$url .= $str;
if($this->useCurl) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
curl_close($ch);
}
else {
$options = array(
'http' => array(
'method' => 'GET',
'header' => 'User-Agent:' . $this->userAgent . '\r\n'
)
);
//create and return stream context with options
$streamContext = stream_context_create($options);
$fh = file_open($url, 'r', FALSE, $streamContext);
$response = fpassthru($fh);
fclose($fh);
}
return $response;
}

/*
* POST method
* @param string: $url: service url
* @param string $postData: request data
* @return response
*/
public function post($url, $postData) {
if($this->useCurl) {
$curlHeaders = array(
'Content-Type: application/x-www-form-urlencoded'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $curlHeaders);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
$response = curl_exec($ch);
curl_close($ch);
}
else {
$options = array(
'http' => array(
'method' => 'POST',
'header' => 'User-Agent: ' . $this->userAgent . '\r\n',
'Content-Type: application/x-www-form-urlencoded' . '\r\n',
'Content-Length: ' . strlen($postData) . '\r\n',
'Content' => $postData
)
);
$streamContext = stream_context_create($options);
$fh = file_open($url, 'r', FALSE, $streamContext);
$response = fpassthru($fh);
fclose($fh);
}
return $response;
}

/*
* PUT method
*
*/
public function put($url, $putData) {
if($this->useCurl) {
$fh = fopen('php://memory', 'rw');
fwrite($fh, $putData);
rewind($fh);

$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
curl_setopt($ch, CURLOPT_INFILE, $fh);
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($putData));
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_PUT, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
curl_close($ch);
fclose($fh);
}
else {
$options = array(
'http' => array(
'method' => 'PUT',
'header' => 'User-Agent: ' . $this->userAgent . '\r\n',
'Content-Type: application/x-www-form-urlencoded' . '\r\n',
'Content-Length: ' . strlen($putData) . '\r\n',
'Content' => $putData
)
);
$context = stream_context_create();
$fh = file_open($url, 'r', FALSE, $context);
$response = fpassthru($fh);
fclose($fh);
}
return $response;
}

/*
* DELETE method
*/
public function delete($url, $arr) {
$str = '?';
if(is_array($arr)) {
foreach($arr as $key => $value) {
$str .= urlencode($key) . '=' . urlencode($value) . '&';
}
}
else {
$str .= $arr;
}
$url .= $str;
if($this->useCurl) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'delete');
curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
$response = curl_exec($ch);
curl_close($ch);
}
else {
$options = array(
'http' => array(
'method' => 'DELETE',
'header' => 'User-Agent: ' . $this->userAgent . '\r\n'
)
);
$context = stream_context_create($options);
$fh = file_open($url, 'r', FALSE, $context);
$response = fpassthru($fh);
fclose($fh);
}
return TRUE;
}
}
?>

5 comments:

Anonymous said...

Why do you prefer CURL? I see you fall back to file_open only if CURL is not available.

Sudhir Bastakoti said...

Hi,
Thanks for comment. You see cURL comes at the top if you do a performance benchmark. Even though we may have to write more code using cURL, but it takes half the time as it takes for other methods to get the contents.
So obiviously, you always opt for a best performing method. And if a curl method does not exist then opt for file related methods. Do some performance benchmarks and you will get the exact results as why i opted for cURL as my first prioritized method.
Hope you got the answer.
Thanks again

Unknown said...

thanks you it working...

Unknown said...

thank you

Rubens Takiguti Ribeiro said...

Don't you know http_build_query?
It would make your class simpler.