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