Created
August 30, 2012 18:20
-
-
Save claviska/3536458 to your computer and use it in GitHub Desktop.
Barebones PHP-based REST client
This file contains hidden or 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 | |
/* | |
Usage: | |
$rest = new REST($config); | |
$result = $rest->get('http://example.com/method, array( | |
'name' => 'value', | |
... | |
)); | |
echo $result->response; // raw response data | |
echo $result->http_code; // resulting HTTP code | |
Config options: | |
$config = array( | |
// Basic auth: | |
'auth_user' => 'username', | |
'auth_pass' => 'password', | |
// Set headers: | |
'headers' => array('Name: value', ...) | |
); | |
Or later on: | |
$rest->set_headers('Name: value'); | |
$rest->set_auth('user', 'pass'); | |
*/ | |
class REST { | |
protected $ch, $headers, $user, $pass; | |
public $response, $http_code; | |
// Setup the object | |
public function __construct($config = null) { | |
$this->set_headers($config['headers']); | |
$this->set_auth($config['auth_user'], $config['auth_pass']); | |
} | |
// Send a GET request | |
public function get($url, $data = null) { | |
// Build and set the request URL | |
$url = (empty($data)) ? $url : $url .'?'. http_build_query((array) $data); | |
return $this->run(array( | |
CURLOPT_URL => $url, | |
CURLOPT_HTTPGET => true | |
)); | |
} | |
// Send a PUT request | |
public function put($url, $data = null) { | |
return $this->run(array( | |
CURLOPT_URL => $url, | |
CURLOPT_CUSTOMREQUEST => 'PUT', | |
CURLOPT_POSTFIELDS => http_build_query((array) $data) | |
)); | |
} | |
// Send a POST request | |
public function post($url, $data = null) { | |
return $this->run(array( | |
CURLOPT_URL => $url, | |
CURLOPT_POST => true, | |
CURLOPT_POSTFIELDS => $data | |
)); | |
} | |
// Send a delete request | |
public function delete($url) { | |
return $this->run(array( | |
CURLOPT_URL => $url, | |
CURLOPT_CUSTOMREQUEST => 'DELETE' | |
)); | |
} | |
// Set basic auth | |
public function set_auth($user, $pass) { | |
$this->user = $user; | |
$this->pass = $pass; | |
return $this; | |
} | |
// Set headers | |
public function set_headers($headers) { | |
$this->headers = $headers; | |
return $this; | |
} | |
// Run the request and send response | |
private function run($curl_opts) { | |
// Init cURL | |
$this->ch = curl_init(); | |
// Set default options | |
curl_setopt($this->ch, CURLOPT_TIMEOUT, 10); | |
curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, false); | |
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($this->ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); | |
// Set headers | |
if( isset($this->headers) ) curl_setopt($this->ch, CURLOPT_HTTPHEADER, $this->headers); | |
// Set basic auth | |
if( isset($this->user) && isset($this->pass) ) curl_setopt($this->ch, CURLOPT_USERPWD, $this->user . ':' . $this->pass); | |
// Set custom options | |
foreach( $curl_opts as $option => $value ) curl_setopt($this->ch, $option, $value); | |
// Send the request | |
$this->response = (string) curl_exec($this->ch); | |
$this->http_code = (int) curl_getinfo($this->ch, CURLINFO_HTTP_CODE); | |
curl_close($this->ch); | |
return $this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment