php - 检测代理服务器是否可用的最佳方法是什么?

标签 php curl proxy

我正在尝试编写一个工具来检查代理服务器是否已启动并可供使用。到目前为止,我已经在下面的类中提出了两种方法(我已经删除了对这个问题来说多余的 setter 和 getter)。

第一种方法使用 cURL 并尝试通过代理请求页面,第二种方法使用 fsockopen 并尝试打开与代理的连接。

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 那样一次检查一个>。

我没有对代理做太多工作,所以我很难判断这些方法是否足以验证代理是否可用,或者我是否缺少更好的方法。

最佳答案

嗯。尝试通过代理建立与安全(最有可能可用)URL 的连接并检查错误,听起来不错。对我来说。

为了绝对最大的安全性,您可能想添加另一个对另一个验证 URL 的调用(例如 Google 的某些东西),或者进行两次调用,以防万一。

关于php - 检测代理服务器是否可用的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2167340/

相关文章:

php - 接受来自信用卡 php 的 paypal 付款

perl - Curl Perl 模块不工作,缺少 formadd 方法

nginx - Docker Nginx 代理 : how to route traffic to different container using path and not hostname

docker - 使用动态配置在容器中运行Nginx作为反向代理

javascript - 如何在php中传递jquery日期参数

php - Eloquent 地如何搜索关系的值(value)

java - HTTP CURL 有效 - Java Jsoup 无效

php - 通过代理的PHP套接字连接

javascript - php的输出字节数组差异版本

php - 使用 PHP API 显示 MySQL 信息