0

get_headers() throws a warning if the URL being checked is invalid. E.g.,

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

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

Is it possible to suppress this error withou using @?

My main goal is to check whether a URL exist or not but I don't want to use the @ suppressor.

IMB
  • 15,163
  • 19
  • 82
  • 140
  • you can disable php warning by using error_reporting(E_ERROR | E_PARSE); or error_reporting(0); so you don't need to use @ sign. and to use with curl you can do something like curl_getinfo($curl, CURLINFO_HTTP_CODE); – shivanshu patel May 24 '17 at 14:56
  • Use [curl](http://php.net/manual/en/book.curl.php) with the `CURLOPT_HEADER` and `CURLOPT_RETURNTRANSFER` options. (And maybe `CURLOPT_NOBODY` depending on what you want.) – Alex Howansky May 24 '17 at 14:57
  • Take a look at https://stackoverflow.com/questions/1241728/can-i-try-catch-a-warning – vzwick May 24 '17 at 15:16

3 Answers3

2

You can check with curl and it's not returning any warnings. If you use 'CURLOPT_NOBODY' then it will not try to download whole page.

<?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";
}
shivanshu patel
  • 782
  • 10
  • 19
1

I assume that you want it handled in such a way that doesn't interfere with your error_reporting and log_errors directives. The only way I can think of is writing a custom error handler. Here's an example from the PhpMailer library:

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

If necessary, there's always additional stuff to fine-tune either in the set_error_handler() call and the handler code itself. Here's another example from Guzzle that uses an anonymous function:

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();
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
0

Change the level of Error reporting before the function, reverting it after the function.

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

Why does this get a -1? This code works [on PHP 5.6.3] . Try it.

Code example (copy, upload and enjoy):

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!";

This code will output only two error messages both relating to line 5. It will also output "done", below.

Martin
  • 22,212
  • 11
  • 70
  • 132