PHP cURL Wrapper

  1. <?php
  2. /*
  3. |============================================================|
  4. |----------------------- cURL Wrapper -----------------------|
  5. |---------------------- By Tammam Nima ----------------------|
  6. |---------------------- tammamnima.com ----------------------|
  7. |============================================================|
  8. |------------------------------------------------------------|
  9. |-------- Only ugly people remove this comment block --------|
  10. |------------------------------------------------------------|
  11. |------------------------------------------------------------|
  12. |------------------------------------------------------------|
  13. |------ If you can see this comment, rest assured that ------|
  14. |---------- whoever posted this script is not ugly ----------|
  15. |------------------------------------------------------------|
  16. |--------------------- Have a nice day. ---------------------|
  17. |------------------------------------------------------------|
  18. |============================================================|
  19. */
  20.     class cURLWrapper {
  21.         private $curlHandle;
  22.         private $cookieFile;
  23.        
  24.         const DEBUGGING = false; //true;
  25.        
  26.         public function __construct() {
  27.             $this->debug("Creating cURL Session.");
  28.             $this->curlHandle = curl_init();
  29.             curl_setopt($this->curlHandle, CURLOPT_RETURNTRANSFER, true);
  30.             $this->setTimeout(60);      //set default timeout of 1 min.
  31.         }
  32.        
  33.         public function __destruct() {
  34.             $this->debug("Closing cURL Session.");
  35.             curl_close($this->curlHandle);
  36.         }
  37.        
  38.         public function sendRequest($executeURL, &$output){
  39.             $this->debug("Sending cURL Request.");
  40.             curl_setopt($this->curlHandle, CURLOPT_URL, $executeURL);
  41.             $output = curl_exec($this->curlHandle);
  42.            
  43.             if($output === false){
  44.                 $this->debug("Error: " . curl_error($this->curlHandle));
  45.                 $output = "CURL ERROR:\n" . curl_error($this->curlHandle);
  46.                 //die($output);
  47.                 return false;
  48.             }
  49.             return true;
  50.         }
  51.        
  52.         public function setDLMode( $fileHandle ){
  53.             if( !curl_setopt( $this->curlHandle, CURLOPT_FILE, $fileHandle ) ){
  54.                 echo "FAIL";
  55.                 die();
  56.             }
  57.         }
  58.        
  59.         public function cancelDLMode(){
  60.             curl_setopt( $this->curlHandle, CURLOPT_RETURNTRANSFER, true );
  61.         }
  62.        
  63.         public function verbose(){
  64.             curl_setopt( $this->curlHandle, CURLOPT_VERBOSE, true );
  65.         }
  66.        
  67.         public function setCookieFile($fileName){
  68.             $this->debug("Setting Cookie File To: \"" . $fileName . "\".");
  69.             $this->cookieFile = $fileName;
  70.             curl_setopt($this->curlHandle, CURLOPT_COOKIEJAR, $this->cookieFile);
  71.             curl_setopt($this->curlHandle, CURLOPT_COOKIEFILE, $this->cookieFile);
  72.         }
  73.        
  74.         public function clearCookies(){
  75.             $this->debug("Clearing Cookies.");
  76.            
  77.             if($this->cookieFile != ""){
  78.                 $fileHandle = fopen($this->cookieFile, 'w');
  79.                 fwrite($fileHandle, "");
  80.                 fclose($fileHandle);
  81.             } else {
  82.                 $this->debug("Cookie file not set. Unable to clear.");
  83.                 trigger_error("Cookie file not set. Unable to clear.");
  84.             }
  85.         }
  86.        
  87.         public function newSession(){
  88.             curl_setopt( $this->curlHandle, CURLOPT_COOKIESESSION, true );
  89.         }
  90.        
  91.         public function forbidReuse(){
  92.             curl_setopt( $this->curlHandle, CURLOPT_FORBID_REUSE, true );
  93.         }
  94.        
  95.         public function freshConnect(){
  96.             curl_setopt( $this->curlHandle, CURLOPT_FRESH_CONNECT, true );
  97.         }
  98.        
  99.         public function disallowVerifyingCertificates(){
  100.             $this->debug("Disallowing Verifying Peer SSL Certificate.");
  101.             curl_setopt($this->curlHandle, CURLOPT_SSL_VERIFYPEER, false);
  102.         }
  103.        
  104.         public function setTimeout($timeout){
  105.             if(is_numeric($timeout)){
  106.                 $this->debug("Setting Timeout To " . $timeout . " Seconds.");
  107.                 curl_setopt($this->curlHandle, CURLOPT_TIMEOUT, $timeout);
  108.                 curl_setopt($this->curlHandle, CURLOPT_CONNECTTIMEOUT, $timeout);
  109.             } else {
  110.                 $this->debug("Timeout Provided Is Not Numeric");
  111.                 trigger_error("Timeout Provided Is Not Numeric");
  112.             }
  113.         }
  114.        
  115.         public function setHTTPProxy($proxyIP, $proxyPort){
  116.             $this->debug("Setting HTTP Proxy To \"" . $proxyIP . ":" . $proxyPort . "\".");
  117.             curl_setopt($this->curlHandle, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
  118.             curl_setopt($this->curlHandle, CURLOPT_PROXY, $proxyIP);
  119.             curl_setopt($this->curlHandle, CURLOPT_PROXYPORT, $proxyPort);
  120.         }
  121.        
  122.         public function setCustomHeaders($headersArray){
  123.             curl_setopt($this->curlHandle, CURLOPT_HTTPHEADER, $headersArray);
  124.         }
  125.        
  126.         public function setReferer($referer){
  127.             curl_setopt($this->curlHandle, CURLOPT_REFERER, $referer);
  128.         }
  129.        
  130.         public function setVerbose($verbose){
  131.             curl_setopt($this->curlHandle, CURLOPT_VERBOSE, $verbose);
  132.         }
  133.        
  134.         public function setUserAgent($userAgent){
  135.             $this->debug("Setting User Agent To \"" . $userAgent . "\".");
  136.             curl_setopt($this->curlHandle, CURLOPT_USERAGENT, $userAgent);
  137.         }
  138.        
  139.         public function enableRedirect(){
  140.             $this->debug("Allowing cURL To Handle Redirects.");
  141.             curl_setopt($this->curlHandle, CURLOPT_FOLLOWLOCATION, true);
  142.         }
  143.        
  144.         public function setPOSTFields($postFields){
  145.             $this->debug("Setting POST Fields To \"" . $postFields . "\"");
  146.             curl_setopt($this->curlHandle, CURLOPT_POST, true);
  147.             curl_setopt($this->curlHandle, CURLOPT_POSTFIELDS, $postFields);
  148.         }
  149.        
  150.         public function setGET(){
  151.             $this->debug("Setting Method To GET");
  152.             curl_setopt($this->curlHandle, CURLOPT_POST, false);
  153.         }
  154.        
  155.        
  156.         private function debug($output, $newLine = true){
  157.             //print, output to file, whatever.
  158.             if(self::DEBUGGING){
  159.                 echo $output . ($newLine ? "\n" : "");
  160.             }
  161.         }
  162.     }
  163. ?>