2017-08-27 54 views
0

我有捲曲隨機IP腳本,但我不知道如何使用端口相同的隨機腳本如何提隨機的IP捲曲腳本IP端口

function curl_get($kix) 
{ 
$ips = array(
    '85.10.230.132', 
    '88.198.242.9', 
    '88.198.242.10', 
    '88.198.242.11', 
    '88.198.242.12', 
    '88.198.242.13', 
    '88.198.242.14', 
); 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_URL, $kix); 
    @curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); 
    curl_setopt($ch, CURLOPT_INTERFACE, $ips[rand(0, count($ips)-1)]); 
    curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); 
    $html = curl_exec($ch); 
    curl_close($ch); 
    return $html; 
} 

您可以在這裏看到我剛纔提到的IP但我不知道如何提及這些IP的端口。

在此先感謝!

+1

它是一個固定端口還是一個隨機端口? – funilrys

+1

@azarudeen對於每個「IP」,你有不同的端口? –

+0

是的,對於每個IP我有不同的端口 – azarudeen

回答

0

如果每個IP有不同的端口,則可以使用關聯數組將端口關聯到IP。

因此,對於以下函數,我假設您使用PHP> = 7.0以及下面的格式作爲我們的數組。

array('IP'=>'Port'); 

我們也CURLOPT_PORT分配替代IP與連接。 引述:

CURLOPT_PORT:的替代端口號連接到。

所以回到你的問題:

  • 我用curl_setopt($ch, CURLOPT_PORT, $port);分配,我們需要使用的端口。
  • 我用array_rand($ips)擺脫新創建的關聯數組隨機密鑰(IP列表)
  • 最後我得到使用$port = $ips[$randomIP];

所以你的函數現在看起來與直接訪問陣列端口如:

function curl_get($kix) 
{ 
    $ips = array(
     '85.10.230.132' => '80', 
     '88.198.242.9' => '8080', 
     '88.198.242.10' => '5754', 
     '88.198.242.11' => '80', 
     '88.198.242.12' => '8888', 
     '88.198.242.13' => '8989', 
     '88.198.242.14' => '8080', 
    ); 

    // We get a random key (IP) 
    $randomIP = array_rand($ips); 

    // We get the port according to the random key (IP) 
    $port = $ips[$randomIP]; 

    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_URL, $kix); 
    @curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); 
    curl_setopt($ch, CURLOPT_INTERFACE, $randomIP); 

    // We set the alternative port with the following 
    curl_setopt($ch, CURLOPT_PORT, $port); 

    curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); 

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

    return $html; 
}