...

View Full Version : Error when using "File" and "FOpen"



MRMAN
09-01-2006, 03:25 PM
Hello,

I have made a simple script that will go to a web site scrape the lottto results off and then create a web page with them on.

I have permision from the owners of the site so i know they havn't blocked my ip address.

But every time i run the script i get this error

Warning: file(): php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution in /home/jueaprnv/public_html/numbers/index.php on line 8

Warning: file(http://www.winonnumbers.com/T2_3.php?YEAR=2006): failed to open stream: Connection timed out in

this is the script



<?
$year = $_GET["year"];
if(!$year)
{
$year = date("Y");
}

$lines = file("http://www.winonnumbers.com/T2_3.php?YEAR=".$year);
$filename = "results-".$year.".htm";
//$filename = "results.htm";

if (file_exists($filename))
{
$outputfile = fopen ($filename, "w");
}
else
{
$outputfile = fopen ($filename, "a");
chmod($filename, 0777);
}

$drawdate = '<td width="225" class="purpleTextBold">';
$draw = '<td width="70" class="purpleTextBold">';
$number = '<td width="32" align="center" class="purpleTextBold">';
$bonus = '<td width="60" align="center" class="purpleTextBold">';

$topline = 0;

$outputstring = "";
foreach ($lines as $line_num => $line)
{
if(strstr($line,"<html>"))
{
$outputstring .="<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">";
$outputstring .="<html>";
$outputstring .="<head>";
$outputstring .="<title>Irish Lottery Results</title>";
$outputstring .="<style media=\"all\" type=\"text/css\">\n td { font-size:12px; font-family:Verdana, Arial, Helvetica, sans-serif; }\n.hd {background-color:#000055; color:#ffffff; padding:4px 0 3px 0; font-weight:bold;}\n.cn {background-color:#F0F4FF; color:#000000; padding:4px 2px 3px 6px;}\n.dn {background-color:#E0E4EE; color:#000000; padding:4px 2px 3px 6px;}\n</style>";
$outputstring .="</head>\n<body>";
}
if(strstr($line,$drawdate))
{
$drawnum = 0;
$topline = 1;
$stringpos = strpos($line,$drawdate) + strlen($drawdate);
if($stringpos != strlen($drawdate)) //draw date
{
$printline = substr($line, $stringpos);
$printline = substr($printline, 0 ,strlen($printline) - 7);

$outputstring .= "<table width=\"380\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n";
$outputstring .= "<tr class=\"hd\">\n";
$outputstring .= "<td width=\"70\" nowrap>Draw Date</td>\n";
$outputstring .= "<td width=\"5\">&nbsp;</td>\n";
$outputstring .= "<td width=\"225\" colspan=\"12\" nowrap align=\"center\">". $printline ."</td>\n";
$outputstring .= "<td width=\"60\" align=\"center\">Bonus</td>\n";
$outputstring .= "</tr>\n";
}
}
if(strstr($line,$draw))
{
$ball = 0;
$stringpos = strpos($line,$draw) + strlen($draw);
if($stringpos != strlen($draw)) //draw date
{
$printline = substr($line, $stringpos);
$printline = substr($printline, 0 ,strlen($printline) - 7);
$outputstring .= "<tr";
if($drawnum == 0 || $drawnum == 2)
{
$outputstring .= " class=\"dn\"";
}
else
{
$outputstring .= " class=\"cn\"";
}
$outputstring .= ">\n<td width=\"70\">" .$printline."</td>";
$outputstring .= "<td width=\"5\">&nbsp;</td>";
}
$drawnum++;
$topline = 0;

}
if(strstr($line,$number))
{

$stringpos = strpos($line,$number) + strlen($number);
if($stringpos != strlen($number)) //draw date
{
$printline = substr($line, $stringpos);
$printline = substr($printline, 0 ,strlen($printline) - 7);
$outputstring .= "<td width=\"32\" align=\"center\">". $printline ."</td><td width=\"5\">&nbsp;</td>";
$ball++;
$topline = 0;
}

}
if(strstr($line,$bonus) && $topline == 0)
{

$stringpos = strpos($line,$bonus) + strlen($bonus);
if($stringpos != strlen($bonus)) //draw date
{
$printline = substr($line, $stringpos);
$printline = substr($printline, 0 ,strlen($printline) - 7);
$outputstring .= "<td width=\"60\" align=\"center\">". $printline."</td>";
$ball ++;
}

}

if($drawnum == 3 && $ball == 7)
{
$outputstring .= "</tr>\n</table>\n<br>\n";
$drawnum = 0;
$ball = 0;
}
if($ball == 7)
{
$outputstring .= "</tr>\n";
$ball = 0;
}
if(strstr($line,"</html>"))
{
$outputstring .= "</body>\n</html>";
}
}
if($outputstring != "")
{
fputs($outputfile,$outputstring);
}

fclose($outputfile);
?>



Now i would jsut think it my code. But it has worked for about 6 months and then suddenly stopped yesterday. I have sent an email to the host of my site to see iff they have change any settings.

Any ideas what it could be or a different way of doing it.

www.betfredcontent.com/numbers/index.php

chump2877
09-01-2006, 04:22 PM
I'm not sure exactly what is causing that error, but why not use file_get_contents (http://us3.php.net/manual/en/function.file-get-contents.php) instead, to do the same thing as this:


$lines = file("http://www.winonnumbers.com/T2_3.php?YEAR=".$year);

and using file_get_contents():


$content = file_get_contents("http://www.winonnumbers.com/T2_3.php?YEAR=".$year);
$lines = explode("\r\n",$content);
You may need to experiment with the string separator -- depending on the server, a line break could either be \n, \r, or \r\n....

And if you need the line break at the end of each line, you could do a preg_split (http://us3.php.net/manual/en/function.preg-split.php) instead (I think this will work):


$content = file_get_contents("http://www.winonnumbers.com/T2_3.php?YEAR=".$year);
$lines = preg_split("/\r\n/",$content,-1,PREG_SPLIT_DELIM_CAPTURE);
otherwise you can simply do something like this:


$content = file_get_contents("http://www.winonnumbers.com/T2_3.php?YEAR=".$year);
$lines = explode("\r\n",$content);
foreach ($lines as $key=>$val)
{
$lines[$key] = $val ."\r\n";
}
good luck!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum