Jump to content

Need help with warning map. PHP


davefootball123

Recommended Posts

Another issue...of course. I tried to make the code have to meet 2 requirements in order to display the image...so I added $search_string2...now I get strpos() expects parameter 3 to be long.

<?php
function curl_get_contents($url) {
// Initiate the curl session
$ch = curl_init();
// Set the URL<span id="more-487"></span>
curl_setopt($ch, CURLOPT_URL, $url);
// Removes the headers from the output
curl_setopt($ch, CURLOPT_HEADER, 0);
// Return the output instead of displaying it directly
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Execute the curl session
$output = curl_exec($ch);
// Close the curl session
curl_close($ch);
// Return the output as a variable
return $output;
}

$output = curl_get_contents('http://www.weatheroffice.gc.ca/city/pages/on-94_metric_e.html');
$search_string = '<div class="lefttab">Current Conditions</div>';
$search_string2 = 'WINDSOR';

if(strpos($output, $search_string, $search_string2) !== false){
// There's a watch.
echo '<center><img style="position:absolute;left:50%;margin-left:-397px; margin-top:30px" src="/Warningoverlays/SVR 

Watch/windsor.png" border="0">';
}

?>

Link to comment
Share on other sites

on another tack, using the RSS

 


$output = curl_get_contents('http://www.weatheroffice.gc.ca/rss/city/on-94_e.xml');
$xml = simplexml_load_string($output);

echo $xml->channel->item[0]->title. '<br /><br />';    //=> No watches or warnings in effect, Windsor
echo $xml->channel->item[1]->title. '<br /><br />';    //=> Current Conditions: Cloudy, 15.1°C

 

 

Link to comment
Share on other sites

Out of interest... Why do you suggest loading the xml using curl first? Is there some advantage to just straight loading it using simplexml_load_file?

 

 

 

on another tack, using the RSS

 


$output = curl_get_contents('http://www.weatheroffice.gc.ca/rss/city/on-94_e.xml');
$xml = simplexml_load_string($output);

echo $xml->channel->item[0]->title. '<br /><br />';    //=> No watches or warnings in effect, Windsor
echo $xml->channel->item[1]->title. '<br /><br />';    //=> Current Conditions: Cloudy, 15.1°C

Link to comment
Share on other sites

OK I pretty well got it

http://www.southernontariochasing.ca/ecwarnings.php

Only problem is that it takes a bit to load. How would I make the script run every 5 minutes to create the map using cron job?

 

Heres the code

<?php
function curl_get_contents($url) {
// Initiate the curl session
$ch = curl_init();
// Set the URL<span id="more-487"></span>
curl_setopt($ch, CURLOPT_URL, $url);
// Removes the headers from the output
curl_setopt($ch, CURLOPT_HEADER, 0);
// Return the output instead of displaying it directly
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Execute the curl session
$output = curl_exec($ch);
// Close the curl session
curl_close($ch);
// Return the output as a variable
return $output;
}

$output = curl_get_contents('http://www.weatheroffice.gc.ca/rss/city/on-94_e.xml');
$search_string = 'No watches or warnings in effect';


if(strpos($output, $search_string) !== false){
// There's a watch.
echo '<center><img style="position:absolute;left:50%;margin-left:-397px; margin-top:30px" src="/Warningoverlays/SVR 

Watch/windsor.png" border="0">';
}
$output = curl_get_contents('http://www.weatheroffice.gc.ca/rss/city/on-11_e.xml');
$search_string = 'No watches or warnings in effect';


if(strpos($output, $search_string) !== false){
// There's a watch.
echo '<center><img style="position:absolute;left:50%;margin-left:-397px; margin-top:30px" src="/Warningoverlays/SVR 

Watch/chatham.png" border="0">';
}
$output = curl_get_contents('http://www.weatheroffice.gc.ca/rss/city/on-147_e.xml');
$search_string = 'No watches or warnings in effect';


if(strpos($output, $search_string) !== false){
// There's a watch.
echo '<center><img style="position:absolute;left:50%;margin-left:-397px; margin-top:30px" src="/Warningoverlays/SVR 

Watch/sarnia.png" border="0">';
}
$output = curl_get_contents('http://www.weatheroffice.gc.ca/rss/warning/on-137_e.xml');
$search_string = 'No watches or warnings in effect';


if(strpos($output, $search_string) !== false){
// There's a watch.
echo '<center><img style="position:absolute;left:50%;margin-left:-397px; margin-top:30px" src="/Warningoverlays/SVR 

Watch/london.png" border="0">';
}
$output = curl_get_contents('http://www.weatheroffice.gc.ca/rss/warning/on-98_e.xml');
$search_string = 'No watches or warnings in effect';


