<?php
/*
|============================================================|
|----------------------- cURL Wrapper -----------------------|
|---------------------- By Tammam Nima ----------------------|
|---------------------- tammamnima.com ----------------------|
|============================================================|
|------------------------------------------------------------|
|-------- Only ugly people remove this comment block --------|
|------------------------------------------------------------|
|------------------------------------------------------------|
|------------------------------------------------------------|
|------ If you can see this comment, rest assured that ------|
|---------- whoever posted this script is not ugly ----------|
|------------------------------------------------------------|
|--------------------- Have a nice day. ---------------------|
|------------------------------------------------------------|
|============================================================|
*/
class cURLWrapper {
private $curlHandle;
private $cookieFile;
const DEBUGGING = false; //true;
public function __construct() {
$this->debug("Creating cURL Session.");
$this->curlHandle = curl_init();
curl_setopt($this->curlHandle, CURLOPT_RETURNTRANSFER, true);
$this->setTimeout(60); //set default timeout of 1 min.
}
public function __destruct() {
$this->debug("Closing cURL Session.");
curl_close($this->curlHandle);
}
public function sendRequest($executeURL, &$output){
$this->debug("Sending cURL Request.");
curl_setopt($this->curlHandle, CURLOPT_URL, $executeURL);
$output = curl_exec($this->curlHandle);
if($output === false){
$this->debug("Error: " . curl_error($this->curlHandle));
$output = "CURL ERROR:\n" . curl_error($this->curlHandle);
//die($output);
return false;
}
return true;
}
public function setDLMode( $fileHandle ){
if( !curl_setopt( $this->curlHandle, CURLOPT_FILE, $fileHandle ) ){
echo "FAIL";
die();
}
}
public function cancelDLMode(){
curl_setopt( $this->curlHandle, CURLOPT_RETURNTRANSFER, true );
}
public function verbose(){
curl_setopt( $this->curlHandle, CURLOPT_VERBOSE, true );
}
public function setCookieFile($fileName){
$this->debug("Setting Cookie File To: \"" . $fileName . "\".");
$this->cookieFile = $fileName;
curl_setopt($this->curlHandle, CURLOPT_COOKIEJAR, $this->cookieFile);
curl_setopt($this->curlHandle, CURLOPT_COOKIEFILE, $this->cookieFile);
}
public function clearCookies(){
$this->debug("Clearing Cookies.");
if($this->cookieFile != ""){
$fileHandle = fopen($this->cookieFile, 'w');
fwrite($fileHandle, "");
fclose($fileHandle);
} else {
$this->debug("Cookie file not set. Unable to clear.");
trigger_error("Cookie file not set. Unable to clear.");
}
}
public function newSession(){
curl_setopt( $this->curlHandle, CURLOPT_COOKIESESSION, true );
}
public function forbidReuse(){
curl_setopt( $this->curlHandle, CURLOPT_FORBID_REUSE, true );
}
public function freshConnect(){
curl_setopt( $this->curlHandle, CURLOPT_FRESH_CONNECT, true );
}
public function disallowVerifyingCertificates(){
$this->debug("Disallowing Verifying Peer SSL Certificate.");
curl_setopt($this->curlHandle, CURLOPT_SSL_VERIFYPEER, false);
}
public function setTimeout($timeout){
if(is_numeric($timeout)){
$this->debug("Setting Timeout To " . $timeout . " Seconds.");
curl_setopt($this->curlHandle, CURLOPT_TIMEOUT, $timeout);
curl_setopt($this->curlHandle, CURLOPT_CONNECTTIMEOUT, $timeout);
} else {
$this->debug("Timeout Provided Is Not Numeric");
trigger_error("Timeout Provided Is Not Numeric");
}
}
public function setHTTPProxy($proxyIP, $proxyPort){
$this->debug("Setting HTTP Proxy To \"" . $proxyIP . ":" . $proxyPort . "\".");
curl_setopt($this->curlHandle, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
curl_setopt($this->curlHandle, CURLOPT_PROXY, $proxyIP);
curl_setopt($this->curlHandle, CURLOPT_PROXYPORT, $proxyPort);
}
public function setCustomHeaders($headersArray){
curl_setopt($this->curlHandle, CURLOPT_HTTPHEADER, $headersArray);
}
public function setReferer($referer){
curl_setopt($this->curlHandle, CURLOPT_REFERER, $referer);
}
public function setVerbose($verbose){
curl_setopt($this->curlHandle, CURLOPT_VERBOSE, $verbose);
}
public function setUserAgent($userAgent){
$this->debug("Setting User Agent To \"" . $userAgent . "\".");
curl_setopt($this->curlHandle, CURLOPT_USERAGENT, $userAgent);
}
public function enableRedirect(){
$this->debug("Allowing cURL To Handle Redirects.");
curl_setopt($this->curlHandle, CURLOPT_FOLLOWLOCATION, true);
}
public function setPOSTFields($postFields){
$this->debug("Setting POST Fields To \"" . $postFields . "\"");
curl_setopt($this->curlHandle, CURLOPT_POST, true);
curl_setopt($this->curlHandle, CURLOPT_POSTFIELDS, $postFields);
}
public function setGET(){
$this->debug("Setting Method To GET");
curl_setopt($this->curlHandle, CURLOPT_POST, false);
}
private function debug($output, $newLine = true){
//print, output to file, whatever.
if(self::DEBUGGING){
echo $output . ($newLine ? "\n" : "");
}
}
}
?>