带代理的cURL不起作用

时间:2016-03-03 18:11:15

标签: php curl proxy

我想使用带代理的cURL来抓取网址。

当我不使用代理时,网址会被删除,但是当我这样做时,我会返回空白页。

这是我的代码,

(deref a)

我正在使用http://proxylist.hidemyass.com/

中的免费代理

我的代码中有任何错误吗?或免费代理根本不工作?

1 个答案:

答案 0 :(得分:-1)

首先,将代理加载到数组

$proxies_list = array(); 
$proxies_list [] = 'user:password@198.235.11.134:76891';  // Some proxies requires user, password, IP and port no.
$proxies_list [] = 'user:password@198.235.120.69:76891';
$proxies_list [] = 'user:password@198.235.46.176:76891';
$proxies_list [] = '198.235.92.107';  // Some proxies only require IP
$proxies_list [] = '198.235.93.94';
$proxies_list [] = '198.235.94.90:76891'; //Some proxies require IP,port no.
$proxies_list [] = '69.147.240.61:76891';

•接下来,从列表中选择任何随机代理以便稍后使用。

if (isset($proxies)) {  
    $proxy = $proxies[array_rand($proxies)];   
}

现在,在初始化cURL句柄后,将cURL的CURLOPT_PROXY选项设置为随机选择的代理,设置所有其他cURL选项,然后执行请求并关闭句柄。

$ch = curl_init(); 
// Setting proxy option for cURL
if (isset($proxy)) {    // If the $proxy variable is set, then
    curl_setopt($ch, CURLOPT_PROXY, $proxy);    // Set CURLOPT_PROXY with proxy in $proxy variable
}

// Set any other cURL options that are required
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);

$results = curl_exec($ch); 
curl_close($ch);