if(strpos($output, $search_string) !== false){
// There's a watch.
echo '<center><img style="position:absolute;left:50%;margin-left:-397px; margin-top:30px" src="/Warningoverlays/SVR 

Watch/elgin.png" border="0">';
}
$output = curl_get_contents('http://www.weatheroffice.gc.ca/rss/warning/on-42_e.xml');
$search_string = 'No watches or warnings in effect';


if(strpos($output, $search_string) !== false){
// There's a watch.
echo '<center><img style="position:absolute;left:50%;margin-left:-397px; margin-top:30px" src="/Warningoverlays/SVR 

Watch/haldimand-norfolk.png" border="0">';
}
$output = curl_get_contents('http://www.weatheroffice.gc.ca/rss/warning/on-15_e.xml');
$search_string = 'No watches or warnings in effect';


if(strpos($output, $search_string) !== false){
// There's a watch.
echo '<center><img style="position:absolute;left:50%;margin-left:-397px; margin-top:30px" src="/Warningoverlays/SVR 

Watch/oxford.png" border="0">';
}
$output = curl_get_contents('http://www.weatheroffice.gc.ca/rss/warning/on-86_e.xml');
$search_string = 'No watches or warnings in effect';


if(strpos($output, $search_string) !== false){
// There's a watch.
echo '<center><img style="position:absolute;left:50%;margin-left:-397px; margin-top:30px" src="/Warningoverlays/SVR 

Watch/brant.png" border="0">';
}
$output = curl_get_contents('http://www.weatheroffice.gc.ca/rss/warning/on-77_e.xml');
$search_string = 'No watches or warnings in effect';


if(strpos($output, $search_string) !== false){
// There's a watch.
echo '<center><img style="position:absolute;left:50%;margin-left:-397px; margin-top:30px" src="/Warningoverlays/SVR 

Watch/hamilton.png" border="0">';
}
$output = curl_get_contents('http://www.weatheroffice.gc.ca/rss/warning/on-107_e.xml');
$search_string = 'No watches or warnings in effect';


if(strpos($output, $search_string) !== false){
// There's a watch.
echo '<center><img style="position:absolute;left:50%;margin-left:-397px; margin-top:30px" src="/Warningoverlays/SVR 

Watch/niagara.png" border="0">';
}
$output = curl_get_contents('http://www.weatheroffice.gc.ca/rss/warning/on-82_e.xml');
$search_string = 'No watches or warnings in effect';


if(strpos($output, $search_string) !== false){
// There's a watch.
echo '<center><img style="position:absolute;left:50%;margin-left:-397px; margin-top:30px" src="/Warningoverlays/SVR 

Watch/waterloo.png" border="0">';
}
$output = curl_get_contents('http://www.weatheroffice.gc.ca/rss/warning/on-68_e.xml');
$search_string = 'No watches or warnings in effect';


if(strpos($output, $search_string) !== false){
// There's a watch.
echo '<center><img style="position:absolute;left:50%;margin-left:-397px; margin-top:30px" src="/Warningoverlays/SVR 

Watch/halton-peel.png" border="0">';
}
$output = curl_get_contents('http://www.weatheroffice.gc.ca/rss/warning/on-5_e.xml');
$search_string = 'No watches or warnings in effect';


if(strpos($output, $search_string) !== false){
// There's a watch.
echo '<center><img style="position:absolute;left:50%;margin-left:-397px; margin-top:30px" src="/Warningoverlays/SVR 

Watch/wellington.png" border="0">';
}
$output = curl_get_contents('http://www.weatheroffice.gc.ca/rss/warning/on-143_e.xml');
$search_string = 'No watches or warnings in effect';


if(strpos($output, $search_string) !== false){
// There's a watch.
echo '<center><img style="position:absolute;left:50%;margin-left:-397px; margin-top:30px" src="/Warningoverlays/SVR 

Watch/toronto.png" border="0">';
}
$output = curl_get_contents('http://www.weatheroffice.gc.ca/rss/warning/on-160_e.xml');
$search_string = 'No watches or warnings in effect';


if(strpos($output, $search_string) !== false){
// There's a watch.
echo '<center><img style="position:absolute;left:50%;margin-left:-397px; margin-top:30px" src="/Warningoverlays/SVR 

Watch/huron.png" border="0">';
}
$output = curl_get_contents('http://www.weatheroffice.gc.ca/rss/warning/on-116_e.xml');
$search_string = 'No watches or warnings in effect';


