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

时间:2013-10-29 12:20:21

标签: 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, 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_ipproxy_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并不完美所以我想知道而不是一遍又一遍地重写顶部卷曲我应该将它存储在一个函数中吗?

任何帮助表示感谢。

谢谢, 西蒙

编辑:

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

    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;

任何人都知道我有更好的方法来进行循环吗?

1 个答案:

答案 0 :(得分:2)

要从列表中获取随机代理,您可以使用此代码:

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

说明:

count($array)获取数组的长度

mt_rand($x,$y)获取$x$y之间的随机数

编辑:

完全可以像你一样做。然后总是像数组的第一个元素一样。

shuffle($array);
$array[0]

这两个选项中的哪一个最适合我无法真正说出的随机性。