
webstar
Members-
Posts
26 -
Joined
-
Last visited
Everything posted by webstar
-
How can i Make this code below grab the 3rd line of the file I am feeding through it and make it the subject line of the email? I'm kinda php dumb and this is likely really simple. #!/usr/bin/php <?php # E-mail recipient(s). Separate each address with a comma and a space. $emailrecip = "REMOVED"; # E-mail subject line. $emailsubject = "REMOVED"; # E-mail From name. $emailfromname = "REMOVED"; # E-mail From address. $emailfromaddr = "REMOVED"; $fp = fopen('php://stdin','r'); $data = ""; while(!feof($fp)) { $data .= fgets($fp,4096); } fclose($fp); $timestamp = "REMOVED"; $timestamp .= date("D, j M Y G:i:s O "); $timestamp .= "\n"; $timestamp .= "Emailed to you at: %EMAILSTAMP%"; $timestamp .= "\n"; # Send the e-mail. $subject = $emailsubject; $recipient = $emailrecip; $body_of_email = $timestamp; $body_of_email .= trim($data); $header = 'From: "' . $emailfromname . '" <' . $emailfromaddr . '>'; $header .= "\n"; $header .= "X-Priority: 1"; $header .= "\n"; mail ($recipient, $subject, $body_of_email, $header); echo ("E-mail sent."); ?>
-
#!/usr/bin/php <?php # E-mail recipient(s). Separate each address with a comma and a space. $emailrecip = "removed"; # E-mail From name. $emailfromname = "removed"; # E-mail From address. $emailfromaddr = "removed"; $fp = fopen('php://stdin','r'); $data = ""; while(!feof($fp)) { $new_data = fgets($fp,4096); $data .= $new_data; $data_array[$i++]=$new_data; } fclose($fp); $patterns = array(); $patterns[0] = '/CWTO/'; $patterns[1] = '/CWWG/'; $patterns[2] = '/CWVR/'; $patterns[3] = '/CWHX/'; $patterns[4] = '/CWUL/'; $replacements = array(); $replacements[0] = '-ON-'; $replacements[1] = '-Prairies-'; $replacements[2] = '-BC-'; $replacements[3] = '-Maritimes-'; $replacements[4] = '-QC-'; $data_array = preg_replace($patterns, $replacements, $data_array); $split_line = explode(' ', $data_array[2]); $subject = $split_line[0].' '.$split_line[1].' '.trim($data_array[3]); # Send the e-mail. $recipient = $emailrecip; $body_of_email = trim($data); $header = 'From: "' . $emailfromname . '" <' . $emailfromaddr . '>'; $header .= "\n"; $header .= "X-Priority: 1"; $header .= "\n"; mail ($recipient, $subject, $body_of_email, $header); echo ("E-mail sent."); ?> I kinda went a different direction with it. The first 3 arrays work but the 4th one never seems to fire and I was wondering if anyone can tell me why.
-
If the data from the file is already passed to the php via this $fp = fopen('php://stdin','r'); $data = ""; while(!feof($fp)) { $data .= fgets($fp,4096); } fclose($fp); would file_get_contents still work on $data?
-
I'm trying to find the write coding to pull the First and Second words from the 2nd line and the entire 3rd line from this file and have them as $subject1 and $subject2 And I have no php background. http://www.weatherserver.net/text/CWTO/WOCN11.txt so $subject1 = WOCN11 CWTO and $subject2 = SPECIAL WEATHER STATEMENT
-
It should be sending email IF the file on my server does not match the file being pulled OR does not exist at all on my server. But it is as if it's reading a buffer or cache from someplace else. if I delete the file on my server and try to trigger the script it runs like the file is there.
-
This has me confused. I've posted the code here for easy reading <?php # ---------------------------------------------------------------------------------- # Configuration # Directory on the remote server. Be sure to include leading and trailing slashes! $remotedir = "/pub/data/raw/wt/"; # File on the remote server. No slashes! $remotefile = "wtnt31.knhc.tcp.at1.txt"; # Keyword to check for in response. $keyword = "$$"; # Remote server address. $remoteserver = "weather.noaa.gov"; # E-mail recipient(s). Separate each address with a comma and a space. $emailrecip = "REMOVED"; # E-mail subject line. $emailsubject = "[NHC] WTNT31 -"; # E-mail From name. $emailfromname = "REMOVED"; # E-mail From address. $emailfromaddr = "REMOVED"; $txtfilename = $remotefile; # End Configuration # ---------------------------------------------------------------------------------- # Format the page for easy viewing. Header( "Content-type: text/plain"); # Setup the request. $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://" . $remoteserver . $remotedir . $remotefile); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, 0); $res = curl_exec($ch); curl_close($ch); $result = array_map('trim',explode("\n",$res)); # Check for keyword. if (!in_array($keyword,$result)) die("Weather Coding not found!"); # Read the file containing the last response. $filename = $txtfilename . '.txt'; if (file_exists($filename)) { $contents = array_map('trim',file($filename)); # Check for changes. if (count(array_diff(array_unique($contents),array_unique($result))) == 0) { # There has not been a change. echo ("No Updates\r\n"); } else { # There has been a change. echo ("**UPDATES DETECTED**\r\n"); # Write the new file. echo implode("\n",$result); file_put_contents($filename,implode("\n",$result)); $subject = $emailsubject . $result[3]; # Send the e-mail. $recipient = $emailrecip; $body_of_email = implode("\n",$result); $header = 'From: "' . $emailfromname . '" <' . $emailfromaddr . '>'; $header .= "\n"; $header .= "X-Priority: 1"; $header .= "\n"; mail ($recipient, $subject, $body_of_email, $header); echo ("E-mail sent."); } } else { # Write the new file. echo implode("\n",$result); file_put_contents($filename,implode("\n",$result)); } ?> This is what it is doing and it's confusing me. If the text on the webserver changes the script runs normally and writes to the file and emails. If I delete the .txt from my server the script SHOULD run normally and send out an email and write a new file. What it is doing is writing is skipping the email part, and jumping right to this part. else { # Write the new file. echo implode("\n",$result); file_put_contents($filename,implode("\n",$result)); } It's as if it is reading from a buffer because it's not reading from the .txt file because I deleted it but writes the txt file back to the drive. What am i missing to get it to run normally? Windows 2008 64bit R2, IIS7 and PHP FastCGI 5.1.3
-
Thank you for your help. It works good.
-
Ok so the script above works kinda. The output to the screen is Array ( ) No Updates E-mail sent. And it does send the email the only issue is on each refresh it does it again. It isn't writing to the WTNT32.txt file so it has a record of the last email. So each refresh is a new email again.
-
Oh wait google was my friend. Curl Support. :-p
-
Wow, ok I wasn't expecting a rewrite but i'll take it. When I run the script it does this. <br /> <b>Fatal error</b>: Call to undefined function curl_init() in <b>C:\WEATHERSCRIPTS\NHCTest\WTNT32.php</b> on line <b>37</b><br />
-
Please please don't fight over me, I have enough questions for everyone. *sigh* I chased everyone away.
-
Please please don't fight over me, I have enough questions for everyone.
-
Pretty much I am trying to use the line that is being captured as the subject rather then the current subject. <? # ---------------------------------------------------------------------------------- # Configuration # Directory on the remote server. Be sure to include leading and trailing slashes! $remotedir = "/text/station/KNHC/"; # File on the remote server. No slashes! $remotefile = "WTNT32.KNHC"; # Keyword to check for in response. $keyword = "$$"; # Remote server address. $remoteserver = "twister.sbs.ohio-state.edu"; # E-mail recipient(s). Separate each address with a comma and a space. $emailrecip = "email address"; # E-mail subject line. $emailsubject = "[NHC] WTNT32 - Public Advisory"; # E-mail From name. $emailfromname = "WxServer NHC"; # E-mail From address. $emailfromaddr = "from address"; $txtfilename = "WTNT32"; # End Configuration # ---------------------------------------------------------------------------------- # Format the page for easy viewing. Header( "Content-type: text/plain"); # Setup the request. $header .= "GET " . $remotedir . $remotefile . " HTTP/1.0\r\n"; $header .= "Host: " . $remoteserver . "\r\n"; $header .= "User-Agent: Downloader\r\n\r\n"; $fp = fsockopen ($remoteserver, 80, $errno, $errstr, 30); # Do the request. fputs ($fp, $header . $req) or die("Can't access the site!"); while (!feof($fp)) { $res .= fgets ($fp, 128); } # Strip off the header info. $headerend = strpos($res,"\r\n\r\n"); if (is_bool($res)) { $result = $res; } else { $result = substr($res,$headerend+4,strlen($res) - ($headerend+4)); } fclose ($fp); # Check for keyword. if (!strstr($result, $keyword)) die("Weather Coding not found!"); # Read the file containing the last response. $filename = $txtfilename . '.txt'; $fp = fopen($filename, "r"); $contents = fread($fp, filesize($filename)); fclose($fp); # Check for changes. if ($contents == $result) { # There has not been a change. echo ("No Updates\r\n"); } else { # There has been a change. echo ("**UPDATES DETECTED**\r\n"); # Write the new file. $filename = $txtfilename . '.txt'; $fp = fopen($filename, "w"); $write = fputs($fp, $result); fclose($fp); # Send the e-mail. $recipient = $emailrecip; $subject = $emailsubject; $body_of_email = $result; $header = 'From: "' . $emailfromname . '" <' . $emailfromaddr . '>'; $header .= "\n"; $header .= "X-Priority: 1"; $header .= "\n"; mail ($recipient, $subject, $body_of_email, $header); echo ("E-mail sent."); } ?>
-
That not working and I think it's because the .txt file itself it just one long line rather then split up like the org. text. How hard would it be to just capture the text between "BULLETIN" and "NWS" That will just capture the line I want.
-
Small problem... I am php stupid, no real programming background and what you just said was like german to me. :'(
-
Comes from plain text. And I just underlined it on here to highlight it.
-
I'm trying to capture a line in a script. The script itself runs and produces the text as $result I'm trying to capture the underlined line. The issue I am having is it changes depend on the storm it may be longer or shorter but NEVER changes positions from line 6 so it would be easier to tell it to copy line 6 to $subject. Does this make any sense to anyone what I am trying to do? 194 WTNT32 KNHC 290231 TCPAT2 BULLETIN TROPICAL STORM GUSTAV ADVISORY NUMBER 17 NWS TPC/NATIONAL HURRICANE CENTER MIAMI FL AL072008 1100 PM EDT THU AUG 28 2008
-
Ok I will first say I know very very very little php and this is confusing me. This script was made for me and worked until this year and I can't figure it out. It reads from this page. http://www.mto.gov.on.ca/english/traveller/conditions/rdclosure.htm and pretty much need to reads just the parts between. <!-- TemplateBeginEditable name="content" --> and </DIV> on the HTML below. Problem is this year it started doing this. Reading "http://www.mto.gov.on.ca/english/traveller/conditions/rdclosure.htm". Searching for " "..." ". Searching for "". Number of current events = 1 Number of old events = 0 Fatal error: Cannot use string offset as an array in C:\WEATHERSCRIPTS\mto\test\roadclosed.php on line 44 And they changed the webpage and I can't figure out how to fix it. I've been trying to for 4 days now when I get time off work. I figure it is something really easy but not knowing PHP it isn't going to be easy for me to find. I've tried changing the part of the code but with no luck either. I hope someone can help. CHANGES I HAVE TRIED $content = find_data($webpage, "TemplateBeginEditable name=\"content\"", "</DIV>"); // All content is in the <DIV></DIV> $content = find_data($content, "<TABLE BORDER", "</TABLE>"); // Look for the events HTML table PHP SCRIPT(with the emails removed) <?php // Declare variables // $debug = true; $url = "http://www.mto.gov.on.ca/english/traveller/conditions/rdclosure.htm"; //$url = "./roadclosed1/rdclosure.htm"; #$email_recip = "email1"; $email_recip = "email2"; #$email_subject = MOVED TO LINE 47 $email_from = "WxServer Roads"; $email_from_addr = "Email3"; $message_header = "\n====START====\n"; $message_footer = "\n\n http://www.mto.gov.on.ca/english/traveller/conditions/rdclosure.htm"; $eventfilename = "roadclosed.txt"; // DON'T EDIT PAST HERE UNLESS YOU KNOW WHAT YOU'RE DOING // // MAIN PROGRAM /// $webpage = read_web($url); $content = find_data($webpage, "<DIV>", "</DIV>"); // All content is in the <DIV></DIV> $content = find_data($content, "<TABLE BORDER", "</TABLE>"); // Look for the events HTML table $curr_events = read_html_table($content,true); // Parse the events HTML table $old_content = read_events_file(); // Read the old events HTML table if ($old_content) { $old_events = read_html_table($old_content, true); // Parse the old events HTML table } else { unset($old_events); } if ($debug) { print("Number of current events = " . count($curr_events) . "\n"); } if ($debug) { print("Number of old events = " . count($old_events) . "\n"); } //Go through the current events and look for a corrisponding old event $send_email = false; // Flag used to see if an email should be sent for new closure events $event_message = $message_header; for ($curr_event_num = 0; $curr_event_num < count($curr_events); $curr_event_num++) { $curr_event_found = false; for ($old_event_num = 0; $old_event_num < count($old_events); $old_event_num++) { if ($debug) { print("Comparing (" . $curr_events[$curr_event_num][0] . "?" . $old_events[$old_event_num][0] . ") & (" . $curr_events[$curr_event_num][1] . "?" . $old_events[$old_event_num][1] . ") & (" . $curr_events[$curr_event_num][2] . "?" . $old_events[$old_event_num][2] . ")\n"); } if (($curr_events[$curr_event_num][0] == $old_events[$old_event_num][0]) && ($curr_events[$curr_event_num][1] == $old_events[$old_event_num][1]) && ($curr_events[$curr_event_num][2] == $old_events[$old_event_num][2])) { // Found a matching event $curr_event_found = true; } } if (!$curr_event_found) { // Add the not found current event to the message $send_email = true; if ($debug) { print ("Found a new event: \nRegion: " . $curr_events[$curr_event_num][1] . " \nHighway(s): " . $curr_events[$curr_event_num][0] . " \nReason: " . $curr_events[$curr_event_num][2] . "\n=====END=====\n"); } // $event_message .= ">>> NEW <<<\n"; $event_message .= "Region: " . $curr_events[$curr_event_num][1] . "\nHighway(s): " . $curr_events[$curr_event_num][0] . "\nReason: " . $curr_events[$curr_event_num][2] . "\n====END====\n"; $email_subject .= " Hwy: " . $curr_events[$curr_event_num][0]; } else { // if ($debug) { print ("Found a previously sent event:\nRegion: " . $curr_events[$curr_event_num][1] . "\nHighway(s): " . $curr_events[$curr_event_num][0] . "\nReason: " . $curr_events[$curr_event_num][2] . "\n====END====\n"); } // $event_message .= "Region: " . $curr_events[$curr_event_num][1] . "\nHighway(s): " . $curr_events[$curr_event_num][0] . "\nReason: " . $curr_events[$curr_event_num][2] . "\n====END====\n"; } } $event_message .= $message_footer; //Send an email if there is a new event //Send an email if there is a new event if ($send_email and !preg_match('/there\s*are\s*no\s*reported\s*closures/i', $event_message)) { print ("Sending the message below to " . $email_recip . ", from \"" . $email_from . "\" <" .$email_from_addr . ">.\n" . $event_message . "\n"); mail ($email_recip, $email_subject, $event_message, "From: \"" . $email_from . "\" <" .$email_from_addr . ">"); print ("Found a new event:\nRegion: " . $curr_events[$curr_event_num][1] . "\nHighway(s): " . $curr_events[$curr_event_num][0] . "\nReason: " . $curr_events[$curr_event_num][2] . "\n==========\n"); } //Save the events to check the next time the script is run write_events_file($content); // Functions // //read_web - Read the web page // $strURL ==> URL of the webpage function read_web($strURL) { global $debug; $buffer = ""; if($debug){ print("Reading \"$strURL\".\n"); } $fh = fopen($strURL, "rb"); if ($fh === false) { return ""; } while (!feof($fh)) { $buffer .= fread($fh, 1024); } fclose($fh); return $buffer; } // end function read_web // find)data - Gets a substring of the webpage by scraping the data // $strFile ==> text of the webpage // $strStart_Pattern ==> start of the substring // $strEnd_Pattern ==> end of the substring function find_data($strFile,$strStart_Pattern,$strEnd_Pattern, $intStart_Position = 0) { global $debug; if($debug){ print("Searching for \"$strStart_Pattern\"...\"$strEnd_Pattern\".\n"); } $first_match = strpos($strFile,$strStart_Pattern, $intStart_Position); if ($first_match) { # find the begining of the tag for ($i = $first_match; $i>0;$i--) { $char = $strFile[$i]; if ($char == "<" ) { $first_match = $i; //record the location of the tag break; } } $partialbuffer = substr($strFile,$first_match,strlen($strFile) - $first_match); # find the end of the sub string $second_match = strpos($partialbuffer,$strEnd_Pattern); return substr($partialbuffer,0,$second_match + strlen($strEnd_Pattern)); } else { return(false); } } //end function find_data // read_html_table - Read the contents of an HTML table and return the array(s) // strHTMLTable ==> HTML table text // boolSkipFirstRow ==> Skip the first row if it contains column titles (true/false) function read_html_table($strHTMLTable, $boolSkipFirstRow) { global $debug; $table = ""; $th = ""; $td = ""; if($boolSkipFirstRow) { $rownum = -2; } else { $rownum = -1; } $rowopen = false; $strPos = 0; do { // Scrape through the table if (strtoupper(substr($strHTMLTable, $strPos, 3)) == "<TR") { //Look for the rows $strPos +=3; $strPos = strpos($strHTMLTable, ">", $strPos) + 1; // Find the end of the row open tag $rowopen = true; $rownum++; } if (strtoupper(substr($strHTMLTable, $strPos, 5)) == "</TR>") { $rowopen = false; $strPos += 5; // jump to the end of the close tag } if (strtoupper(substr($strHTMLTable, $strPos, 3)) == "<TH") { // Look for headers $strPos += 3; $strPos = strpos($strHTMLTable, ">", $strPos) + 1; // Find the end of the header open tag $endPos = strpos(strtoupper($strHTMLTable), "</TH>", $strPos); // Find the header close tag $th[] = substr($strHTMLTable, $strPos, $endPos - $strPos); $strPos = $endPos + 5; if($debug) { print("Found column header \"" . end($th) . "\" (" . (count($th) - 1) . ")\n"); } } if (strtoupper(substr($strHTMLTable, $strPos, 3)) == "<TD") { // Look for cells $strPos += 3; $strPos = strpos($strHTMLTable, ">", $strPos) + 1; // Find the end of the cell open tag $endPos = strpos(strtoupper($strHTMLTable), "</TD>", $strPos); // Find the cell close tag $td[$rownum][] = str_replace(array("\n","\t")," ",substr($strHTMLTable, $strPos, $endPos - $strPos)); $td[$rownum][count($td[$rownum]) - 1] = str_replace("\r"," ",$td[$rownum][count($td[$rownum]) - 1]); $td[$rownum][count($td[$rownum]) - 1] = preg_replace("/[\s]+/"," ",$td[$rownum][count($td[$rownum]) - 1]); $strPos = $endPos + 5; if($debug) { print("Found cell data \"" . end($td[$rownum]) . "\" (" . $rownum . "|" . (count($td[$rownum]) - 1) . ")\n"); } } $strPos++; } while ($strPos <= strlen($strHTMLTable)); return $td; /* if ($boolRowOneColumnTitles) { if ($th[0] != "") { // Create a new array that uses the headers as the array key $newtd = ""; $header = ""; $row = ""; if ($debug) { print(count($th) . " headers\n"); } if ($debug) { print(count($td) . " rows\n"); } for ($row = 0; $row <= count($td) - 1; $row++) { for ($cell = 0; $cell <= count($td[$row]) - 1; $cell++) { if ($debug) { print("(". $row . "|" . $cell . ") => " . $th[$cell] . "(" . $row . ") = " . $td[$row][$cell] . "\n"); } $newtd[$th[$cell]][$cell] = $td[$row][$cell]; } } return $newtd; } else { // There are no headers, so just return the td array. return $td; } } else { // Just return what's in the td array return $td; } */ } //end function readhtmltable // read_events_file - Reads the old events from event file function read_events_file() { global $debug; $eventfile = "roadclosed.txt"; $buffer = ""; if (is_file($eventfile)) { // Check to see if the event log exists $fh = fopen($eventfile, "r"); while (!feof($fh)) { $buffer .= fread($fh, 1024); } return $buffer; } return; // Return an empty array } // end function read_events_file // write_events_file - Writes the current events to a file to be read later // - strEvents ==> array of events function write_events_file($strEvents) { global $debug; $eventfile = "roadclosed.txt"; $fh = fopen($eventfile, "w"); if ($fh === false) { if ($debug) { print("ERROR: Could not write to \"" . $eventfile . "\"!\n"); } } else { fwrite ($fh, $strEvents); fclose ($fh); } return; } // end function write_events_file print ("ClosedRoadScript"); print date(" (G:i:s)"); ?> HTML SOURCE OF WEBPAGE WITH AN ACTIVE ALERT <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "hmpro6.dtd"> <HTML LANG="en-ca"> <HEAD> <BASE HREF="http://www.mto.gov.on.ca/english/"> <LINK HREF="http://www.mto.gov.on.ca/css/style.css" REL="stylesheet" TYPE="text/css"> <META NAME="Author" CONTENT="Ontario Ministry of Transportation"> <!-- TemplateBeginEditable name="head" --> <META NAME="robots" CONTENT="noindex,nofollow"> <TITLE>Road Closures</TITLE> <!-- TemplateEndEditable --> </HEAD> <BODY BGCOLOR="#ffffff"> <P></P> <TABLE WIDTH="100%" BORDER="0" CELLSPACING="0" CELLPADDING="0"> <TR> <TD><A HREF="http://www.gov.on.ca/MBS/english/index.html"><IMG SRC="images/ont_head.gif" WIDTH="246" HEIGHT="61" BORDER="0" ALT="Government of Ontario"></A><!-- TemplateBeginEditable name="skipMenu" --><A HREF="traveller/conditions/rdclosure.htm#skipmenu"><IMG SRC="images/bump.gif" HEIGHT="1" WIDTH="1" BORDER="0" ALT="| Skip Navigation Menu |"></A><!-- TemplateEndEditable --></TD> <TD ALIGN="right"><A HREF="index.html"><IMG SRC="images/mto_e.gif" WIDTH="259" HEIGHT="51" BORDER="0" ALT="Ministry of Transportation"></A><BR><A ACCESSKEY="1" HREF="http://www.gov.on.ca/MBS/english/index.html"><IMG SRC="images/central-site.gif" WIDTH="63" HEIGHT="10" BORDER="0" ALT="Government of Ontario Central Site."></A><!-- TemplateBeginEditable name="topFeedback" --><A ACCESSKEY="2" HREF="feedback/mtoinfo.htm"><IMG SRC="images/feedback.gif" WIDTH="54" HEIGHT="10" BORDER="0" ALT="Contact us for questions and comments."></A><!-- TemplateEndEditable --><A ACCESSKEY="3" HREF="search.htm"><IMG SRC="images/search.gif" WIDTH="43" HEIGHT="10" BORDER="0" ALT="Search the Ministry of Transportation Web site."></A><A ACCESSKEY="4" HREF="map.htm"><IMG SRC="images/site-map.gif" WIDTH="52" HEIGHT="10" BORDER="0" ALT="Site map for the ministry Web site."></A><!-- TemplateBeginEditable name="topFrench" --><A ACCESSKEY="5" HREF="/french/traveller/conditions/rdclosure.htm"><SPAN LANG="fr-ca"><IMG SRC="images/francais.gif" WIDTH="48" HEIGHT="10" BORDER="0" ALT="Version française de cette page."></SPAN></A><!-- TemplateEndEditable --></TD> </TR> <TR> <TD COLSPAN="2" STYLE="background-color: #666666"><IMG SRC="images/bump.gif" WIDTH="1" HEIGHT="1" ALT=" "></TD> </TR> <TR> <TD COLSPAN="2"><IMG SRC="images/bump.gif" WIDTH="1" HEIGHT="3" ALT=" "></TD> </TR> </TABLE> <TABLE WIDTH="100%" BORDER="0" CELLSPACING="1" CELLPADDING="0"> <TR> <TD CLASS="menuTop"><A HREF="index.html">Home</A></TD> <TD CLASS="menuTop"><A HREF="about/">About the Ministry</A></TD> <TD CLASS="menuTop"><A HREF="dandv/">Drivers & Vehicles</A></TD> <TD CLASS="menuTop"><A HREF="traveller/">Traveller's Information</A></TD> <TD CLASS="menuTop"><A HREF="trucks/">Trucks & Buses</A></TD> <TD CLASS="menuTop"><A HREF="safety/">Road Safety</A></TD> </TR> <TR> <TD CLASS="menuTop"><A HREF="new/">What's New</A></TD> <TD CLASS="menuTop"><A HREF="engineering/">Highways</A></TD> <TD CLASS="menuTop"><A HREF="business/">Opportunities</A></TD> <TD CLASS="menuTop"><A HREF="pubs/">Publications</A></TD> <TD CLASS="menuTop"><A HREF="sites/sites.htm">Related Links</A></TD> <TD CLASS="menuTop"><A HREF="faq/">FAQ</A></TD> </TR> <TR> <!-- TemplateBeginEditable name="location" --> <TD CLASS="location" COLSPAN="6">Location: <A HREF="index.html">Ministry Home</A> > <A HREF="traveller/">Traveller's Information</A> > <A HREF="traveller/conditions/">Road Conditions</A> > Road Closures</TD> <!-- TemplateEndEditable --> </TR> <TR> <TD COLSPAN="6"><IMG SRC="images/bump.gif" WIDTH="1" HEIGHT="3" ALT=" "></TD> </TR> <TR> <TD COLSPAN="6" STYLE="background-color: #666666"><IMG SRC="images/bump.gif" WIDTH="1" HEIGHT="1" ALT=" "></TD> </TR> </TABLE> <BR> <!-- TemplateBeginEditable name="content" --> <DIV CLASS="content"> <H1>Road Closures</H1> <P>Please press the Refresh or Reload button at the top of your screen to see the most recent updates.</P> <P><B>Road closures and unscheduled events</B></P> <P><B>Last updated: October 12th, 2007at 2:20 a.m.</B></P> </DIV> <DIV CLASS="content"> <TABLE BORDER="1" CELLPADDING="2" CELLSPACING="0"> <TR> <TH BGCOLOR="#FFFFE6" WIDTH="15%" HEIGHT="33">Highway</TH> <TH BGCOLOR="#FFFFE6" WIDTH="22%" HEIGHT="33">Area</TH> <TH BGCOLOR="#FFFFE6" WIDTH="61%" HEIGHT="33" ALIGN="LEFT">Reason for closure</TH> </TR> <TR> <TH BGCOLOR="#FFFFE6" WIDTH="15%" HEIGHT="19">401</TH> <TH BGCOLOR="#FFFFE6" WIDTH="22%" HEIGHT="19">Ottawa</TH> <TH BGCOLOR="#FFFFE6" WIDTH="61%" HEIGHT="19" ALIGN="LEFT">is closed eastbound at Lancaster approximately 11 kilometres west of the Quebec border due to a collision. </TH> </TR> <!-- BEGIN ROW --> </TABLE></DIV> <DIV CLASS="content"> <P><SMALL>This information is provided as a public service. Although we endeavor to ensure that the information is as current and accurate as possible, errors do occasionally occur. Therefore, we cannot guarantee the accuracy of the information. Readers should, where possible, verify the information before relying on it.</SMALL></P> <H2 STYLE="font-size:1.1em;">Road information by phone</H2> <P><B>Provincial Call Centre:</B> 1-800-268-4686<BR><B>In GTA:</B> 416-235-4686</P> <P><B>Provincial TTY:</B> 1-866-471-8929<BR><B>Niagara Region TTY:</B> 905-704-2426</P><HR> <P CLASS="noBottom">See also:</P> <UL CLASS="noTop"> <LI><A HREF="traveller/conditions/index.html">Winter Road Condition Reports</A></LI> <LI><A HREF="traveller/compass/camera/pictures/tris/trismain.htm">Central Region Traffic Reports</A></LI> <LI><A HREF="winter.htm">Winter Driving in Ontario</A></LI> </UL> </DIV> <!-- TemplateEndEditable --> <HR><BR> <DIV CLASS="menuBottom"> | <A HREF="http://www.gov.on.ca/MBS/english/index.html">central site</A> | <!-- TemplateBeginEditable name="bottomFeedback" --> <A HREF="feedback/mtoinfo.htm">feedback</A> <!-- TemplateEndEditable --> | <A HREF="search.htm">search</A> | <A HREF="map.htm">site map</A> | <!-- TemplateBeginEditable name="bottomFrench" --> <A HREF="/french/traveller/conditions/rdclosure.htm"><SPAN LANG="fr-ca">français</SPAN></A> <!-- TemplateEndEditable --> | <BR>| <A HREF="index.html">Home</A> | <A HREF="about/">About the Ministry</A> | <A HREF="dandv/">Drivers & Vehicles</A> | <A HREF="traveller/">Traveller's Information</A> | <A HREF="trucks/">Trucks & Buses</A> | <A HREF="safety/">Road Safety</A> |<BR>| <A HREF="new/">What's New</A> | <A HREF="engineering/">Highways</A> | <A HREF="business/">Opportunities</A> | <A HREF="pubs/">Publications</A> | <A HREF="sites/sites.htm">Related Links</A> | <A HREF="faq/">FAQ</A> | <P><IMG SRC="images/wordmark.gif" WIDTH="404" HEIGHT="39" ALT="This site is maintained by the Government of Ontario, Canada." BORDER="0"><BR><BR> © 2003 <A HREF="http://www.gov.on.ca/MBS/english/common/queens.html">Queen's Printer for Ontario</A></P> </DIV> </BODY> </HTML>
-
I'm not sure I am doing this right. $removedata = ("THIS IS A WARNING THAT SEVERE THUNDERSTORMS ARE IMMINENT OR OCCURRING IN THESE REGIONS. REMEMBER SOME SEVERE THUNDERSTORMS PRODUCE TORNADOES..LISTEN FOR UPDATED WARNINGS. EMERGENCY MANAGEMENT ONTARIO RECOMMENDS TAKING COVER IMMEDIATELY WHEN THREATENING WEATHER APPROACHES."); $replacedata =""; str_replace($removedata, $replacedata, $pagedata); My PHP script reads the page, saves it to a txt file and emails it off. I need it to remove the part above before saving and emailing. I will be adding some other lines that need to be removed also. Am I doing it right?
-
Little more detail. This script used to read the coding from the URL for anything between <DIV></DIV> But the page has changed and now there is multiple DIV's in it now. If you run the script you will see the error. I have tried to edit the script at this point. $webpage = read_web($url); $content = find_data($webpage, "<DIV>", "</DIV>"); // All content is in the <DIV></DIV> $content = find_data($content, "<TABLE", "</TABLE>"); // Look for the events HTML table to have it start at different points but I still get the same error. It's likely something simple I overlooked.
-
Hi all, The script below is outputting this error. It just started today and I can't figure out how to correct it. Fatal error: Cannot use string offset as an array in C:\WEATHERSCRIPTS\mto\test\roadclosed.php on line 37 <?php // Declare variables // $debug = false; $url = "http://www.mto.gov.on.ca/english/traveller/conditions/rdclosure.htm"; //$url = "./roadclosed1/rdclosure.htm"; $email_recip = "REMOVED"; #$email_recip = "REMOVED"; #$email_subject = MOVED TO LINE 47 $email_from = "WxServer Roads"; $email_from_addr = "REMOVED"; $message_header = "\n====START====\n"; $message_footer = "\n\n http://www.mto.gov.on.ca/english/traveller/conditions/rdclosure.htm"; $eventfilename = "roadclosed.txt"; // DON'T EDIT PAST HERE UNLESS YOU KNOW WHAT YOU'RE DOING // // MAIN PROGRAM /// $webpage = read_web($url); $content = find_data($webpage, "<DIV CLASS=\"content\">", "</DIV>"); // All content is in the <DIV></DIV> $content = find_data($content, "<TABLE", "</TABLE>"); // Look for the events HTML table $curr_events = read_html_table($content,true); // Parse the events HTML table $old_content = read_events_file(); // Read the old events HTML table if ($old_content) { $old_events = read_html_table($old_content, true); // Parse the old events HTML table } else { unset($old_events); } if ($debug) { print("Number of current events = " . count($curr_events) . "\n"); } if ($debug) { print("Number of old events = " . count($old_events) . "\n"); } //Go through the current events and look for a corrisponding old event $send_email = false; // Flag used to see if an email should be sent for new closure events $event_message = $message_header; for ($curr_event_num = 0; $curr_event_num < count($curr_events); $curr_event_num++) { $curr_event_found = false; for ($old_event_num = 0; $old_event_num < count($old_events); $old_event_num++) { if ($debug) { print("Comparing (" . $curr_events[$curr_event_num][0] . "?" . $old_events[$old_event_num][0] . ") & (" . $curr_events[$curr_event_num][1] . "?" . $old_events[$old_event_num][1] . ") & (" . $curr_events[$curr_event_num][2] . "?" . $old_events[$old_event_num][2] . ")\n"); } if (($curr_events[$curr_event_num][0] == $old_events[$old_event_num][0]) && ($curr_events[$curr_event_num][1] == $old_events[$old_event_num][1]) && ($curr_events[$curr_event_num][2] == $old_events[$old_event_num][2])) { // Found a matching event $curr_event_found = true; } } if (!$curr_event_found) { // Add the not found current event to the message $send_email = true; if ($debug) { print ("Found a new event:\nRegion: " . $curr_events[$curr_event_num][1] . "\nHighway(s): " . $curr_events[$curr_event_num][0] . "\nReason: " . $curr_events[$curr_event_num][2] . "\n=====END=====\n"); } // $event_message .= ">>> NEW <<<\n"; $event_message .= "Region: " . $curr_events[$curr_event_num][1] . "\nHighway(s): " . $curr_events[$curr_event_num][0] . "\nReason: " . $curr_events[$curr_event_num][2] . "\n====END====\n"; $email_subject .= " Hwy: " . $curr_events[$curr_event_num][0]; } else { // if ($debug) { print ("Found a previously sent event:\nRegion: " . $curr_events[$curr_event_num][1] . "\nHighway(s): " . $curr_events[$curr_event_num][0] . "\nReason: " . $curr_events[$curr_event_num][2] . "\n====END====\n"); } // $event_message .= "Region: " . $curr_events[$curr_event_num][1] . "\nHighway(s): " . $curr_events[$curr_event_num][0] . "\nReason: " . $curr_events[$curr_event_num][2] . "\n====END====\n"; } } $event_message .= $message_footer; //Send an email if there is a new event //Send an email if there is a new event if ($send_email and !preg_match('/there\s*are\s*no\s*reported\s*closures/i', $event_message)) { print ("Sending the message below to " . $email_recip . ", from \"" . $email_from . "\" <" .$email_from_addr . ">.\n" . $event_message . "\n"); mail ($email_recip, $email_subject, $event_message, "From: \"" . $email_from . "\" <" .$email_from_addr . ">"); print ("Found a new event:\nRegion: " . $curr_events[$curr_event_num][1] . "\nHighway(s): " . $curr_events[$curr_event_num][0] . "\nReason: " . $curr_events[$curr_event_num][2] . "\n==========\n"); } //Save the events to check the next time the script is run write_events_file($content); // Functions // //read_web - Read the web page // $strURL ==> URL of the webpage function read_web($strURL) { global $debug; $buffer = ""; if($debug){ print("Reading \"$strURL\".\n"); } $fh = fopen($strURL, "rb"); if ($fh === false) { return ""; } while (!feof($fh)) { $buffer .= fread($fh, 1024); } fclose($fh); return $buffer; } // end function read_web // find)data - Gets a substring of the webpage by scraping the data // $strFile ==> text of the webpage // $strStart_Pattern ==> start of the substring // $strEnd_Pattern ==> end of the substring function find_data($strFile,$strStart_Pattern,$strEnd_Pattern, $intStart_Position = 0) { global $debug; if($debug){ print("Searching for \"$strStart_Pattern\"...\"$strEnd_Pattern\".\n"); } $first_match = strpos($strFile,$strStart_Pattern, $intStart_Position); if ($first_match) { # find the begining of the tag for ($i = $first_match; $i>0;$i--) { $char = $strFile[$i]; if ($char == "<" ) { $first_match = $i; //record the location of the tag break; } } $partialbuffer = substr($strFile,$first_match,strlen($strFile) - $first_match); # find the end of the sub string $second_match = strpos($partialbuffer,$strEnd_Pattern); return substr($partialbuffer,0,$second_match + strlen($strEnd_Pattern)); } else { return(false); } } //end function find_data // read_html_table - Read the contents of an HTML table and return the array(s) // strHTMLTable ==> HTML table text // boolSkipFirstRow ==> Skip the first row if it contains column titles (true/false) function read_html_table($strHTMLTable, $boolSkipFirstRow) { global $debug; $table = ""; $th = ""; $td = ""; if($boolSkipFirstRow) { $rownum = -2; } else { $rownum = -1; } $rowopen = false; $strPos = 0; do { // Scrape through the table if (strtoupper(substr($strHTMLTable, $strPos, 3)) == "<TR") { //Look for the rows $strPos +=3; $strPos = strpos($strHTMLTable, ">", $strPos) + 1; // Find the end of the row open tag $rowopen = true; $rownum++; } if (strtoupper(substr($strHTMLTable, $strPos, 5)) == "</TR>") { $rowopen = false; $strPos += 5; // jump to the end of the close tag } if (strtoupper(substr($strHTMLTable, $strPos, 3)) == "<TH") { // Look for headers $strPos += 3; $strPos = strpos($strHTMLTable, ">", $strPos) + 1; // Find the end of the header open tag $endPos = strpos(strtoupper($strHTMLTable), "</TH>", $strPos); // Find the header close tag $th[] = substr($strHTMLTable, $strPos, $endPos - $strPos); $strPos = $endPos + 5; if($debug) { print("Found column header \"" . end($th) . "\" (" . (count($th) - 1) . ")\n"); } } if (strtoupper(substr($strHTMLTable, $strPos, 3)) == "<TD") { // Look for cells $strPos += 3; $strPos = strpos($strHTMLTable, ">", $strPos) + 1; // Find the end of the cell open tag $endPos = strpos(strtoupper($strHTMLTable), "</TD>", $strPos); // Find the cell close tag $td[$rownum][] = str_replace(array("\n","\t")," ",substr($strHTMLTable, $strPos, $endPos - $strPos)); $td[$rownum][count($td[$rownum]) - 1] = str_replace("\r"," ",$td[$rownum][count($td[$rownum]) - 1]); $td[$rownum][count($td[$rownum]) - 1] = preg_replace("/[\s]+/"," ",$td[$rownum][count($td[$rownum]) - 1]); $strPos = $endPos + 5; if($debug) { print("Found cell data \"" . end($td[$rownum]) . "\" (" . $rownum . "|" . (count($td[$rownum]) - 1) . ")\n"); } } $strPos++; } while ($strPos <= strlen($strHTMLTable)); return $td; /* if ($boolRowOneColumnTitles) { if ($th[0] != "") { // Create a new array that uses the headers as the array key $newtd = ""; $header = ""; $row = ""; if ($debug) { print(count($th) . " headers\n"); } if ($debug) { print(count($td) . " rows\n"); } for ($row = 0; $row <= count($td) - 1; $row++) { for ($cell = 0; $cell <= count($td[$row]) - 1; $cell++) { if ($debug) { print("(". $row . "|" . $cell . ") => " . $th[$cell] . "(" . $row . ") = " . $td[$row][$cell] . "\n"); } $newtd[$th[$cell]][$cell] = $td[$row][$cell]; } } return $newtd; } else { // There are no headers, so just return the td array. return $td; } } else { // Just return what's in the td array return $td; } */ } //end function readhtmltable // read_events_file - Reads the old events from event file function read_events_file() { global $debug; $eventfile = "roadclosed.txt"; $buffer = ""; if (is_file($eventfile)) { // Check to see if the event log exists $fh = fopen($eventfile, "r"); while (!feof($fh)) { $buffer .= fread($fh, 1024); } return $buffer; } return; // Return an empty array } // end function read_events_file // write_events_file - Writes the current events to a file to be read later // - strEvents ==> array of events function write_events_file($strEvents) { global $debug; $eventfile = "roadclosed.txt"; $fh = fopen($eventfile, "w"); if ($fh === false) { if ($debug) { print("ERROR: Could not write to \"" . $eventfile . "\"!\n"); } } else { fwrite ($fh, $strEvents); fclose ($fh); } return; } // end function write_events_file print ("ClosedRoadScript"); print date(" (G:i:s)"); ?>
-
<?php //============ Local Functions ====================== function webfile($url) { // var $totalbuffer; $totalbuffer = ""; if (!($fp=fopen($url,"rb"))) { return "None"; } else { while ($buffer = fread($fp,1024)) { $totalbuffer .= $buffer; } return $totalbuffer; } } //end function // Gets a substring of the webpage for scraping of the data // $strFile ==> text webpage; $strStart_Pattern ==> start of the substring; $strEnd_Pattern ==> end of the substring function finddata($strFile,$strStart_Pattern,$strEnd_Pattern, $intStart_Position = 0) { $first_match = strpos($strFile,$strStart_Pattern, $intStart_Position); if ($first_match) { # find the begining of the tag for ($i = $first_match; $i>0;$i--) { $char = $strFile[$i]; if ($char == "<" ) { $first_match = $i; //record the location of the tag break; } } $partialbuffer = substr($strFile,$first_match,strlen($strFile) - $first_match); # find the end of the sub string $second_match = strpos($partialbuffer,$strEnd_Pattern); return substr($partialbuffer,0,$second_match + strlen($strEnd_Pattern)); } else { return_false; } // end if } //end function // records the all events with their last updated time function event_log ($event) { } // end function event_log //============== Main program ===================== # ---------------------------------------------------------------------------------- # Configuration # Directory on the remote server. Be sure to include leading and trailing slashes! $remotedir = "/english/traveller/compass/camera/pictures/tris/"; # File on the remote server. No slashes! $remotefile = "durham.htm"; # Event type (Unscheduled, Scheduled) $event_type = "Unscheduled"; # Keyword to check for in response. $keyword = "Reference"; # Remote server address. $remoteserver = "http://www.mto.gov.on.ca"; # E-mail recipient(s). Separate each address with a comma and a space. $emailrecip = "###@####"; # E-mail subject line. $emailsubject = "Road Alert" . date(' (G:i:s)'); # E-mail From name. $emailfromname = "WxSrv Roads"; # E-mail From address. $emailfromaddr = "####@###"; # End Configuration # ---------------------------------------------------------------------------------- // retrive the webpage $currentpage = webfile($remoteserver . $remotedir . $remotefile); // isolate the date $currentrow = finddata($currentpage,"<TABLE WIDTH=\"600\" CELLPADDING=\"0\" CELLSPACING=\"0\">","</TABLE>"); // futher isoloate for the last update $table_row = finddata($currentrow,'<TR>','</TR>'); $document_position = strpos($currentpage,$table_row); //================================================ if ( strpos($table_row,$event_type) ) { //Determin if an $event type occurs //Records the first starting location of the string $start_pos = strpos($table_row,"<P ALIGN=\"LEFT\">"); //Start of the loop for extracting events while ( $start_pos ) { //events are coded in the table row and start with the tags <P> and end with</P> $event = finddata($table_row,"<P ALIGN=\"LEFT\">","</P>",$start_pos); if ( strpos($event,"Reference ID:") ) { //Determine if this is and event with a reference ID: //Now we need to massage the string to get the info $event = strip_tags($event,'<BR>'); // Creates an array from the $event string based on the remain html tag <BR> # list($referenceID, $location, $description, $impact, $updated) = explode("<BR>",$event); $event_array = explode("<BR>",$event); //Loops through the array to print it out $new_event = true; foreach ($event_array as $item) { echo "$item<br>\n"; $message .= "$item\n"; } //end foreach echo "<br />"; echo "=====<br />"; $message .= "====-END-====\n\n"; //End of display loop } // end Reference ID $start_pos = strpos($table_row,"<P ALIGN=\"LEFT\">",$start_pos+1); // get next event } // end while loop //End of Event Loop // MY CHANGES $message = trim($message); $message = strip_tags($message); $message = str_replace("\r",'',$message); $message = str_replace("\n ",'',$message); # Check for keyword. if (!strstr($message, $keyword)) die("ERROR Code Word Not Found"); # Read the file containing the last response. $filename = $remotefile . '.txt'; $fp = fopen($filename, "r"); $contents = fread($fp, filesize($filename)); fclose($fp); # Check for changes. if ($contents == $message) { # There has not been a change. echo ("No Updates\r\n"); } else { # There has been a change. echo ("**UPDATES DETECTED**\r\n"); # Write the new file. $filename = $remotefile . '.txt'; $fp = fopen($filename, "w"); $write = fputs($fp, $message); fclose($fp); # Send the e-mail. $recipient = $emailrecip; $subject = $emailsubject; $body_of_email = $message; $header = 'From: "' . $emailfromname . '" <' . $emailfromaddr . '>'; mail ($recipient, $subject, $body_of_email, $header); echo ("E-mail sent."); } } else { //This is displayed when nothing is to report ***Can be deleted*** echo "No $emailsubject\n"; echo "<br>============<br>\n"; } // end Unscheduled Event Scrape // I COMMENTED THIS OUT BECAUSE OF THE EMAIL STUFF ABOVE // if ( strlen($message) ) { //must change to // mail($emailrecip, $emailsubject, $message); // } ?> And this is what it outputs. Reference ID: 10479 HWY 401 Eastbound Collector [ WHITES RD - LIVERPOOL RD ] Lanes affected: 1 right lane and right shoulder Event start date: 2007-04-22 Event start time: 01:31 Reason: Collision Traffic impact: Serious Last change: UPDATED 2007-04-22 03:43 ====-END-==== Is there a way to have lines 2 and 6 as part of the subject so it looked like this. Subject: MTO-DURHAM - Collision - HWY 401 Eastbound Collector [ WHITES RD - LIVERPOOL RD ]
-
So is the 3rd Party Script forum the place where bad people go? It's not a 3rd Party Script. It was a script created for me which you won't find anyplace else online that anyone else can use. The only problem is the guy who created it for me got shipped overseas for 6 months.
-
I am trying to make an if statement that does the following but I can't figure out how to word it or where to place it. if $message == There are no reported closures. then DIE; Now I think it needs to be before this part. [code]//Send an email if there is a new event if ($send_email) { print ("Sending the message below to " . $email_recip . ", from \"" . $email_from . "\" <" .$email_from_addr . ">.\n" . $event_message . "\n"); mail ($email_recip, $email_subject, $event_message, "From: \"" . $email_from . "\" <" .$email_from_addr . ">"); print ("Found a new event:\nRegion: " . $curr_events[$curr_event_num][1] . "\nHighway(s): " . $curr_events[$curr_event_num][0] . "\nReason: " . $curr_events[$curr_event_num][2] . "\n==========\n"); } [/code] So that it stops the script from sending the email. Does this help any?