2016-05-11 73 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/)和看看你得到了什么状态码? –