grizum
asked on
file_get_contents .. display error message if no connect
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
file_get_contents("http://website.html");
any help? thanks
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).
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.
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.
ASKER
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\">","",$zz z);
$zzz = str_replace("<font face=\"Arial, Helvetica, sans-serif\" size=\"1\">","",$zzz);
$zzz = str_replace("</font>","",$ zzz);
$patterns[0] = "/<b>/";
$replacements[0] = "· ";
$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><e m>Axis RDP Brief</em></b></u></td></t r>
<?php if (!$zzz) {
echo "Data not available";
} else {
?>
<list>
<?php echo "$zzz";
}
?>
</td></tr></table>
</body>
</html>
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\">","",$zz
$zzz = str_replace("<font face=\"Arial, Helvetica, sans-serif\" size=\"1\">","",$zzz);
$zzz = str_replace("</font>","",$
$patterns[0] = "/<b>/";
$replacements[0] = "· ";
$begin = "<!--Axis RDP Archive -->";
$end = "<td align=\"center\" valign=\"middle\"><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">";
$zzz = preg_replace("'<html[^>]*?
$zzz = preg_replace("'<\/table[^>
$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><e
<?php if (!$zzz) {
echo "Data not available";
} else {
?>
<list>
<?php echo "$zzz";
}
?>
</td></tr></table>
</body>
</html>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks u the man!
no problem :)
|)iablo
|)iablo
$gc = file_get_contents("http://website.html");
if (!$gc) {
echo "data does not exist";
}