Daniele D
Daniele D

Reputation: 939

Preventing php_network_getaddresses: getaddrinfo failed:

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?

Upvotes: 1

Views: 1929

Answers (1)

GeoffreyHervet
GeoffreyHervet

Reputation: 288

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)

Upvotes: 1

Related Questions