2012-04-24 161 views
0

我需要從sitemap.xml文件獲取頁面URL的http響應代碼。當我通過cron進程獲取響應代碼時,它返回403(稱爲訪問被禁止:雖然我可以從瀏覽器訪問傳遞的URL)。curl請求返回錯誤的響應代碼

但是,如果我從本地主機運行相同的代碼,它將返回正確的http響應代碼(即200)。

爲什麼從本地主機和服務器返回不同的http響應代碼的區別?如何解決問題?

提取http響應碼的代碼如下。

function check_response_code() { 
    $pageurl='http://www.certona.com/online-merchandising/'; 
    $trimurl = ''; 
    $start = ''; 
    $end = ''; 
    $total = ''; 

    $start = microtime(true); 
    $response_code = ''; 
    if (!stristr($pageurl, "http://")) 
    { 
     if (!stristr($pageurl, "https://")) 
     { 
      $trimurl = "http://" . $pageurl; 
     } else 
     { 
      $trimurl = $pageurl; 
     } 
    } else 
    { 
     $trimurl = $pageurl; 
    } 
    $curl = curl_init(); 
    //don't fetch the actual page, you only want headers 

    curl_setopt($curl, CURLOPT_URL, $trimurl); 
    curl_setopt($curl, CURLOPT_NOBODY, true); 
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); 
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($curl, CURLOPT_FILETIME, true); 

    $result = curl_exec($curl); 

    $timestamp = curl_getinfo($curl, CURLINFO_FILETIME); 
    $response_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); 
    $mime_type = curl_getinfo($curl, CURLINFO_CONTENT_TYPE); 
    $end = microtime(true); 
    $total = round($end - $start, 5); 

    if ($timestamp != -1) 
    { //otherwise unknown 
     $arr=array(date("Y-m-d H:i:s", $timestamp), $response_code, $total, $mime_type); //etc 
    } else 
    { 
     $arr=array("", $response_code, $total, $mime_type); 
    } 
    echo "<pre>"; 
    print_r($arr); 
    echo "</pre>"; 
} 

謝謝。

回答

0

我不知道,但你的代碼似乎好工作

嘗試

check_response_code(); 

function check_response_code() { 
    $pageurl='http://www.certona.com/online-merchandising/'; 
    $curl = curl_init($pageurl); 
    curl_setopt($curl, CURLOPT_NOBODY, true); 
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); 
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($curl, CURLOPT_FILETIME, true); 

    $result = curl_exec($curl); 
    $info = curl_getinfo($curl); 
    $info['filetime'] = date("Y-m-d H:i:s", $info['filetime']); 
    echo "<pre>"; 
    print_r($info); 
    echo "</pre>"; 
} 

輸出

Array 
(
    [url] => http://www.certona.com/online-merchandising/ 
    [content_type] => text/html; charset=utf-8 
    [http_code] => 200 
    [header_size] => 488 
    [request_size] => 76 
    [filetime] => 2012-04-24 15:11:28 
    [ssl_verify_result] => 0 
    [redirect_count] => 0 
    [total_time] => 1.342 
    [namelookup_time] => 0 
    [connect_time] => 0.25 
    [pretransfer_time] => 0.25 
    [size_upload] => 0 
    [size_download] => 0 
    [speed_download] => 0 
    [speed_upload] => 0 
    [download_content_length] => 0 
    [upload_content_length] => 0 
    [starttransfer_time] => 1.342 
    [redirect_time] => 0 
    [certinfo] => Array 
     (
     ) 

    [redirect_url] => 
) 
+0

hello ...這是本地主機的輸出。但是當我從服務器上的cron進程嘗試相同的代碼時,它會返回不同的http響應代碼(即403而不是200)! – nir 2012-04-25 07:03:24

0

有可以由許多原因...

是不是你自己的服務器? =>http://codewithdesign.com/2011/05/26/curl-403-error-returning/

也許設置CURLOPT_USERAGENT爲 「Mozilla的/ 5.0(窗口; U; Windows NT的5.1; EN-US; rv中:1.7.5)的Gecko/20041107火狐/ 1.0」

或者閱讀本curl gives 403 error?

+0

CURLOPT_USERAGENT是一些什麼幫助時,我請求之間保持睡眠(10) ..但如果我不使用睡眠(10),一段時間後我會得到403響應代碼。 – nir 2012-04-25 10:54:25

+0

也許如果你問服務器加快一些反DoS工具可能會阻止你... – PiTheNumber 2012-04-25 14:12:07

0

你的localhost通過你的計算機運行curl。這就像你的瀏覽器打開你的IP地址和東西的網站。

服務器以另一種方式執行此操作。

我記得有一次,我通過刪除URL中尾隨的/解決了一個很常見的問題。

嘗試運行代碼

$pageurl = rtrim('http://www.certona.com/online-merchandising/', '/)'; 

但basicly我不認爲你的允許從其他網站獲取的目錄中的數據。
不應該以.xml結束網址以獲取站點地圖嗎?

$pageurl = 'http://www.certona.com/sitemap.xml'; 
+0

嗨.. sitemap.xml包含一個網站的鏈接網址。這裏的頁面URL是來自sitemap.xml的鏈接URL之一。謝謝你的回覆。 – nir 2012-04-25 07:00:04

+0

你試過刪除結尾'/'嗎? – 2012-04-25 07:02:47

+0

不會..會在一段時間內做到這一點.. – nir 2012-04-25 08:15:19