Shoutbox

Question... - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: Question... (/showthread.php?tid=75805)

Question... by Eddie on 07-02-2007 at 12:25 PM

Hello guys, another web based question :)

With the code that collects a number from a certain website, is constantly failing when the connection that site isnt working...here is the code.

code:
$data = file_get_contents("http://www.habbo.com.au/habbo_count_xml.action") or die("Could not get data");
$usercount = preg_replace("/<usercount>([0-9]+)<\\/usercount>/", "\\1", $data);
echo $data;
Is there a way i can make it so that when the website it collects data from isnt working it just displays "Some" or something, because the "die blablabla" doesnt seem to do that *-)
RE: Question... by MeEtc on 07-02-2007 at 03:20 PM

first, you have to remove the die message. then, put if statements around any place that uses the contents of $data to see if it is null or empty


RE: Question... by user13774 on 07-02-2007 at 08:26 PM

What MeEtc means:

code:
$data = file_get_contents("http://www.habbo.com.au/habbo_count_xml.action");
if ($data)
{
$usercount = preg_replace("/<usercount>([0-9]+)<\\/usercount>/", "\\1", $data);
// and all the other stuff you want to do with $data
echo $data;
}
else
{
echo("Unable to retreive data");
}


RE: Question... by andrewdodd13 on 07-03-2007 at 10:01 AM

code:
$data = @file_get_contents("http://www.habbo.com.au/habbo_count_xml.action") or die("Could not get data");
$usercount = preg_replace("/<usercount>([0-9]+)<\\/usercount>/", "\\1", $data);
echo $data;
Shows the number of users.

code:
$data = @file_get_contents("http://www.habbo.com.au/habbo_count_xml.actionaa") or die("Could not get data");
$usercount = preg_replace("/<usercount>([0-9]+)<\\/usercount>/", "\\1", $data);
echo $data;
(Notice the aa at the end of the URL, bumming it up). This works as it should for me. (ie, Could not get data is sent to the browser).

Notice the @ in front of file_get_contents - this disables warnings. (Edit: For that function).

You could also do (as the last line):
echo ($data) ? $data : "No Data";
RE: Question... by Eddie on 07-04-2007 at 07:22 AM

quote:
Originally posted by Markus
What MeEtc means:

code:
$data = file_get_contents("http://www.habbo.com.au/habbo_count_xml.action");
if ($data)
{
$usercount = preg_replace("/<usercount>([0-9]+)<\\/usercount>/", "\\1", $data);
// and all the other stuff you want to do with $data
echo $data;
}
else
{
echo("Unable to retreive data");
}


Thanks Markus, will use that in the future :)
Thanks all :]