PHP CURL 使用代理(官方)

/**
 	CURLOPT_PROXYTYPE :
 	   CURLPROXY_HTTP,    CURLPROXY_HTTP_1_0
       CURLPROXY_SOCKS4,  CURLPROXY_SOCKS4A
       CURLPROXY_SOCKS5,  CURLPROXY_SOCKS5_HOSTNAME    
*/

$proxy_list = array(	"http"=>"107.150.96.188:8080",
						"https"=>"162.208.49.45:3127",
						"socks4"=>"211.236.185.151:1080",
						"socks5"=>"218.21.230.156:443");

foreach ($proxy_list as $key=>$proxy){
	echo $key . "=>" . $proxy . "<br>";
	
	$ch = curl_init ();
	curl_setopt ( $ch, CURLOPT_URL, "http://www.baidu.com" );	// http://proxy.mimvp.com
	
	curl_setopt ( $ch, CURLOPT_HTTPPROXYTUNNEL, false );		
	curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
	curl_setopt ( $ch, CURLOPT_PROXY, $proxy );
	
	if ($key == "http") {
		curl_setopt ( $ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP );
	} 
	elseif ($key == "https") {
		curl_setopt ( $ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTPS );
	} 
	elseif ($key == "socks4") {
		curl_setopt ( $ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4 );
	} 
	elseif ($key == "socks5") {
		curl_setopt ( $ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5 );
	}
	else {
		curl_setopt ( $ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP );
	}
		
	curl_setopt ( $ch, CURLOPT_TIMEOUT, 60 );
	curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 60 );
	curl_setopt ( $ch, CURLOPT_HEADER, false );
	curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
	
	$result = curl_exec ( $ch );
	$curl_info = curl_getinfo($ch);
	echo $curl_info['size_download'] . "<br><br>";

	curl_close ( $ch );
}


/**
  RUN Result: 
  
 	http=>107.150.96.188:8080
	14613
	
	https=>36.250.69.4:80
	14613
	
	socks4=>211.236.185.151:1080
	14613
	
	socks5=>218.21.230.156:443
	14613
*/

更多代理,请见米扑代理: http://proxy.mimvp.com

更多用法,请见米扑示例:http://proxy.mimvp.com/demo.php

 

使用CURL库访问代理服务器

function curl_string ($url,$user_agent,$proxy){
       $ch = curl_init();
       curl_setopt ($ch, CURLOPT_PROXY, $proxy);
       curl_setopt ($ch, CURLOPT_URL, $url);
       curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
       curl_setopt ($ch, CURLOPT_COOKIEJAR, "c:\cookie.txt");
       curl_setopt ($ch, CURLOPT_HEADER, 1);
       curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
       curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
       curl_setopt ($ch, CURLOPT_TIMEOUT, 120);
       $result = curl_exec ($ch);
       curl_close($ch);
       return $result;
}

$url_page = "http://www.google.com";
$user_agent = "Mozilla/4.0";
$proxy = "http://192.11.222.124:8000";
$string = curl_string($url_page,$user_agent,$proxy);
echo $string;

 

简洁代码

$proxy = "80.25.198.25";
$proxyport = "8080";
$ch = curl_init("http://sfbay.craigslist.org/");

curl_setopt($ch, curlOPT_RETURNTRANSFER,1);
curl_setopt($ch,curlOPT_proxy,$proxy);
curl_setopt($ch,curlOPT_proxyPORT,$proxyport);
curl_setopt ($ch, CURLOPT_TIMEOUT, 120);

$result = curl_exec($ch);
echo $result;

curl_close($ch); 

 

参考推荐:

PHP实现Curl代理proxy爬取网页

PHP CURL使用代理来访问目标

PHP获取网页内容方法总结

PHP获得网页源代码抓取网页内容的几种方法

一个简单的增强型PHP curl函数