Advertisement
Guest User

easycurl

a guest
Oct 6th, 2010
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.27 KB | None | 0 0
  1. <?php
  2. class easyCurl {
  3.     private $handle;
  4.  
  5.     function start($file=null)
  6.     {
  7.         $this->handle=curl_init('');
  8.         curl_setopt($this->handle, CURLOPT_RETURNTRANSFER, 1);
  9.         curl_setopt($this->handle, CURLOPT_FOLLOWLOCATION, 1);
  10.         curl_setopt($this->handle, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6 (.NET CLR 3.5.30729)");
  11.         if($file!=null)
  12.         {
  13.             curl_setopt($this->handle, CURLOPT_COOKIEFILE,  $file);
  14.             curl_setopt($this->handle, CURLOPT_COOKIEJAR,       $file);
  15.         }
  16.         return $this->handle;
  17.     }
  18.    
  19.     function prepareUserAnget($text) {
  20.         curl_setopt($this->handle, CURLOPT_USERAGENT, $text);
  21.     }
  22.    
  23.     function headersPrepare($headers=true) {
  24.         if($headers==false) {
  25.             curl_setopt($this->handle, CURLOPT_HEADER, 0);
  26.         } else {
  27.             curl_setopt($this->handle, CURLOPT_HEADER, 1);
  28.         }
  29.     }
  30.    
  31.     function prepareTimeOut($value=0) {
  32.         if($value<=0) {
  33.             curl_setopt($this->handle, CURLOPT_TIMEOUT, round($value,0));
  34.             curl_setopt($this->handle, CURLOPT_CONNECTTIMEOUT, round($value,0));
  35.         } else {
  36.            
  37.         }
  38.     }
  39.    
  40.     function noExpect() {
  41.         curl_setopt($this->handle, CURLOPT_HTTPHEADER, array('Expect:'));
  42.     }
  43.    
  44.     function prepareHttpProxy($ok=false,$ip=null,$port=8080) {
  45.         if($ok==true) {
  46.             if($ip==null) {
  47.                 curl_setopt($this->handle, CURLOPT_HTTPPROXYTUNNEL, 0               );
  48.                 return false;
  49.             }
  50.             curl_setopt($this->handle, CURLOPT_HTTPPROXYTUNNEL, 1               );
  51.             curl_setopt($this->handle, CURLOPT_PROXYTYPE,       CURLPROXY_HTTP  );
  52.             curl_setopt($this->handle, CURLOPT_PROXYPORT,       $port           );
  53.             curl_setopt($this->handle, CURLOPT_PROXY,           $ip             );
  54.             return true;
  55.         } else {
  56.             curl_setopt($this->handle, CURLOPT_HTTPPROXYTUNNEL, 0               );
  57.             return false;
  58.         }
  59.     }
  60.    
  61.     function prepareHttpAuth($mode='CURLAUTH_ANY',$user,$pass) {
  62.         curl_setopt($this->handle, CURLOPT_HTTPAUTH, $mode);
  63.         curl_setopt($this->handle, CURLOPT_USERPWD, $user.':'.$pass);
  64.     }
  65.    
  66.     function curlPrepare($url,$get_array=false,$post_array=false)
  67.     {
  68.         if(isset($url)&&strlen($url)>0)
  69.         {
  70.             curl_setopt($this->handle, CURLOPT_URL, $url);
  71.         }
  72.  
  73.         if(isset($get_array) && $get_array!=false)
  74.         {
  75.             curl_setopt($this->handle,CURLOPT_GET,1);
  76.             if(is_array($get_array))
  77.             {
  78.                 $getarray='';
  79.                 foreach($get_array as $key=>$value)
  80.                 {
  81.                     $getarray=$getarray.urlencode($key).'='.urlencode($value).'&';
  82.                 }
  83.                 curl_setopt($this->handle, CURLOPT_GETFIELDS, $getarray);
  84.             }
  85.         } else {
  86.             /*errdbg*/ // curl_setopt($this->handle, CURLOPT_GETFIELDS, '');
  87.             /*errdbg*/ // curl_setopt($this->handle, CURLOPT_GET, 0);
  88.         }
  89.        
  90.         if(isset($post_array) && $post_array!=false)
  91.         {
  92.             curl_setopt($this->handle,CURLOPT_POST,1);
  93.             if(is_array($post_array))
  94.             {
  95.                 $postarray='';
  96.                 foreach($post_array as $key=>$value)
  97.                 {
  98.                     $postarray=$postarray.urlencode($key).'='.urlencode($value).'&';
  99.                 }
  100.                 curl_setopt($this->handle, CURLOPT_POSTFIELDS, $postarray);
  101.             }
  102.         } else {
  103.             curl_setopt($this->handle, CURLOPT_POSTFIELDS, '');
  104.             curl_setopt($this->handle, CURLOPT_POST, 0);
  105.         }
  106.         return $this->handle;
  107.     }
  108.    
  109.     function grab()
  110.     {
  111.         return curl_exec($this->handle);
  112.     }
  113.    
  114.     function getError() {
  115.         return curl_error($this->handle);
  116.     }
  117.         function getErrno() {
  118.                 return curl_errno($this->handle);
  119.         }
  120.  
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement