php - curl返回200找不到页面

标签 php curl error-handling

$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。

请帮忙

最佳答案

如果您在下面尝试使用此代码,则会看到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 
?>

关于php - curl返回200找不到页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37152118/

相关文章:

php - AES crypt Delphi/PHP 损坏的输出

php - 努力用 php 显示 blob 图像

linux - 如何在 curl 上指定 IP 地址?

php - Curl 失败并出现 HTTP 错误 500

php - 无法在 Amazon EC2 上设置 cron-job

php - PHP中如何检查函数是否最后一次被调用

python - 如何安装 Rust 而不会在文件下载过程中出现超时错误?

python - 如何有意使具有错误打印输出的Python代码崩溃(“IntegrationWarning”)

vb6 - 如何处理VB6中的OCR扫描错误

r - plm问题,重复的时间ID错误