PHP从代理列表和Curl中设置随机变量(如果失败,请重复)

PHP从代理列表和Curl中设置随机变量(如果失败,请重复),php,curl,Php,Curl,我试图弄清楚如何从列表中随机选择一个代理ip,然后使用它执行curl,如果出现故障,则使用一个新的代理ip。这是我的工作代码,没有随机化: $url = "www.example.com"; $loginpassw = 'myproxypw'; $proxy_ip = '23.27.37.128'; $proxy_port = '29842'; $ch = curl_init(); curl_setopt($ch, C

我试图弄清楚如何从列表中随机选择一个代理ip,然后使用它执行curl,如果出现故障,则使用一个新的代理ip。这是我的工作代码,没有随机化:

    $url       = "www.example.com";
    $loginpassw = 'myproxypw';

    $proxy_ip = '23.27.37.128';
    $proxy_port = '29842';

    $ch        = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_PROXYPORT, $proxy_port);
    curl_setopt($ch, CURLOPT_PROXY, $proxy_ip);
    curl_setopt($ch, CURLOPT_PROXYUSERPWD, $loginpassw);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 600);
    $html  = curl_exec($ch);    

    if (strpos($html,'To continue, please type the characters below') !== false) {
        echo "now an error has occurred, let's try a new proxy";
    }

    curl_close($ch);
理想情况下,
proxy_ip
proxy_port
必须在列表中保持不变,例如:

$proxylist = array (
            array("ip" => "23.27.37.128", "port" => "29842"),
            array("ip" => "23.27.37.111", "port" => "29852"),
            array("ip" => "23.27.37.112", "port" => "29742"),
            array("ip" => "23.27.37.151", "port" => "29242")
             );
我想知道我是否可以使用shuffle:

shuffle($proxylist);

while($element = array_pop($proxylist)){
  return $element;
}
我的第二个问题是这样做的最佳方式,我的PHP并不完美,因此我想知道,与其一遍遍地重写top curl,不如将其存储在函数中

谢谢你的帮助

谢谢, 西蒙

编辑: 以下代码似乎在我将代码拆分为两个函数的地方起作用:

    function curltime($url, $proxy_ip, $proxy_port, $loginpassw){
            $ch        = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_PROXY, $proxy_ip);
            curl_setopt($ch, CURLOPT_PROXYUSERPWD, $loginpassw);
            curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
            curl_setopt($ch, CURLOPT_AUTOREFERER, true);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_TIMEOUT, 600);
            return curl_exec($ch);  
            curl_close($ch);
    }

//now let's do the curl

            $url       = "www.example.com";
            $proxylist = array (
            array("proxyip" => "23.27.37.128", "proxyport" => "29842"),
            array("proxyip" => "23.27.37.111", "proxyport" => "29852"),
            array("proxyip" => "23.27.37.112", "proxyport" => "29742"),
            array("proxyip" => "23.27.37.151", "proxyport" => "29242")
             );
            foreach ($proxylist[mt_rand(0,count($proxylist)-1)] as $key => $value) {
                $$key = $value;
            }
            $html = $this->curltime($url, $proxyip, $proxyport, 'somepassword');


            if (strpos($html,'To continue, please type the characters below') !== false) {
                echo "now we have errors so let's try again" 
            foreach ($proxylist[mt_rand(0,count($proxylist)-1)] as $key => $value) {
                $$key = $value;
            }
            $html = $this->curltime($url, $proxyip, $proxyport, 'somepassword');
            }
            $cache .= $html;

有谁知道我做循环的更好方法吗?要从列表中获取随机代理,您可以使用以下方法:

$proxylist[mt_rand(0,count($proxylist)-1)]
解释:

count($array)
获取数组长度

mt_rand($x,$y)
获取介于
$x
$y

编辑:

完全可以像你那样做。然后始终取数组的第一个元素

shuffle($array);
$array[0]

这两个选项中,哪一个最适合随机性,我真的说不上来。

谢谢,我现在正在测试这一点。太棒了,这就是我要编辑我的帖子的诀窍。我想知道他们是否是这样做的更好方法,例如,与随机操作相比,在这种情况下,它可能只使用同一个阻止的代理来执行下一个。我现在将接受这个答案,但会继续寻找更好的方法。谢谢你的帮助。