davefootball123 Posted August 17, 2012 Share Posted August 17, 2012 Hi everyone. I'm coding an Environment Canada weather warning map for better ease of use for people. What I need is hopefully simple. On the EC website if a warning is issued a text bulliten for the forecast page will say SEVERE THUNDERSTORM WARNING or any type of warning for that matter. Here is an example of the page http://www.weatheroffice.gc.ca/city/pages/on-94_metric_e.html , if a severe thunderstorm warning is issued at the top of the page it will say SEVERE THUNDERSTORM WARNING IN EFFECT, or TORNADO WARNING IN EFFECT for a tornado warning. What I need to do is have a php code check that url and see if SEVERE THUNDERSTORM WARNING exists and it if does show an image...just a normal <img src="file name"> image. I hope i explained it clear enough. I think what I need is for php to read the content of an external url and if the required content exists...show the image...if not dont show the image. If anyone could help me that would be greatly appreciated. Thanks, Dave Quote Link to comment Share on other sites More sharing options...
xyph Posted August 17, 2012 Share Posted August 17, 2012 What have you tried so far? Quote Link to comment Share on other sites More sharing options...
davefootball123 Posted August 17, 2012 Author Share Posted August 17, 2012 I haven't tried very much, except get_file_contents...and then the url. I think thats how I need to go but I have no clue how to make it look for a piece of text...and if that piece of text is there...show an image. Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 17, 2012 Share Posted August 17, 2012 for the text you could use strpos Quote Link to comment Share on other sites More sharing options...
davefootball123 Posted August 17, 2012 Author Share Posted August 17, 2012 Thanks very much. I'm probably sounding rude here but can anyone put some code together... that would read content from a url and then if the defined text is found show an image...of someone could do that...that would be greatly appreciated then I could do it all from there. Â Thanks, Dave Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 17, 2012 Share Posted August 17, 2012 You could do all what? That's the whole project minus one if construct. :-P Â Edit: Wait no, that is the whole project. Quote Link to comment Share on other sites More sharing options...
davefootball123 Posted August 17, 2012 Author Share Posted August 17, 2012 Lol true...im just new to PHP and trying to make a better weather warning map than what our weather agency is currently using. I'm not sure how much code it would take though.. Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 17, 2012 Share Posted August 17, 2012 $url = ''; $search_string = 'WARNING IN EFFECT'; $file_contents = file_get_contents($url); if(stripos($file_contents, $search_string) !== FALSE){ // There's a warning. } Quote Link to comment Share on other sites More sharing options...
davefootball123 Posted August 17, 2012 Author Share Posted August 17, 2012 Wow, thats so much. Another quick question. What code would display <img src="filenamegoeshere"> Â Thanks so much for the help. It is greatly appreciated. Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 17, 2012 Share Posted August 17, 2012 The code you just posted. It's HTML. PHP can output HTML using echo or print. Quote Link to comment Share on other sites More sharing options...
davefootball123 Posted August 17, 2012 Author Share Posted August 17, 2012 <?php $url = 'http://www.southernontariochasing.ca/testwarn.html'; $search_string = 'SEVERE THUNDERSTORM WARNING IN EFFECT'; $file_contents = file_get_contents($url); if(stripos($file_contents, $search_string) !== FALSE){ // There's a warning. echo '<img src="Day 1 Cat.png" />'; } ?> Â Works great. Thanks! If I have another issue I will be sure to come back. You guys are great! Quote Link to comment Share on other sites More sharing options...
davefootball123 Posted August 17, 2012 Author Share Posted August 17, 2012 To follow up to my last post...heres a quick example of what im doing http://www.southernontariochasing.ca/test.php  Works wonderful. Thank you.  EDIT: One other quick thing. the website that is being read displays the data as <a herf="link>SEVERE THUNDERSTORM WARNING</a> and I have to put that full html code in the seach_string...works fine however the link changes with the issuance of each weather warning. Is there anyway to get around that?  Sorry to edit again. I have seem'd to figure it out. Thanks for all the help! Quote Link to comment Share on other sites More sharing options...
davefootball123 Posted August 18, 2012 Author Share Posted August 18, 2012 I've run into some trouble. Is there a way to have multiple $search_strings...What I mean is it has to meet like SEVERE THUNDERSTORM WATCH and then like the city ie WINDSOR...how would I do this? Quote Link to comment Share on other sites More sharing options...
Christian F. Posted August 18, 2012 Share Posted August 18, 2012 I think this page will be a good starting point for you, especially if you read the user notes as well. I also recommend reading some tutorials on how to write PHP, the one in the PHP manual is a good start. Programming is pure logic, after all. So if you've done something that works before, and want to do something just a bit differently, chances are you just have to change the part that's different. Quote Link to comment Share on other sites More sharing options...
Drongo_III Posted August 18, 2012 Share Posted August 18, 2012 Does the site you're scraping have an RSS feed for weather warnings? Â That would provide you with a much more robust way of outputting multiple weather warnings... Quote Link to comment Share on other sites More sharing options...
davefootball123 Posted August 18, 2012 Author Share Posted August 18, 2012 Yes it does, however its an RSS feed for each warning region. Would it still work? Quote Link to comment Share on other sites More sharing options...
davefootball123 Posted August 18, 2012 Author Share Posted August 18, 2012 One other thing. How could I replace file_get_contents with cUrl? Quote Link to comment Share on other sites More sharing options...
Christian F. Posted August 18, 2012 Share Posted August 18, 2012 The PHP manual has some very good examples on how to use cURL, along with all of the information you'd ever need about the library. Read it, try for yourself, and come back (with code and a detailed description) if you have problems. Quote Link to comment Share on other sites More sharing options...
davefootball123 Posted August 18, 2012 Author Share Posted August 18, 2012 OK, thanks Christian Quote Link to comment Share on other sites More sharing options...
davefootball123 Posted August 18, 2012 Author Share Posted August 18, 2012 OK, I've done a little bit of work, however I continue to have some issues. I believe I changed to cURL properly...however I think strpos is having some trouble reading $search_string because when the condition is met...the overlay doesnt show up. When I set the ! == TRUE...the overlays will always show up. If i set it to false the overlays never show up even when they should. Here is the code <?php $url = 'http://www.weatheroffice.gc.ca/city/pages/on-147_metric_e.html'; function curl_get_contents($url) { // Initiate the curl session $ch = curl_init('http://www.weatheroffice.gc.ca/city/pages/on-147_metric_e.html'); // 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-147_metric_e.html'); $search_string = 'SEVERE THUNDERSTORM WATCH IN EFFECT</a>'; 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">'; } ?> Quote Link to comment Share on other sites More sharing options...
Christian F. Posted August 18, 2012 Share Posted August 18, 2012 Try to add echo '<pre>';var_dump ($output);die (); after your call to curl_get_contents (), and see what you get. Quote Link to comment Share on other sites More sharing options...
davefootball123 Posted August 18, 2012 Author Share Posted August 18, 2012 It shows the whole webpage on my page...without any CSS of course. I need it to grab that data but only look for 1 thing...the $search_string variable. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted August 18, 2012 Share Posted August 18, 2012 OK, at least we know that the cURL request works. Now you only need to figure out why the strpos () call fails. I suspect it has something to do with the </a> at the end of the search string. Quote Link to comment Share on other sites More sharing options...
davefootball123 Posted August 18, 2012 Author Share Posted August 18, 2012 I will try a few things to see if it helps. I will report back in a minute. Â OK, I tried the full link, didnt work, tried no link and just text didnt work. Still shows the full web page. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted August 18, 2012 Share Posted August 18, 2012 Ah, yeah... The snippet with var_dump () I gave above was just to debug the process, to check that the cURL request was working. You can delete it now. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.