2009-09-18 18 views
2

我在使用curl爲少數站點檢索標題時遇到問題。在PHP中使用cURL,無法獲取少數站點的標題

一些示例是digg.com和microsoft.com。

function get_headers_curl($url, $port) 
{ 
    $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_URL,   $url); 
    curl_setopt($ch, CURLOPT_HEADER,   true); 
    curl_setopt($ch, CURLOPT_NOBODY,   true); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_PORT,   $port); 
    curl_setopt($ch, CURLOPT_TIMEOUT,  10); 

    $r = curl_exec($ch); 
    $r = split("\n", $r); 
    return $r; 
} 

這是功能和我目前使用的選項,以及易於使用的我有一個小的測試腳本運行@ isitup.org/test.php?d=example.com。它只是返回響應的標題,並且示例網站缺少一個。

問題是這些網站似乎無視請求,我沒有得到任何迴應。我已經玩過不同的選項,但似乎無法得到迴應。

有什麼我失蹤了嗎?或者,它是不是可以使用curl來訪問這些網站?

問候,

山姆

編輯:

test.php的是以下幾點:

<?php 

$domain = preg_replace("/[^A-Za-z0-9-\/\.\:]/", "", trim($_GET["d"])); 

$agent = "Mozilla/5.0 (X11; U; Linux i686; pl-PL; rv:1.9.0.2) Gecko/20121223 Ubuntu/9.25 (jaunty) Firefox/3.8"; 

function get_headers_curl($url, $port) 
{ 
    $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_URL,   $url); 
// curl_setopt($ch, CURLOPT_HEADER,   true); 
// curl_setopt($ch, CURLOPT_NOBODY,   true); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_PORT,   $port); 
    curl_setopt($ch, CURLOPT_TIMEOUT,  10); 
    curl_setopt($ch, CURLOPT_USERAGENT,  $agent); 


    $r = curl_exec($ch); 
    $r = split("\n", $r); 
    return $r; 
} 

$headers = get_headers_curl("http://".$domain, 80); 

print("<pre>".print_r($headers,true)."</pre>"); 


?> 

然而,新的用戶代理仍然沒有得到來自這些網站的響應。 ..

更新:Woops看到我的錯誤,轉移到func代理重刑和它是有效的!感謝:P

+0

您可以發佈您在test.php中使用的PHP代碼嗎? – razzed 2009-09-18 00:38:43

+3

順便說一下,split()已被棄用,並將在6.0中被刪除。 – GZipp 2009-09-18 03:12:48

+0

我現在也試過兩個不同的服務器,在巴黎和美國,所以它不是一個有限的IP ... – 2009-09-18 11:47:54

回答

4

這兩個工作正常我用CURLOPT_USERAGENT添加用戶代理字符串。

// e.g. 
$agent = 'Mozilla/5.0 (X11; U; Linux i686; pl-PL; rv:1.9.0.2) Gecko/20121223 Ubuntu/9.25 (jaunty) Firefox/3.8'; 
curl_setopt($ch, CURLOPT_USERAGENT, $agent); 
+0

你能告訴我你正在使用的所有捲曲選項,也許我正在做我的當前選項的錯誤。 也感謝分裂()信息,不知道這一點。我將使用explode()來代替。 – 2009-09-18 11:38:24

+0

我將這兩行添加到了你的函數中,我看到你現在已經完成了。 – GZipp 2009-09-18 12:29:20