1

While usign the function get_headers() for example, If i wrote

get_headers("www.websitedoesntexist.com");

i get the following error

Warning: get_headers(): php_network_getaddresses: getaddrinfo failed: No such host is known Warning: get_headers(www.websitedoesntexist.com): failed to open stream: php_network_getaddresses: getaddrinfo failed: No such host is known

I want to know how to handle these problems, Something like

if (isset(get_headers("www.websitedoesntexist.com"))) {
    echo "URL Exists";
}
Pankaj Makwana
  • 2,964
  • 6
  • 28
  • 45
AXAI
  • 656
  • 3
  • 15
  • Possible duplicate of [Best way to handle errors on a php page?](https://stackoverflow.com/questions/11894115/best-way-to-handle-errors-on-a-php-page) – WoogieNoogie Oct 09 '17 at 14:28
  • @WoogieNoogie So hiding the fatal problem is enough? – AXAI Oct 09 '17 at 14:33

3 Answers3

2

From the PHP manual :

Returns an indexed or associative array with the headers, or FALSE on failure.

Which means you can test against FALSE.

Read more : http://php.net/manual/en/function.get-headers.php

Edit

If you want just to check if the URL exists or not, instead of supressing the errors I suggest you use curl, a simple function like this can do the job :

function url_exists($url) {
    if (!$fp = curl_init($url)) {
        return false;
    } 
    return true;
}
teeyo
  • 3,155
  • 3
  • 20
  • 35
  • I've tried `if (get_headers("www.websitedoesntexist.com" != FALSE)` but still gives me the fatal problem. – AXAI Oct 09 '17 at 14:31
  • How about `empty` ? Like so : `if (empty(get_headers("www.websitedoesntexist.com"))) { echo 'Oh man!'; }` – teeyo Oct 09 '17 at 14:35
  • Same, Gives the `fatal error`, The after condition `echo` works, But after the long `fatal error` report – AXAI Oct 09 '17 at 14:36
  • I tried it and it works for me, the only warning I get is because I'm not configuring my **php.ini** to `allow_url_fopen = On` ! – teeyo Oct 09 '17 at 14:43
  • my `allow_url_fopen` is `On` too, The problem i don't know a lot about `curl`, Would it use more Bandwidth then just supressing the error? Also what is `$fp` since it is undefined? – AXAI Oct 09 '17 at 14:52
  • It should be fast, here's another function that uses curl to retrieve the status code of the page (without the content), you can benchmark the two solutions yourself and test the response time :) : https://stackoverflow.com/questions/4607684/curl-and-ping-how-to-check-whether-a-website-is-either-up-or-down – teeyo Oct 09 '17 at 15:00
2

Suppress errors on the get_headers() function. Assign get_headers to a variable. Then check the Boolean value of that variable.

 $file_headers = @get_headers("www.websitedoesntexist.com");
 if(!$file_headers) {
     $exists = false;
 }
 else {
     $exists = true;
 }

Edit:

Based on the comments, for a better, long-term solution using curl see https://stackoverflow.com/a/36743670/4374801

Roger Creasy
  • 1,127
  • 13
  • 28
  • That solved it, I tried doing this before, but without the `@` at the beginning of the function, How did the `@` change everything exactly? – AXAI Oct 09 '17 at 14:38
  • It suppresses errors on the function. But, BE CAREFUL using it. You do want to make sure you handle error situations. It just gives you control over handling the errors. – Roger Creasy Oct 09 '17 at 14:41
  • 1
    You are just ignoring the warning and notices here, not solving the issue :) – teeyo Oct 09 '17 at 14:41
  • @teeyo The idea is that i check if the url exists or not, the error just tells me that the url doesn't exist, So i don't think it would matter if i ignore it or not, Since i only check the existence of the url, Or would it be? – AXAI Oct 09 '17 at 14:45
  • 1
    @AXAI imagine, for example, one day you have a problem with your outgoing connections. The function will fail and not necessarily because the URL failed, which will make debugging a bitch. It's always better to check what happened and handle accordingly. – ishegg Oct 09 '17 at 14:48
  • 1
    In this case why don't you use `curl` instead ? You know ignoring errors is a bad practice ! – teeyo Oct 09 '17 at 14:48
  • @AXAI the problem is that you could change the code in the future and cause errors that you are not anticipating right now. Read the answer at the link provided by ishegg above. – Roger Creasy Oct 09 '17 at 14:48
1

if you want to check the url exist or not in php, simply code this.testing purpose

$array = get_headers('url');
$string = $array[0];
    if (strpos($string, "200")) {
    echo "thats cool dude";
} else {
    echo "Bad request";
} 
Saad Ali
  • 11
  • 5