[英]curl returns 200 for page not found
$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我有的代碼。 當我顯示$ code時,我得到200。 在我的瀏覽器中,我得到一個404頁的廣告。 我不知道什么是得到404頁的200。
請幫忙
您從哪里執行代碼?
在本地嘗試時,我得到:
error:Could not resolve host: www.testxcvt.com
$code
返回為0
。
如果您嘗試使用其他URL,例如http://www.text.com
,它可以工作嗎?
如果您在下面嘗試使用此代碼,則會看到curl將帖子發布到google,並且google響應為405錯誤,因此404 check為false。 然后在頁面輸出中檢查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
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.