1

I am trying to call an URL with PHP to check if it exists and is reachable.

My initial code was

fopen('http://'.$this -> url, 'r');

But it throws the following errors every time an URL is unreachable:

fopen(http://dwzegdzgwedzgew.com): failed to open stream: php_network_getaddresses: getaddrinfo failed
php_network_getaddresses: getaddrinfo failed:

The error operator (@) is ignored in this case as the error isn't thrown by fopen itself but while resolving the asdress. So I thought that should do it:

 @fopen(@'http://'.$this -> url, 'r');

but it also goes on throwing the error.

Is there a non-error-throwing possibility to check if an URL exists within PHP before opening it?

1 Answer 1

1

What is the error message with $ressource = @fopen('http://' . $this->url, 'r'); ?

<?php


$urls = array('kihgkighuhgkig.li', 'google.com', 'adsfafdf.fr');
foreach ($urls as $url)
{
    if (gethostbyname($url) != $url)
    {
        $ressource = fopen('http://' . $url, 'r');
        var_dump($url, $ressource);
    }
}

The output is :

$> php test.php
string(10) "google.com"
resource(6) of type (stream)
4
  • fopen(kihgkighuhgkig.li): failed to open stream: php_network_getaddresses: getaddrinfo failed: Der angegebene Host ist unbekannt. Edit: Sorry for german, "unknwon host"
    – Daniele D
    Commented Oct 15, 2014 at 9:02
  • You can try to ping the request before using fopen. But if it's in production, hide errors error_reporting(0); Commented Oct 15, 2014 at 9:09
  • I am not able to hide errors for the whole project as the project has it's own errorhandling. Also to ping in PHP you need to create a socket, which would throw exactly the same error and have the issues stated above in the OP when trying to resolve the host.
    – Daniele D
    Commented Oct 15, 2014 at 9:14
  • I've edit my previous answer, for adding some code, did it works? Commented Oct 15, 2014 at 9:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.