if(strpos($output, $search_string) !== false){
// There's a watch.
echo '<center><img style="position:absolute;left:50%;margin-left:-397px; margin-top:30px" src="/Warningoverlays/SVR 

Watch/perth.png" border="0">';
}
$output = curl_get_contents('http://www.weatheroffice.gc.ca/rss/warning/on-28_e.xml');
$search_string = 'No watches or warnings in effect';


if(strpos($output, $search_string) !== false){
// There's a watch.
echo '<center><img style="position:absolute;left:50%;margin-left:-397px; margin-top:30px" src="/Warningoverlays/SVR 

Watch/southernbruce.png" border="0">';
}
$output = curl_get_contents('http://www.weatheroffice.gc.ca/rss/warning/on-157_e.xml');
$search_string = 'No watches or warnings in effect';


if(strpos($output, $search_string) !== false){
// There's a watch.
echo '<center><img style="position:absolute;left:50%;margin-left:-397px; margin-top:30px" src="/Warningoverlays/SVR 

Watch/northernbruce.png" border="0">';
}
$output = curl_get_contents('http://www.weatheroffice.gc.ca/rss/warning/on-7_e.xml');
$search_string = 'No watches or warnings in effect';


if(strpos($output, $search_string) !== false){
// There's a watch.
echo '<center><img style="position:absolute;left:50%;margin-left:-397px; margin-top:30px" src="/Warningoverlays/SVR 

Watch/grey.png" border="0">';
}
$output = curl_get_contents('http://www.weatheroffice.gc.ca/rss/warning/on-140_e.xml');
$search_string = 'No watches or warnings in effect';


if(strpos($output, $search_string) !== false){
// There's a watch.
echo '<center><img style="position:absolute;left:50%;margin-left:-397px; margin-top:30px" src="/Warningoverlays/SVR 

Watch/dufferin-innisfil.png" border="0">';
}
$output = curl_get_contents('http://www.weatheroffice.gc.ca/rss/warning/on-151_e.xml');
$search_string = 'No watches or warnings in effect';


if(strpos($output, $search_string) !== false){
// There's a watch.
echo '<center><img style="position:absolute;left:50%;margin-left:-397px; margin-top:30px" src="/Warningoverlays/SVR 

Watch/barrie.png" border="0">';
}
$output = curl_get_contents('http://www.weatheroffice.gc.ca/rss/warning/on-64_e.xml');
$search_string = 'No watches or warnings in effect';


if(strpos($output, $search_string) !== false){
// There's a watch.
echo '<center><img style="position:absolute;left:50%;margin-left:-397px; margin-top:30px" src="/Warningoverlays/SVR 

Watch/york-durham.png" border="0">';
}
$output = curl_get_contents('http://www.weatheroffice.gc.ca/rss/warning/on-168_e.xml');
$search_string = 'No watches or warnings in effect';


if(strpos($output, $search_string) !== false){
// There's a watch.
echo '<center><img style="position:absolute;left:50%;margin-left:-397px; margin-top:30px" src="/Warningoverlays/SVR 

Watch/kawartha.png" border="0">';
}
?>

Link to comment
Share on other sites

I have the map part done...now I need some help with the text part...where I'm completely lost.  Heres the link of the text I need to grab http://kamala.cod.edu/Canada/latest.wfcn11.CWTO.html

Now, I need to have it so that if TORNADO WARNINGS ISSUED FOR ....or TORNADO WARNING CONTINUED FOR....and then have the specific area...in this case it would be looking for WINDSOR-LEAMINGTON-ESSEX COUNTY....I need it so that if tornado warning issued for/continued for windsor...then get the text...else dont get text. Anyone have any ideas?

 

Thanks, Dave

Link to comment
Share on other sites

OK, I've figured out that I will use file_get_contents() ...now I just need to know how to have 2 conditions met...before it will display the text. Example

 

file_get_contents($url) ...if the link has WINDSOR-LEAMINGTON-ESSEX COUNTY...and...TORNADO WARNING...show the text thats on the page.

 

 

Thanks for any help, Dave

Link to comment
Share on other sites

Well if you want to find specific strings within your contents then you could use preg_match with a regular expression. e.g.:

 

$tester = file_get_contents('http://kamala.cod.edu/Canada/latest.wfcn11.CWTO.html'); //$tester contains contents to search


$pat = '/TORNADO WARNING/';  // literal pattern to find

