Created
April 5, 2010 15:40
-
-
Save sweeney/356481 to your computer and use it in GitHub Desktop.
A Class Wrapper for various CURL methods from php, super useful
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
<? | |
Class Rest { | |
public $Gets; | |
public $Posts; | |
public $Url; | |
public $return; | |
public $responseHeader; | |
public $info; | |
public $Headers; | |
function __construct(){ | |
$this->Gets = (!is_array($this->Gets))?array():$this->Gets; | |
$this->Posts = (!is_array($this->Posts))?array():$this->Posts; | |
$this->Headers = (!is_array($this->Headers))?array():$this->Headers; | |
} | |
function AddUrl($url){ | |
$this->Url = $url; | |
} | |
function AddHeader($str=''){ | |
if($str){ | |
$this->Headers[] = $str; | |
} | |
} | |
function OverwriteHeaders($str=''){ | |
$this->Headers = array(); | |
$this->AddHeader($str); | |
} | |
function AddGetParam($name, $value, $Force = FALSE){ | |
if(!array_key_exists($name, $this->Gets)){ | |
$this->Gets[$name] = $value; | |
} else { | |
if($Force){ | |
$this->Gets[$name] = $value; | |
} | |
} | |
} | |
function AddPostParam($name, $value, $Force = FALSE){ | |
if(!array_key_exists($name, $this->Posts)){ | |
$this->Posts[$name] = $value; | |
} else { | |
if($Force){ | |
$this->Posts[$name] = $value; | |
} | |
} | |
} | |
function Sanitize($input){ | |
if(count($input)<1){return '';} | |
$output = ''; | |
foreach($input as $name=>$value){ | |
$output .= $name ."=".urlencode($value)."&"; | |
} | |
$output = substr($output, 0, -1); | |
return $output; | |
} | |
function MakeRequest(){ | |
if(!isset($this->Url)){ | |
die('No URL was specified to request'); | |
} | |
$ch = curl_init(); | |
$timeout = 0; | |
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); | |
curl_setopt ($ch, CURLOPT_HTTPHEADER, $this->Headers); | |
curl_setopt ($ch, CURLINFO_HEADER_OUT, 1); | |
if(count($this->Posts)>0 || isset($this->manualSanitized)){ | |
$posts = isset($this->manualSanitized) ? $this->manualSanitized : $this->Sanitize($this->Posts); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $posts); | |
curl_setopt ($ch, CURLOPT_URL, $this->Url); | |
} else { | |
if(count($this->Gets)>0){ | |
$gets = $this->Sanitize($this->Gets); | |
$newUrl = $this->Url . '?'. $gets; | |
curl_setopt ($ch, CURLOPT_URL, $newUrl); | |
} else { | |
curl_setopt ($ch, CURLOPT_URL, $this->Url); | |
} | |
} | |
$file_contents = curl_exec($ch); | |
$this->info = curl_getinfo($ch); | |
$this->info['gets'] = $this->Gets; | |
$this->info['posts'] = $this->Posts; | |
$this->info['return'] = $file_contents; | |
$this->info['manual_headers'] = $this->Headers; | |
$this->responseHeader = (isset($this->info['http_code'])) ? $this->info['http_code'] : '200'; | |
curl_close($ch); | |
$this->return = $file_contents; | |
return $file_contents; | |
} | |
function GetContent(){ | |
return $this->return; | |
} | |
function Clear(){ | |
unset($this->Url); | |
$this->Posts = array(); | |
$this->Gets = array(); | |
$this->return = ''; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment