1

我有一个要添加代理的 Pagerank 检查脚本。我通常使用 Curl,所以我不确定如何通过代理发出请求。我希望请求从 $proxies 数组中选择一个随机代理并将其用于请求。谁能告诉我如何使用此脚本完成此操作。提前致谢。

$proxyauth = "username:password";

$proxies = array(
    "proxy1",
    "proxy2",
    "proxy3",
    "proxy4",
    "proxy5",
    "proxy6",
    "proxy7",
    "proxy8",
    "proxy9",
    "proxy10"
);

function check($page){
    // Open a socket to the toolbarqueries address, used by Google Toolbar
    $socket = fsockopen("toolbarqueries.google.com", 80, $errno, $errstr, 30);

    // If a connection can be established
    if($socket) {
        // Prep socket headers
        $out = "GET /tbr?client=navclient-auto&ch=".$this->checkHash($this->createHash($page))
                ."&features=Rank&q=info:".$page."&num=100&filter=0 HTTP/1.1\r\n";
        $out .= "Host: toolbarqueries.google.com\r\n";
        $out .= "User-Agent: Mozilla/4.0 (compatible; GoogleToolbar 2.0.114-big; Windows XP         5.1)\r\n";
        $out .= "Connection: Close\r\n\r\n";

        // Write settings to the socket
        fwrite($socket, $out);

        // When a response is received...
        $result = "";
        while(!feof($socket)) {
            $data = fgets($socket, 128);
            $pos = strpos($data, "Rank_");
            if($pos !== false){
                $pagerank = substr($data, $pos + 9);
                $result += $pagerank;
            }
        }
        // Close the connection
        fclose($socket);

        // Return the rank!
        return $result;
    }
}
4

1 回答 1

3

使用fsockopen, 而不是toolbarqueries.google.com您连接到选定的代理。然后,您使用 GET 请求发送完整的 URL:

$socket = fsockopen("proxy5", 80, $errno, $errstr, 30);
...
$out = "GET http://toolbarqueries.google.com/tbr?client=..."

您仍然可以发送Host标头,但您不需要它。

于 2012-11-16T21:10:00.410 回答