Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.
Not a member yet? Click here to register.
Forgot Password?

Anyone know how to fix file_get_contents Error?

Asked Modified Viewed 2,509 times
C
Craig
C
Craig 14
  • Fusioneer, joined since
  • Contributed 4,462 posts on the community forums.
  • Started 212 threads in the forums
  • Started this discussions
asked
Fusioneer

Hi,

If using file_get_contents to get the status of a site or whatever and if you have entered a url that does not exist or down it will fail and produce the error...

Quote

file_get_contents() [<a href='function.file-get-contents'>function.file-get-contents</a>]: php_network_getaddresses: getaddrinfo failed: Name or service not known


So example,
I have entered a url that does not exist like....

http://www.php-fangree.co.uk


So this....

<?php
   
$url = "http://www.php-fangree.co.uk";


$contents = @file_get_contents($url);


if ($contents){
echo "<span style='color: green;'> OK</span> ";
} else {
echo "<span style='color: red;'> NOT OK</span> ";
}
       

?>


Produces the error but the point is to see if the site is on-line or not so if the site is going to be off-line or does not exist then what's the point if it is going to throw this error every time the wrong URL is entered or every time a site is down.

Now as you see in the code I have added @ before file_get_contents to suppress the error, however this will still produce the error in the PHPFusion log.

Does anyone know what I can do so it will NOT produce the error if a site is not on-line or wrong URL or can anyone show me a better way or different function to do it that will not produce errors if things are down?

Thanks
0 replies

4 posts

H
Homdax
H
Homdax 10
  • Fusioneer, joined since
  • Contributed 2,246 posts on the community forums.
  • Started 108 threads in the forums
answered
Fusioneer

There is some kind of script checking sites on the page with the NSS list on this site. Could it be related?
0 replies
C
Craig
C
Craig 14
  • Fusioneer, joined since
  • Contributed 4,462 posts on the community forums.
  • Started 212 threads in the forums
  • Started this discussions
answered
Fusioneer

The License Violations DB on next does it but I am not sure how that handles errors if the site is down or does not exist.
0 replies
D
DrunkeN
D
  • Member, joined since
  • Contributed 161 posts on the community forums.
  • Started 18 threads in the forums
answered
Member

If you want check if site is available then you can use this function
   function check_url($url) {
           if(!filter_var($url, FILTER_VALIDATE_URL)) {
              return "<span style='color: orange; font-size: 14px; font-weight: bold;'>URL provided wasn't valid</span>";
           }
           $ul = curl_init($url);
           curl_setopt($ul, CURLOPT_CONNECTTIMEOUT, 10);
           curl_setopt($ul, CURLOPT_HEADER, true);
           curl_setopt($ul, CURLOPT_NOBODY, true);
           curl_setopt($ul, CURLOPT_RETURNTRANSFER, true);
           $resp = curl_exec($ul);
           curl_close($ul);
           if ($resp)
         return "<span style='color: green; font-size: 14px; font-weight: bold;'>Site seems to be up and running!</span>";
           return "<span style='color: red; font-size: 14px; font-weight: bold;'>Oops nothing found, the site is either offline or the domain doesn't exist</span>";
   }


Usage
   $resp = check_url("http://www.fangree.com");
   echo "<div class='resp'>".$resp."</div>";


http://www.fangree.com will give following message
Site seems to be up and running!

http://www.php-fangree.co.uk will give following message
Oops nothing found, the site is either offline or the domain doesn't exist

htp://www.php-fangree.co.uk will give following message
URL provided wasn't valid



I have this as a page on my site
http://www.dark-fusion.se/site_check.php
0 replies
S
spunk
S
spunk 10
www.duesseltag.de/../../images/banner/banner209x69.png
  • Member, joined since
  • Contributed 92 posts on the community forums.
  • Started 6 threads in the forums
answered
Member

Hey, nice function for weblinks.php :D
0 replies

Category Forum

General Discussion

Labels

None yet

Statistics

  • Views 0 views
  • Posts 4 posts
  • Votes 0 votes
  • Topic users 4 members

4 participants

H
H
Homdax 10
  • Fusioneer, joined since
  • Contributed 2,246 posts on the community forums.
  • Started 108 threads in the forums
C
C
Craig 14
  • Fusioneer, joined since
  • Contributed 4,462 posts on the community forums.
  • Started 212 threads in the forums
  • Started this discussions
D
D
  • Member, joined since
  • Contributed 161 posts on the community forums.
  • Started 18 threads in the forums
S
S
spunk 10
www.duesseltag.de/../../images/banner/banner209x69.png
  • Member, joined since
  • Contributed 92 posts on the community forums.
  • Started 6 threads in the forums

Notifications

Track thread

You are not receiving notifications from this thread.

Related Questions

Not yet