2016-03-03 95 views
0

我想湊使用cURL使用代理的URL。捲曲與代理不起作用

當我不使用代理的URL被刮掉,但是當我做,我得到的空白頁返回。

這裏是我的代碼,

global $curl_init; 
    $proxy = '201.172.53.123:10000'; 
//$proxyauth = 'user:password'; 
    curl_setopt($curl_init, CURLOPT_URL, $url); 
    curl_setopt($curl_init, CURLOPT_PROXY, $proxy); 
//curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth); 
    curl_setopt($curl_init, CURLOPT_FOLLOWLOCATION, 1); 
    curl_setopt($curl_init, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($curl_init, CURLOPT_HEADER, 1); 

    curl_setopt($curl_init, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17'); 
    curl_setopt($curl_init, CURLOPT_SSL_VERIFYPEER, false); 
    echo $curl_scraped_page = curl_exec($curl_init); 

我使用的免費代理從http://proxylist.hidemyass.com/

我要在我的代碼的任何錯誤?或免費代理不工作?

+0

嘗試在'curl_exec'行之後寫'echo curl_error($ curl_init);''。什麼是輸出? – Curious

回答

-1

首先,負載代理放入數組

$proxies_list = array(); 
$proxies_list [] = 'user:[email protected]:76891'; // Some proxies requires user, password, IP and port no. 
$proxies_list [] = 'user:[email protected]:76891'; 
$proxies_list [] = 'user:[email protected]: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)]; 
} 

現在,初始化捲曲手柄後,將捲曲的CURLOPT_PROXY選項來隨機選擇代理服務器,將所有其他捲曲選項,然後執行請求,並關閉句柄。

$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); 
+0

不是有用的代碼在所有 – Umair