2017-05-24 39 views
0
的情况下抑制get_headers()中的错误

get_headers()如果要检查的URL无效,则会发出警告。例如,如何在不使用@

get_headers('http://nonexistingrubbish-url.com');

警告:get_headers():php_network_getaddresses:的getaddrinfo失败:没有这样的主机被称为

是否有可能制止这种错误withou使用@

我的主要目标是检查URL是否存在,但我不想使用@抑制器。

+0

您可以通过使用error_reporting(E_ERROR | E_PARSE)来禁用php警告;或error_reporting(0);所以你不需要使用@符号。并使用curl你可以做一些像curl_getinfo($ curl,CURLINFO_HTTP_CODE); –

+0

使用[curl](http://php.net/manual/en/book.curl.php)和'CURLOPT_HEADER'和'CURLOPT_RETURNTRANSFER'选项。 (也许'CURLOPT_NOBODY'取决于你想要什么。) –

+0

看看https://stackoverflow.com/questions/1241728/can-i-try-catch-a-warning – vzwick

回答

2

您可以使用卷曲进行检查,但不会返回任何警告。如果你使用'CURLOPT_NOBODY',那么它不会尝试下载整个页面。

<?php 
$url = "http://nonexistingrubbish-url.com"; 
$curl = curl_init($url); 
curl_setopt($curl, CURLOPT_NOBODY, true); 
$result = curl_exec($curl); 
if ($result !== false) { 
    $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); 
    if ($statusCode == 404) { 
     echo "URL Not Exists"; 
    } else { 
     echo "URL Exists"; 
    } 
} else { 
    echo "URL not Exists"; 
} 
+0

海事组织这是最好的办法。 – ceejayoz

+0

也许它在最近的版本中发生了变化,但[Curl显然也会触发警告](https:// stackoverflow。COM /问题/ 21984964 /卷曲错误警告 - 卷曲的exec-45是 - 不是一个有效卷曲手柄资源)。 –

1

我假设你想在这样不符合你的error_reportinglog_errors指令干涉的方式处理。我能想到的唯一办法是编写一个custom error handler。下面是来自PHPMailer的图书馆的例子:

Error handler

/** 
* Reports an error number and string. 
* 
* @param int $errno The error number returned by PHP 
* @param string $errmsg The error message returned by PHP 
* @param string $errfile The file the error occurred in 
* @param int $errline The line number the error occurred on 
*/ 
protected function errorHandler($errno, $errmsg, $errfile = '', $errline = 0) 
{ 
    $notice = 'Connection failed.'; 
    $this->setError(
     $notice, 
     $errmsg, 
     (string) $errno 
    ); 
    $this->edebug(
     "$notice Error #$errno: $errmsg [$errfile line $errline]", 
     self::DEBUG_CONNECTION 
    ); 
} 

Usage:

// Begin encrypted connection 
set_error_handler([$this, 'errorHandler']); 
$crypto_ok = stream_socket_enable_crypto(
    $this->smtp_conn, 
    true, 
    $crypto_method 
); 
restore_error_handler(); 

如有必要,总是有更多的东西微调无论是在set_error_handler()调用和处理程序代码本身。下面是一个使用匿名函数从狂饮另一个例子:

Error handler and Usage:

$errors = null; 
set_error_handler(function ($_, $msg, $file, $line) use (&$errors) { 
    $errors[] = [ 
     'message' => $msg, 
     'file' => $file, 
     'line' => $line 
    ]; 
    return true; 
}); 
$resource = $callback(); 
restore_error_handler(); 
-1

更改错误报告的级别的功能之前,该功能后恢复它。

// Show all errors except warnings. 
error_reporting(E_ALL & ~E_WARNING); 
get_headers('http://nonexistingrubbish-url.com'); 
// revert to the above error reporting level. 
error_reporting(E_ALL); 

为什么这个得到-1?此代码适用于[在PHP 5.6.3上]。尝试一下。

代码示例(复制,上传和享受):

ini_set("display_errors",1); 
error_reporting(E_ALL & ~E_WARNING); 
get_headers('http://nonexistingrubbish-url.com'); 
error_reporting(E_ALL); 
get_headers('http://nonexistingrubbish-url.com'); 
print "<br><Br>done!"; 

这段代码将只输出两个错误消息都与5号线 还将输出“完成”,以下。