2016-05-11 76 views
0
$ch = curl_init();  
$varpost = '&res=1'; // Initiate cURL 
$url = 'http://www.testxcvt.com/';// is 404 page 
curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $varpost); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$output = curl_exec ($ch); // Execute 
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
if(curl_errno($ch)) 
    { 
     echo 'error:' . curl_error($ch); 
    } 
curl_close ($ch); // Close cURL handle 

PFA代碼,我有。當我顯示$代碼時,我得到200。在我的瀏覽器中,我收到了404頁的廣告。我不確定什麼是共享404頁的200。curl返回200找不到頁面

請幫忙

+0

是http://www.testxcvt.com/您試圖獲得的實際頁面?因爲我沒有看到任何404頁面。該頁面不存在或尚未激活。 – jakob

+0

沒有它的一個randon地址我試過了,它給了我404 –

回答

0

如果您在下面嘗試下面的代碼,您會看到curl使得發佈到谷歌和谷歌響應405錯誤,所以404檢查是錯誤的。然後在頁面輸出中檢查403並處理。

在您的情況下,頁面不會返回405 http代碼,但會返回200,因此您應該檢查頁面輸出並設置正確的錯誤消息以查找。
唯一的問題是如果該錯誤消息發生在您實際需要的頁面某處,那麼您也不會得到它們。

<?php 
$ch = curl_init();  
$varpost = '&res=1'; // Initiate cURL 
$url = 'http://www.google.com/';// is 404 page 
curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $varpost); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$output = curl_exec ($ch); // Execute 
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
if($code == 404){ 
    /* handle real 404 here. */ 
    echo 'real 404 found'; 
}else{ 
    // here you can set your own error, for example 'Page Not Found (12)' 
    if (strpos($output, '405') !== false) { 
     // 404 page found 
     echo 'Google custom error 405 found'; 
     print_r($output); 
    }else{ 
     // no 404 error 
     echo 'Google custom error not found'; 
     print_r($output); 
    } 
} 
curl_close ($ch); // Close cURL handle 
?> 
+0

是的......但我需要在我的代碼中處理404個頁面。在404頁的格蘭源代碼我看到網頁未找到如下: 頁面未找到(12) <體ID = '型主體'> <腳本類型= '文本/ JavaScript的' src ='http://midascdn.nervesis.com/spring/js/nxd.js?oyenl%60ho = vvv%2fudruybwu%2fbn&nshfho = vvv%2fudruybwu%2fbn&sdgdsds ='> –

+0

好的,我不理解你..看到更新的代碼..是那個在你需要? – jakob

+0

我想我現在明白了。 Curl會返回200,因爲實際上可以找到頁面,但在該頁面上您有404個信息。所以,正如我所看到的那樣,您應該通過檢查'$ output'來執行錯誤處理。 – jakob

0

你從哪裏執行代碼?

當我嘗試在本地,我得到:

error:Could not resolve host: www.testxcvt.com 

而且$code回來爲0

如果您嘗試其他網址,例如http://www.text.com - 是否有效?

+0

我把這個在一個php文件中。然而www.text.com是一個有效的坐:) –

+0

基本上爲有效和404站點地址我得到200作爲結果 –

+0

你可能嘗試[Guzzle](https://guzzle.readthedocs.org/)和看看你得到了什麼狀態碼? –