Załączam Ci moją klasę do cURL`a:
<?php
class cURL {
public $userAgents = array( "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; WOW64; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; Media Center PC 5.0)",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)",
"Opera/9.63 (Windows NT 6.0; U; ru) Presto/2.1.1",
"Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5",
"Mozilla/5.0 (X11; U; Linux i686 (x86_64); en-US; rv:1.8.1.18) Gecko/20081203 Firefox/2.0.0.18",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.16) Gecko/20080702 Firefox/2.0.0.16",
"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6; en-us) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/3.2.1 Safari/525.27.1",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)",
"AmigaVoyager/3.4.4 (MorphOS/PPC native)",
"xChaos_Arachne/5.1.89;GPL,386+",
"Mozilla/5.0 (X11; U; Linux; en-US) AppleWebKit/523.15 (KHTML, like Gecko, Safari/419.3) Arora/0.2 (Change: 0 )",
"Mozilla/6.0; (Spoofed by Amiga-AWeb/3.5.07 beta)",
"Mozilla/4.61 [en] (X11; U; ) - BrowseX (2.0.0 Windows)",
"Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en; rv:1.8.1.14) Gecko/20080409 Camino/1.6 (like Firefox/2.0.0.14)",
"Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.5b) Gecko/20030917 Camino/0.7+",
"Mozilla/4.08 (Charon; Inferno)",
"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.237 Safari/534.10",
"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/532.5 (KHTML, like Gecko) Comodo_Dragon/4.0.1.6 Chrome/4.0.249.78 Safari/532.5",
"Contiki/1.0 (Commodore 64; <a href="http://dunkels.com/adam/contiki/)"" target="_blank">http://dunkels.com/adam/contiki/)"</a>,
"Democracy/0.8.1 (http://www.participatoryculture.org)",
"edbrowse/2.2.10",
"ELinks/0.12~pre2.dfsg0-1ubuntu1-lite (textmode; Debian; Linux 2.6.32-4-jolicloud i686; 143x37-2)",
"Emacs-W3/4.0pre.46 URL/p4.0pre.46 (i686-pc-linux; X11)",
"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.5) Gecko/20060731 Epiphany/2.14 Firefox/1.5.0.5");
public $proxies = array("http;localhost;8118"); //Format are type;ip;port, type can be only socks5 for now public $proxy = array(); //Chosed proxy addess, default there is no proxy public $ua = "Mozilla/5.0 (compatible; SimpleCURL/1.0 en-US)";
private $returnTransfer = true;
private $returnHeaders = false;
private $connTimeout = 10;
private $locationFollow = true; //Follow Location: CURLOPT_FOLLOWLOCATION
private $locationLimit = 10; //Prevent recursive redirection
private $keepSession = true; //keep session cookie or not
private $freshConn = false; //Always use fresh connection
function shuffleUA($save=true) {
$newUA = $this->userAgents[rand(0
, (count($this->userAgents)-1
))]; if(empty($newUA)) return false; //Maybe array is empty? if($save) $this->ua = $newUA;
return $newUA;
}
function shuffleProxy($save=true) {
$proxy = $this->proxies[rand(0
, (count($this->proxies)-1
))]; if(empty($proxy) || count($proxy) != 3
) return false; //Maybe array is empty or proxy is invalid $prx["type"] = $proxy[0]; //Proxy type
$prx["ip"] = $proxy[1]; //Proxy ip
$prx["port"] = $proxy[2]; //Proxy port
if($save) $this->proxy = $prx;
return $prx;
}
private function initCURL
($url, $ref, $method="get", $cookies=array(), $postFields=array(), $headers=array()) { $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, $this->returnTransfer); //Return document instead echo
curl_setopt($ch, CURLOPT_HEADER, $this->returnHeaders); //Return headers
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connTimeout); //Connection timeout
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $this->locationFollow); //Follow Location: headers
curl_setopt($ch, CURLOPT_MAXREDIRS, $this->locationLimit); //Following Location: limit
curl_setopt($ch, CURLOPT_COOKIESESSION, $this->keepSession); //Keep sessions cookies
curl_setopt($ch, CURLOPT_FRESH_CONNECT, $this->freshConn); //Always use fresh connection instead of keep-alive
curl_setopt($ch, CURLOPT_USERAGENT, $this->ua); //Set useragent
if(!empty($ref)) curl_setopt
($ch, CURLOPT_REFERER
, $ref); //Set referer if(!empty($headers)) curl_setopt
($ch, CURLOPT_HTTPHEADER
, $headers); //Extra custom headers
if(!empty($this->proxy)) { curl_setopt($ch, CURLOPT_PROXYTYPE, (($this->proxy["type"]=="socks5")?CURLPROXY_SOCKS5:CURLPROXY_HTTP));
curl_setopt($ch, CURLOPT_PROXY, $this->proxy["ip"].":".$this->proxy["port"]);
}
$cURLcook = ""; //Prevent E_NOTICE
curl_setopt
($ch, CURLOPT_COOKIE
, substr($cURLcook, 0
, -1
)); //Do not forgot to cur last ; }
if($method=="post") {
curl_setopt($ch, CURLOPT_POST, true);
if(!empty($postFields)) { $cURLpf = ""; //Prevent E_NOTICE
curl_setopt
($ch, CURLOPT_POSTFIELDS
, substr($cURLpf, 0
, -1
)); //Do not forgot to cur last & }
}
return $ch;
}
function getData
($url, $ref, $method="get", $post=array()) { $ch = $this->initCURL($url, $ref, $method, array(), $post); $buffer = curl_exec($ch);
//var_dump(curl_error($ch));
curl_close($ch);
return $buffer;
}
}
?>
Aby jej tworzysz jej obiekt, ustawiasz co potrzeba (domyślnie nie musisz nic), wywołujesz metodę getData() z odpowiednimi parametrami.