Skip to content

Instantly share code, notes, and snippets.

@philsturgeon
Created August 25, 2009 11:19
Show Gist options
  • Save philsturgeon/174644 to your computer and use it in GitHub Desktop.
Save philsturgeon/174644 to your computer and use it in GitHub Desktop.
<?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;
private $responce_string;
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";
if($this->responce_string)
{
echo "<code>".nl2br(htmlentities($this->responce_string))."</code><br/>\n\n";
}
else
{
echo "No responce<br/>\n\n";
}
echo "=============================================<br/>\n";
if($this->CI->curl->error_string)
{
echo "<h3>Errors</h3>";
echo "<strong>Code:</strong> ".$this->CI->curl->error_code."<br/>\n";
echo "<strong>Message:</strong> ".$this->CI->curl->error_string."<br/>\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)
{
return str_getcsv($string);
}
// 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