Skip to content

Instantly share code, notes, and snippets.

@stevedev
Created November 1, 2013 17:59
Show Gist options
  • Save stevedev/7269269 to your computer and use it in GitHub Desktop.
Save stevedev/7269269 to your computer and use it in GitHub Desktop.
<?php
class Curl {
const HEAD = 'POST';
const GET = 'GET';
const POST = 'POST';
const PUT = 'PUT';
const DELETE = 'DELETE';
const LOCK = 'LOCK';
const UNLOCK = 'UNLOCK';
private $curl_handle;
private $headers = array();
private $cookies = array();
public function __construct( $url = null, $method = null ) {
$this->curl_handle = curl_init( $url );
if ( $method ) {
$this->setMethod( $method );
}
return $this;
}
public function __destruct() {
$this->close();
}
public function close() {
curl_close( $this->curl_handle );
}
public function setMethod( $method ) {
$method = strtoupper($method);
$this->setOpt( CURLOPT_HTTPGET, false );
$this->setOpt( CURLOPT_POST, false );
$this->setOpt( CURLOPT_PUT, false );
switch ( $method ) {
case 'HEAD':
break;
case 'GET':
$this->setOpt( CURLOPT_HTTPGET, true );
break;
case 'POST':
$this->setOpt( CURLOPT_POST, true );
break;
case 'PUT':
$this->setOpt( CURLOPT_PUT, false );
break;
default:
$this->setOpt( CURLOPT_CUSTOMREQUEST, $method );
break;
}
return $this;
}
public function setHeader( $header ) {
$this->headers[] = $header;
}
public function setUserAgent( $user_agent_string ) {
$this->setOpt( CURLOPT_USERAGENT, $user_agent_string );
}
public function setPostData( $data ) {
$this->setOpt( CURLOPT_POSTFIELDS, $data );
return $this;
}
public function addCookie( $cookie ) {
$this->cookies[] = $cookie;
}
public function returnHeader( $value ) {
$this->setOpt( CURLOPT_HEADER, $value);
return $this;
}
public function exec() {
if ( count($this->headers) > 0 ) {
$this->setOpt( CURLOPT_HTTPHEADER, $this->headers );
}
$output = curl_exec( $this->curl_handle );
$info = curl_getinfo( $this->curl_handle );
return $output;
}
public function setOpt( $option, $value ) {
curl_setopt( $this->curl_handle, $option, $value);
return $this;
}
}
<?php
require_once 'curl.php';
class Request {
private $curl, $response;
public function __construct( $curl = null ) {
if ($curl) {
$this->curl = $curl;
}
}
static function get( $url ) {
$curl = new Curl( $url, Curl::GET );
return new Request( $curl );
}
static function post( $url, $data ) {
$curl = new Curl( $url, Curl::POST );
$curl->setPostData( $data );
return new Request( $curl );
}
static function put( $url, $data ) {
$curl = new Curl( $url, Curl::PUT );
$curl->setPostData( $data );
return new Request( $curl );
}
static function delete( $url ) {
$curl = new Curl( $url, Curl::DELETE );
return new Request( $curl );
}
static function lock( $url ) {
$curl = new Curl( $url, Curl::LOCK );
return new Request( $curl );
}
static function unlock( $url ) {
$curl = new Curl( $url, Curl::UNLOCK );
return new Request( $curl );
}
public function setHeader( $header, $value ) {
$this->curl->setHeader( $header . ': ' . $value );
}
public function setUserAgent( $user_agent_string ) {
$this->curl->setUserAgent( $user_agent_string );
}
public function addCookie( $cookie ) {
$this->curl->addCookie( $cookie );
}
public function setBasicAuth( $user, $password ) {
$this->curl->setOpt( CURLOPT_USERPWD, "{$user}:{$password}" );
}
public function process() {
$this->curl->setOpt( CURLOPT_SSL_VERIFYPEER, false )
->setOpt( CURLOPT_SSL_VERIFYHOST, 2 )
->setOpt( CURLOPT_RETURNTRANSFER, true)
->setOpt( CURLOPT_HEADER, true);
$this->response = $this->curl->exec();
return $this;
}
public function setResponse($response) {
$this->response = $response;
}
public function status() {
$count = preg_match("/^(HTTP.*)/", $this->response, $matches);
if ($count) {
return $matches[0];
}
}
public function statusCode() {
$status = $this->status();
$count = preg_match("/([\d]{3})/", $this->response, $matches);
if ($count) {
return (int) $matches[0];
}
}
public function header($key) {
$key = addslashes($key);
$pattern = "/{$key}: (.*)/";
$count = preg_match($pattern, $this->response, $matches);
if ($count) {
return $matches[1];
}
}
public function content() {
$count = preg_match("/(\<\?xml.*)/s", $this->response, $matches);
if ($count) {
return $matches[1];
}
}
public function xml() {
$content = $this->content();
return simplexml_load_string( $content );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment