Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

file_get_contents .. display error message if no connect

Posted on 2004-08-13
8
Medium Priority
?
255 Views
Last Modified: 2008-03-06
i think this is an easy one, but I can't get it working.  im using something similar to below to grab data from other sites, what I want to do is in case that site is down or unreachable for some reason .. i want to display a nice error message like 'Data not available' or something like that ...

file_get_contents("http://website.html");

any help?  thanks
0
Comment
Question by:grizum
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
8 Comments
 
LVL 27

Expert Comment

by:Diablo84
ID: 11797926
you should be able to do something simple like

$gc = file_get_contents("http://website.html");
if (!$gc) {
 echo "data does not exist";
}
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11797934
file_get_contents returns false if it fails so you can use a simple if statement as shown above to check if it was successful or not, obviously !$gc means not $gc (or in other words if $gc returns false).
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11797946
you could always use alternative methods such as

if (!fopen("http://www.example.com/", "r") {
  echo "data does not exist";
}

or even a slightly more advanced mthod using fsockopen (http://www.php.net/manual/en/function.fsockopen.php) but the first suggestion should work fine.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:grizum
ID: 11797970
thanks for the response.

I tried that and, while it works ... it still dispalys the 'Connection refused ' error if the source is not available.  here is my code below

<?php

$zzz = file_get_contents("http://www5.playnet.com/bv/wwiiol/axis_playnow.jsp");
$zzz = str_replace("<table width=\"100%\" border=\"0\" cellspacing=\"0\">","",$zzz);
$zzz = str_replace("<font face=\"Arial, Helvetica, sans-serif\" size=\"1\">","",$zzz);
$zzz = str_replace("</font>","",$zzz);

$patterns[0] = "/<b>/";
$replacements[0] = "&middot;&nbsp";

$begin = "<!--Axis RDP Archive -->";
$end = "<td align=\"center\" valign=\"middle\"><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">";

$zzz = preg_replace("'<html[^>]*?>.*?$begin'si", $end, $zzz);
$zzz = preg_replace("'<\/table[^>]*?>.*?<\/html>'si", '', $zzz);
$zzz = preg_replace($patterns, $replacements, $zzz);

?>

<html>
<head>
<title>Allied RDP Brief</title>
<link rel="stylesheet" href="stats.css" type="text/css">
</head>
<body bgcolor="#CCCCCC" text="#000000" topmargin="0" bottommargin="0" leftmargin="0" marginwidth="0" marginheight="0">
<table width="350" class="text1" bgcolor="#CCCCCC"><tr><td class="text1" valign="middle" align="left" bgcolor="#CCCCCC"><u><b><em>Axis RDP Brief</em></b></u></td></tr>
<?php if (!$zzz) {
 echo "Data not available";
} else {
?>
<list>
<?php echo "$zzz";
}
?>

</td></tr></table>
</body>
</html>

0
 
LVL 27

Accepted Solution

by:
Diablo84 earned 800 total points
ID: 11797971
i presume you mean you just want to hide the error (which is as much as you can do)

just add the @ operator in front of the function to supress the error
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11797972
ie.

$zzz = @file_get_contents("http://www5.playnet.com/bv/wwiiol/axis_playnow.jsp");
0
 

Author Comment

by:grizum
ID: 11797974
thanks u the man!  
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11797977
no problem :)

|)iablo
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question