2010-09-14 87 views
0

一個頁面時,這是我的代碼:403 Forbidden錯誤掌握PHP捲曲

 
$url = 'http://www.douban.com/'; 

$url = str_replace(" ", "%20", $url); 
$TheURL_header = substr($url, 0, 7); 
if ($TheURL_header == "http://") { 
    $pos = strpos($url, "/", 7); 
    if ($pos) { 
     $host = substr($url, 7, $pos - 7); 
    } else { 
     $host = substr($url, 7); 
    } 
    $referer = "http://" . $host . "/"; 
} else if ($TheURL_header == "https:/") { 
    $pos = strpos($url, "/", 8); 
    if ($pos) { 
     $host = substr($url, 8, $pos - 8); 
    } else { 
     $host = substr($url, 8); 
    } 
    $referer = "https://" . $host . "/"; 
} else { 
    $pos = strpos($url, "/"); 
    if ($pos) { 
     $host = substr($url, 0, $pos); 
    } else { 
     $host = substr($url, 0); 
    } 
    $url = "http://" . $url; 
    $referer = "http://" . $host . "/"; 
} 

$c = curl_init(); 
$curl_header = array(
    'Accept: */*', 
    'Referer: ' . $referer, 
    'User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.2) Gecko/20090803 Ubuntu/9.04 (jaunty) Shiretoko/3.5.2', 
    'Host: ' . $host, 
    'Connection: Keep-Alive'); 
curl_setopt($c, CURLOPT_URL, $url); 
curl_setopt($c, CURLOPT_CUSTOMREQUEST, 'GET'); 
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($c, CURLOPT_HTTPHEADER, $curl_header); 
curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 30); 
curl_setopt($c, CURLOPT_TIMEOUT, 30); 
curl_setopt($c, CURLOPT_HEADER, 0); 

$res = curl_exec($c); 

echo $res; 

它工作正常,當我設置$ URL =「http://www.google.com」,但如果我改變$網址,如www。 aoguejewellery.com和其他一些網址,我總是得到一個403錯誤。

+2

'403'不是一個錯誤的請求錯誤,而是一個禁止的錯誤。錯誤請求的狀態碼是'400'。 – BoltClock 2010-09-14 04:02:01

+0

謝謝,我已更新。 – yesmeck 2010-09-14 04:31:49

+0

該網站可能使用cookie來跟蹤會話。我沒有把它作爲答案,因爲我有更多使用java抓取網頁的經驗。但我有類似的問題,我不得不使用Apache HttpClient Java庫來完全模擬瀏覽器並處理cookie管理。只是要看看。 – 2010-09-14 04:37:49

回答

0

嘗試刪除curl_setopt($c, CURLOPT_CUSTOMREQUEST, 'GET');,然後重試。