$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 页的广告。 我不确定为 200 页获得 404 的 Reson。
请帮忙
如果你在下面尝试这段代码,你会看到 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
?>
你从哪里执行代码?
当我在本地尝试时,我得到:
error:Could not resolve host: www.testxcvt.com
$code
以0
的身份回来了.
如果您尝试其他网址,例如http://www.text.com
- 它是否有效?