Created
June 4, 2009 15:30
-
-
Save philsturgeon/123668 to your computer and use it in GitHub Desktop.
This file contains 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 if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
/** | |
* @author Philip Sturgeon | |
* @created 04/06/2009 | |
*/ | |
class REST { | |
private $CI; // CodeIgniter instance | |
private $rest_server; | |
private $supported_formats = array( | |
'xml' => 'application/xml', | |
'json' => 'application/json', | |
'serialize' => 'text/plain', | |
'php' => 'text/plain', | |
); | |
private $auto_detect_formats = array( | |
'application/xml' => 'xml', | |
'text/xml' => 'xml', | |
'application/json' => 'json', | |
'text/json' => 'json', | |
'text/csv' => 'csv', | |
'application/csv' => 'csv' | |
); | |
private $format; | |
private $mime_type; | |
function __construct($config = NULL) | |
{ | |
$this->CI =& get_instance(); | |
log_message('debug', 'REST Class Initialized'); | |
$this->CI->load->library('curl'); | |
// If a URL was passed to the library | |
if($config !== NULL) | |
{ | |
$this->initialize($config); | |
} | |
} | |
public function initialize($config) | |
{ | |
$this->rest_server = @$config['server']; | |
} | |
public function get($uri, $format = NULL) | |
{ | |
if($format !== NULL) | |
{ | |
$this->format($format); | |
} | |
$this->_set_headers(); | |
// Make a basic get call to recieve formatted data | |
$responce = $this->CI->curl->simple_get($this->rest_server.$uri); | |
// Format and return | |
return $this->_format_responce($responce); | |
} | |
public function post($uri, $params = array(), $format = NULL) | |
{ | |
if($format !== NULL) | |
{ | |
$this->format($format); | |
} | |
$this->_set_headers(); | |
// Make a basic get call to recieve formatted data | |
$responce = $this->CI->curl->simple_post($this->rest_server.$uri, $params); | |
// Format and return | |
return $this->_format_responce($responce); | |
} | |
public function delete($uri, $format = NULL) | |
{ | |
if($format !== NULL) | |
{ | |
$this->format($format); | |
} | |
$this->_set_headers(); | |
// Make a basic get call to recieve formatted data | |
$responce = $this->CI->curl->simple_delete($this->rest_server.$uri); | |
// Format and return | |
return $this->_format_responce($responce); | |
} | |
// If a type is passed in that is not supported, use it as a mime type | |
public function format($format) | |
{ | |
if(array_key_exists($format, $this->supported_formats)) | |
{ | |
$this->format = $format; | |
$this->mime_type = $this->supported_formats[$format]; | |
} | |
else | |
{ | |
$this->mime_type = $format; | |
} | |
return $this; | |
} | |
public function debug() | |
{ | |
echo "=============================================<br/>\n"; | |
echo "<h2>REST Test</h2>\n"; | |
echo "=============================================<br/>\n"; | |
echo "<h3>Responce</h3>\n"; | |
echo "<code>".nl2br(htmlentities($this->responce_string))."</code><br/><br/>\n\n"; | |
echo "=============================================<br/>\n"; | |
echo "<h3>Call details</h3>"; | |
echo "<pre>"; | |
print_r($this->CI->curl->info); | |
echo "</pre>"; | |
} | |
private function _set_headers() | |
{ | |
$this->CI->curl->http_header('Accept: '.$this->mime_type); | |
} | |
private function _format_responce($responce) | |
{ | |
$this->responce_string =& $responce; | |
// It is a supported format, so just run its formatting method | |
if(array_key_exists($this->format, $this->supported_formats)) | |
{ | |
return $this->{"_".$this->format}($responce); | |
} | |
$returned_mime = $this->CI->curl->info['content_type']; | |
if(array_key_exists($returned_mime, $this->auto_detect_formats)) | |
{ | |
return $this->{"_".$this->auto_detect_formats[$returned_mime]}($responce); | |
} | |
return $responce; | |
} | |
// Format XML for output | |
private function _xml($string) | |
{ | |
return (array) simplexml_load_string($string); | |
} | |
// Format HTML for output | |
// This function is DODGY! Not perfect CSV support but works with my REST_Controller | |
private function _csv($string) | |
{ | |
$data = array(); | |
// Splits | |
$rows = explode("\n", trim($string)); | |
$headings = explode(',', array_shift($rows)); | |
foreach( $rows as $row ) | |
{ | |
// The substr removes " from start and end | |
$data_fields = explode('","', trim(substr($row, 1, -1))); | |
if(count($data_fields) == count($headings)) | |
{ | |
$data[] = array_combine($headings, $data_fields); | |
} | |
} | |
return $data; | |
} | |
// Encode as JSON | |
private function _json($string) | |
{ | |
return json_decode(trim($string)); | |
} | |
// Encode as Serialized array | |
private function _serialize($string) | |
{ | |
return unserialize(trim($string)); | |
} | |
// Encode raw PHP | |
private function _php($string) | |
{ | |
$string = trim($string); | |
$populated = array(); | |
eval("\$populated = \"$string\";"); | |
return $populated; | |
} | |
} | |
// END REST Class | |
/* End of file REST.php */ | |
/* Location: ./application/libraries/REST.php */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment