douzi7219 2016-05-11 02:30
浏览 63
已采纳

curl返回200,找不到页面

$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. i get 200 when i display $code. in my browser i get an advertisement for 404 pages. i am not sure what is the reson to get 200 for 404 page.

Please help

  • 写回答

2条回答 默认 最新

  • duanqun9740 2016-05-11 02:53
    关注

    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. Then page output is checked for 403 in it and handled.

    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.
    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 
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?