简体   繁体   English

curl返回200找不到页面

[英]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 the code that i have. PFA我有的代码。 i get 200 when i display $code. 当我显示$ code时,我得到200。 in my browser i get an advertisement for 404 pages. 在我的浏览器中,我得到一个404页的广告。 i am not sure what is the reson to get 200 for 404 page. 我不知道什么是得到404页的200。

Please help 请帮忙

Where are you executing the code from? 您从哪里执行代码?

When I try it locally, I get: 在本地尝试时,我得到:

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

And $code comes back as 0 . $code返回为0

If you try another URL, like http://www.text.com - does it work? 如果您尝试使用其他URL,例如http://www.text.com ,它可以工作吗?

If you try this code below you will see that curl makes post to google and google responds with 405 error so 404 check is false. 如果您在下面尝试使用此代码,则会看到curl将帖子发布到google,并且google响应为405错误,因此404 check为false。 Then page output is checked for 403 in it and handled. 然后在页面输出中检查403并进行处理。

In your case page doesn't return 405 http code but 200 so you should check page output and set correct error message to find. 在您的情况下,页面不会返回405 http代码,而返回200,因此您应该检查页面输出并设置正确的错误消息以进行查找。
Only problem is if that error message occurs somewhere on pages you actually need, then you won't get them also. 唯一的问题是,如果该错误消息出现在您实际需要的页面上某处,那么您也将不会得到它们。

<?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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM