【问题标题】:Proxy rotation with only live proxies仅使用实时代理的代理轮换
【发布时间】:2018-11-06 19:58:53
【问题描述】:

如何让我的 PHP 代码遍历数组并删除不起作用的代理?

<?php

$proxies = array(
"187.120.243.27:42026",
"138.185.56.166:32070",
"200.195.186.205:30698",
"170.239.46.2:41823",
"189.85.84.186:80",
);

//foreach value of array do the instructions below

$ch = curl_init();
curl_setopt($ch, CURLOPT_PROXY, $proxies);
//this value should be one value at time of the array 'proxies'
curl_setopt($ch,CURLOPT_URL,'http://icanhazip.com/');
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_HTTPHEADER,array(
    "User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0",
    "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    "Accept-Language: en-US,en;q=0.5",
    "DNT: 1",
    "Upgrade-Insecure-Requests: 1",
    "Connection: close"
));

$data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    if ($httpcode > 1) {
        echo "$data";
//live proxy stays on the list

    }

    else {
        //unset current array(remove not working proxy)
    }
?>

我已经完成了基本代码,但是我是 PHP 新手,有人可以帮助我吗? 想法是检查列表中的所有代理并删除不起作用的代理。

【问题讨论】:

    标签: php arrays curl foreach proxy


    【解决方案1】:

    我们可以在foreach 循环中访问键值对。我们使用$key =&gt; $value 语法分别访问它们。现在,当代理失败时,我们只需要使用unset() 函数,使用$key$proxies 数组中删除该代理值:

    // Initialize live_proxies array
    $live_proxies = $proxies;
    
    // In the foreach loop, we access key and its value
    // value is $proxy
    foreach ($live_proxies as $key => $proxy) {
    
        $ch = curl_init();
    
        curl_setopt($ch, CURLOPT_PROXY, $proxy);
        //this value should be one value at time of the array 'proxies'
    
        curl_setopt($ch,CURLOPT_URL,'http://icanhazip.com/');
        curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_HTTPHEADER,array(
            "User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0",
            "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
            "Accept-Language: en-US,en;q=0.5",
            "DNT: 1",
            "Upgrade-Insecure-Requests: 1",
            "Connection: close"
        ));
    
        $data = curl_exec($ch);
        $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    
        if ($httpcode > 1) {
            echo "$data";
        }
        else {
            // use unset function to remove current proxy value
            // from the live_proxies array using the $key
            unset($live_proxies[$key]);
        }
    }
    

    【讨论】:

    • 感谢您的超快速回答,我理解了代码,但我如何才能创建一个只有实时代理的新数组?
    • @eduardtrionleon 检查更新的答案。我制作了代理数组的副本并将其命名为 live_proxies;现在在循环中我修改副本(而不是原始)
    • 你太棒了,代码100%运行,非常感谢xD
    猜你喜欢
    • 1970-01-01
    • 2015-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    • 2019-09-28
    • 2019-09-16
    相关资源
    最近更新 更多