2012-11-16 64 views
1

我有一個Pagerank檢查腳本,我想添加代理。我通常使用Curl,因此我不確定如何通過代理來完成請求。我希望請求從$ proxies數組中選擇一個隨機代理並將其用於請求。任何人都可以通過使用這個腳本來完成這個過程。提前致謝。使用代理與fsockopen

$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; 
    } 
} 
+0

如果你平時使用捲曲,爲什麼你不這樣做呢?通過fsockopen更麻煩;你在'GET'頭部添加絕對URL,而將'Host:'留空。 (更簡單的方法是使用'fopen'和適當的[context params](http://www.php.net/manual/en/context.http.php)代理。) – mario

回答

3

隨着fsockopen,而不是toolbarqueries.google.com您連接到所選擇的代理。然後你發送一個完整的URL與GET請求:

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

,您仍然可以發送Host頭,但你並不需要它。