如何最好地检测代理服务器是否可用?

5
我正在尝试编写一个工具,以检查代理服务器是否可用。到目前为止,我已经在下面的类中提供了两种方法(我已经删除了与此问题无关的setter和getter)。
第一种方法使用并尝试通过代理请求页面,第二个工具使用并只尝试打开到代理的连接。
class ProxyList {
    /**
     * You could set this to localhost, depending on your environment
     * @var string The URL that the proxy validation method will use to check proxies agains
     * @see ProxyList::validate()
     */
    const VALIDATION_URL = "http://m.www.yahoo.com/robots.txt";
    const TIMEOUT        = 3;

    private static $valid = array(); // Checked and valid proxies
    private $proxies      = array(); // An array of proxies to check

    public function validate($useCache=true) {
        $mh       = curl_multi_init();
        $ch       = null;
        $handles  = array();
        $delay    = count($this->proxies) * 10000;
        $running  = null;
        $proxies  = array();
        $response = null;

        foreach ( $this->proxies as $p ) {
            // Using the cache and the proxy already exists?  Skip the rest of this crap
            if ( $useCache && !empty(self::$valid[$p]) ) {
                $proxies[] = $p;
                continue;
            }

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_HTTP_VERSION,    CURL_HTTP_VERSION_1_1);
            curl_setopt($ch, CURLOPT_URL,             self::VALIDATION_URL);
            curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, true);
            curl_setopt($ch, CURLOPT_PROXY,           $p);
            curl_setopt($ch, CURLOPT_NOBODY,          true); // Also sets request method to HEAD
            curl_setopt($ch, CURLOPT_HEADER,          false);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION,  true);
            curl_setopt($ch, CURLOPT_TIMEOUT,         self::TIMEOUT);

            curl_multi_add_handle($mh, $ch);
            $handles[$p] = $ch;
        }

        // Execute the multi-handle
        do {
            curl_multi_exec($mh, $running);
            usleep($delay);
        } while ( $running );

        // Get the results of the requests
        foreach ( $handles as $proxy => $ch ) {
            $status = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);

            // Great success
            if ( $status >= 200 && $status < 300 ) {
                self::$valid[$proxy] = true;
                $proxies[] = $proxy;
            }
            else {
                self::$valid[$proxy] = false;
            }

            // Cleanup individual handle
            curl_multi_remove_handle($mh, $ch);
        }

        // Cleanup multiple handle
        curl_multi_close($mh);

        return $this->proxies = $proxies;
    }

    public function validate2($useCache=true) {
        $proxies = array();

        foreach ( $this->proxies as $proxy ) {
            // Using the cache and the proxy already exists?  Skip the rest of this crap
            if ( $useCache && !empty(self::$valid[$proxy]) ) {
                $proxies[] = $proxy;
                continue;
            }

            list($host, $post) = explode(":", $proxy);

            if ( $conn = @fsockopen($host, $post, $errno, $error, self::TIMEOUT) ) {
                self::$valid[$proxy] = true;
                $proxies[] = $proxy;
                fclose($conn);
            } else {
                self::$valid[$proxy] = false;
            }
        }

        return $this->proxies = $proxies;
    }
}

到目前为止,我更喜欢使用cURL方法,因为它允许我并行检查大批量的代理,速度非常快,而不像fsockopen一次只能检查一个。我没有做过太多有关代理的工作,所以很难判断这两种方法是否足以验证代理是否可用,或者是否有更好的方法我不知道。
2个回答

1

cURL是首选的方式,因为它具有multi_exec功能。

我不会费心去做两次检查,而是立即进行谷歌(或代理评判)调用。代理有时可以允许套接字,但只是无法获取内容:因此,您的cURL方法将是安全且不那么慢的。

正如Pekka所提到的:这取决于预期使用方式。

如果您使用Charon并收集了大量代理,则需要对它们进行代理评判,并且我想知道周转时间(以避免慢代理)和匿名性。

如果您想将其用作企业代理的监视系统,我只想确保它可以获取页面。

通过使用cURL获取URL来检查代理的(混乱)示例。

简而言之:使用cURL,它可以处理并行请求,并且最稳定而不会太慢(不进行双重检查)。 http://www.oooff.com/php-affiliate-seo-blog/php-automation-coding/easy-php-proxy-checker-writing-tutorial/


1

嗯,尝试通过代理连接到一个安全的(很可能可用的)URL,并检查错误,听起来还不错。

为了确保绝对最大的安全性,您可能需要添加另一个调用另一个验证URL(例如Google的某些内容),或进行两个调用,以防万一。


第二次可用性检查听起来像是个好主意,但是请求越多,性能就越成为一个问题。 - Justin Johnson
真的。我想这取决于预期的使用方式。 - Pekka

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接