Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 用旋度检测错误_Php_Curl - Fatal编程技术网

Php 用旋度检测错误

Php 用旋度检测错误,php,curl,Php,Curl,我试图在PHP中使用cURL解析一个网站列表,但遇到了在返回响应时检测任何html错误的问题。根据php手册,下面的代码片段应该有助于检测任何错误。错误意味着页面对解析没有用处,因此在调用过程中,404或500之类的代码都是我的书中的错误: if(curl_error($c)) { print_r( 'error:' . curl_error($curl)); } 上面的代码确实适用于404个错误,但无法捕获500个错误,例如,可能有更多的错误无法捕获。我当然可以用这个: $httpC

我试图在PHP中使用cURL解析一个网站列表,但遇到了在返回响应时检测任何html错误的问题。根据php手册,下面的代码片段应该有助于检测任何错误。错误意味着页面对解析没有用处,因此在调用过程中,404或500之类的代码都是我的书中的错误:

if(curl_error($c)) {
    print_r( 'error:' . curl_error($curl));
}
上面的代码确实适用于404个错误,但无法捕获500个错误,例如,可能有更多的错误无法捕获。我当然可以用这个:

$httpCode = curl_getinfo ( $curl, CURLINFO_HTTP_CODE );
但是,如果结果是500、404或401等,我将不得不手动添加检查以评估变量,并忽略结果。如何简单准确地忽略对解析网站没有用处的响应?

如果$httpCode==200{
$wrong_http_codes = '^0|4.*|5.*$';
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if (preg_match("/$wrong_http_codes/", $http_code) === 1) {
    echo 'Error';
}