if(preg_match($pat, $tester, $matches)){

echo "Yup, tornado heading your way kid...get out of there you crazy fool!";
print_r($matches);
}

else {

echo "Nothing Found!";
}

 

 

You can pass it an array of matches too so you'll be able to find everything you want in one go.

 

Hope that helps a bit...

 

You will probably want to get more sophisticated with the match - i.e. only match place name if it's preceded by a specific bit of text. Otherwise the actualy page might say "tornado warning has been stepped down in WINDSOR" and you're preg match will see the warning text and windsor and start scaring people on your site :(

 

 

 

I have the map part done...now I need some help with the text part...where I'm completely lost.  Heres the link of the text I need to grab http://kamala.cod.edu/Canada/latest.wfcn11.CWTO.html

Now, I need to have it so that if TORNADO WARNINGS ISSUED FOR ....or TORNADO WARNING CONTINUED FOR....and then have the specific area...in this case it would be looking for WINDSOR-LEAMINGTON-ESSEX COUNTY....I need it so that if tornado warning issued for/continued for windsor...then get the text...else dont get text. Anyone have any ideas?

 

Thanks, Dave

Link to comment
Share on other sites

Thats what im looking for, however Im having trouble with 2 conditions. I need the script to check for TORNADO WARNING ENDED FOR as well as SMITHS FALLS - LANARK - SHARBOT LAKE...and I cant seem to get that.

Im trying to make it look for both conditions before showing the text. Any ideas?

<?php

$tester = file_get_contents('http://kamala.cod.edu/Canada/latest.wfcn11.CWTO.html'); //$tester contains contents to search


$pat = '/TORNADO WARNING ENDED FOR:/';  // literal pattern to find
$pat2 = '/SMITHS FALLS - LANARK - SHARBOT LAKE/';  
if(preg_match($pat,  $pat2, $tester)){

echo '<center><img style="position:absolute;left:50%;margin-left:-397px; margin-top:30px" src="/Warningoverlays/TOR 

Warn/windsor.png" border="0">';

}
else 
{ 
echo '';
}
?>

Link to comment
Share on other sites

After a little bit of playing around I managed to get somewhere

This works decent. If the $city...isnt that value then it wont echo the html however it doesnt matter what $tornado is set to...as long as $city is correct it will always echo...even if its not set to tornado warning.

<?php

$tester = file_get_contents('http://kamala.cod.edu/Canada/latest.wfcn11.CWTO.html'); //$tester contains contents to search
$tornado = 'TORNADO WARNING ENDED FOR:';
$city = 'SMITHS FALLS - LANARK - SHARBOT LAKE';


if(preg_match( "/$tornado|$city/i", $tester ) ){

echo '<center><img style="position:absolute;left:50%;margin-left:-397px; margin-top:30px" src="/Warningoverlays/TOR 

Warn/windsor.png" border="0">';

}
else 
{ 
echo '';
}
?>

Link to comment
Share on other sites

The thing about Regular Expressions, is that you're meant to use them to define a pattern you want to search for. It is a highly complex syntax, but it has to be in order to make it possible to generate rules for natural languages. At least without it being longer than the patterns you want to match.

 

The string you want to match, constitutes of two parts: A static part, and one part in which you have a selected group which can be matched. The easiest RegExp to match this, would be written like this:

$RegExp = '/TORNADO WARNING ENDED FOR:\\s+(SMITHS FALLS|LANARK|SHARBOT LAKE)/';

A quick run down of what it means:

/                            // Opening delimiter, to tell that whatever's after this is the actual pattern to match
TORNADO WARNING ENDED FOR:   // The static, literal part.
\s+                         // One or more whitespace characters (spaces, tabs, etc).
(                            // Open a capturing sub group, so that we can store the name of the location.
SMITHS FALLS                 // Another string literal.
|                            // Pipe, means "or". When between two string literals, it matches either the one to the left, or the one to the right.
LANARK                       // Another string literal. Because of the pipe preceeding this, it'll only match if the previous string literal didn't match.
|                            // Another "or", in case either of the two previous string literals doesn't match.
SHARBOT LAKE                 // The last string literal.
)                            // Close the capturing sub group, which contains the "or" operation to whatever is inside of it.
/                            // Ending delimiter, everything after this is a modifier for the pattern.

 

If you want to learn more about RegExps, which I recommend if you're going to use them, then please visit the link I gave in the beginning of this post.

 

PS: You also need to check the PHP manual for preg_match (), as you're using the wrong syntax for it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.