Skip to content

Instantly share code, notes, and snippets.

@hubgit
Created June 19, 2012 09:18
Show Gist options
  • Save hubgit/2953176 to your computer and use it in GitHub Desktop.
Save hubgit/2953176 to your computer and use it in GitHub Desktop.
PHP HTTP Client
<?php
class HTTPClient {
public $base;
public $curl;
public $format;
function __construct($base = null) {
$this->base = $base;
$this->curl_init();
}
function absolute($url) {
return preg_match('/^https?:/', $url) ? $url : $this->base . $url;
}
/**
* HTTP methods
*/
function get($url, $params = array(), $format = null) {
$this->format = $format;
if($params) $url .= '?' . http_build_query($params);
$this->curl_setopt_array(array(
CURLOPT_URL => $this->absolute($url),
CURLOPT_HTTPGET => true,
));
return $this->curl_exec();
}
function post($url, $params = array(), $format = null) {
$this->format = $format;
$this->curl_setopt_array(array(
CURLOPT_URL => $this->absolute($url),
CURLOPT_POSTFIELDS => http_build_query($params),
));
return $this->curl_exec();
}
/**
* cURL methods
*/
function set_cookie_file($file = null) {
if(!$file) $file = tempnam(sys_get_temp_dir(), 'cookies-');
$this->curl_setopt_array(array(
CURLOPT_COOKIEJAR => $file,
CURLOPT_COOKIEFILE => $file,
CURLOPT_COOKIESESSION => true,
));
}
function curl_setopt_array($options = array()) {
curl_setopt_array($this->curl, $options);
}
function curl_getinfo($field) {
return curl_getinfo($this->curl, $field);
}
private function curl_init() {
$this->curl = curl_init();
$this->curl_setopt_array(array(
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
//CURLOPT_VERBOSE => true,
));
}
function curl_exec() {
return new HTTPResponse($this, curl_exec($this->curl));
}
}
class HTTPResponse {
public $request;
public $code;
public $type;
public $charset;
public $headers;
public $body;
public $data;
public $dom;
public $xpath;
public $format;
private $formats = array(
'application/xml' => 'xml',
'text/xml' => 'xml',
'application/json' => 'json',
'text/json' => 'json',
'text/plain' => 'text',
'text/html' => 'html',
);
function __construct($request, $response) {
$this->request = $request;
$this->code = $request->curl_getinfo(CURLINFO_HTTP_CODE);
list($this->type, $this->charset) = array_map('trim', explode(';', $request->curl_getinfo(CURLINFO_CONTENT_TYPE), 2));
$this->format = $request->format;
if (!$this->format && isset($this->formats[$this->type])) {
$this->format = $this->formats[$this->type];
}
$header_size = $request->curl_getinfo(CURLINFO_HEADER_SIZE);
$this->headers = $this->parse_headers(mb_substr($response, 0, $header_size));
$this->body = mb_substr($response, $header_size);
//if (substr($this->code, 0, 1) == 2)
$this->parse($this->body, $this->format);
}
function parse_headers($input) {
$items = array();
foreach(explode("\n", $input) as $row) {
list($name, $value) = array_map('trim', explode(':', $row, 2));
if($value) $items[strtolower($name)][] = $value;
}
return $items;
}
function parse($content, $format) {
switch ($format) {
case 'html':
$this->dom = new DOMDocument;
$this->dom->preserveWhiteSpace = false;
$this->dom->documentURI = $this->request->curl_getinfo(CURLINFO_EFFECTIVE_URL);
$useErrors = libxml_use_internal_errors(true); // silence errors loading HTML
$this->dom->loadHTML($content);
libxml_use_internal_errors($useErrors); // restore
$this->xpath = new DOMXPath($this->dom);
break;
case 'xml':
$this->dom = new DOMDocument;
$this->dom->preserveWhiteSpace = false;
$this->dom->documentURI = $this->request->curl_getinfo(CURLINFO_EFFECTIVE_URL);
$this->dom->loadXML($content, LIBXML_DTDLOAD | LIBXML_NOENT | LIBXML_NOCDATA);
$this->xpath = new DOMXPath($this->dom);
break;
case 'json':
$this->data = json_decode($content, true);
break;
case 'text':
default:
$this->data = $content;
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment