【问题标题】:How to suppress error in get_headers() without using @如何在不使用 @ 的情况下抑制 get_headers() 中的错误
【发布时间】:2017-05-24 14:54:12
【问题描述】:

get_headers() 如果正在检查的 URL 无效,则会引发警告。例如,

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

警告:get_headers(): php_network_getaddresses: getaddrinfo failed: No such host is known

不使用@ 是否可以抑制此错误?

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

【问题讨论】:

  • 您可以使用 error_reporting(E_ERROR | E_PARSE); 禁用 php 警告或错误报告(0);所以你不需要使用@符号。并与 curl 一起使用,您可以执行 curl_getinfo($curl, CURLINFO_HTTP_CODE);
  • curlCURLOPT_HEADERCURLOPT_RETURNTRANSFER 选项一起使用。 (也许CURLOPT_NOBODY 取决于你想要什么。)

标签: php


【解决方案1】:

您可以使用 curl 进行检查,它不会返回任何警告。如果您使用“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";
}

【讨论】:

【解决方案2】:

我假设您希望以不干扰您的 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() 调用和处理程序代码本身中总会有额外的东西需要微调。这是 Guzzle 的另一个使用匿名函数的示例:

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();

【讨论】:

    【解决方案3】:

    在函数之前更改错误报告的级别,在函数之后恢复它。

    // 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 行有关的两条错误消息。 它还将在下面输出“完成”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-23
      • 2013-02-12
      • 1970-01-01
      • 1970-01-01
      • 2021-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多