Created
March 6, 2012 09:29
-
-
Save thinkphp/1985287 to your computer and use it in GitHub Desktop.
notify.me.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| include("simple_html_dom.php"); | |
| //settings on top | |
| $siteToCheck = array( | |
| array("url"=>"http://davidwalsh.name/","selector"=>"div[id='bigShow']"), | |
| array("url"=>"http://www.loto6-49.ro/","selector"=>"div[class='numere_castigatoare_div']"), | |
| array("url"=>"http://christianheilmann.com/","selector"=>"div[id='pad']/div[1]"), | |
| array("url"=>"http://addyosmani.com/blog/","selector"=>"div[id='content']/div[1][class='entry-wrap']/h1[class='entry-title']") | |
| ); | |
| $savePath = "cachedPage33/"; | |
| if(!is_dir($savePath)) { | |
| if(!mkdir($savePath,0777)) { | |
| die('Failed to create folder'); | |
| } | |
| } | |
| $emailContent = ""; | |
| //for every page to check | |
| foreach($siteToCheck as $site) { | |
| $url = $site["url"]; | |
| //calculate the cachedPage name and set oldContent = "" | |
| $fileName = md5($url); | |
| $html = file_get_html($url); | |
| foreach($html->find($site['selector']) as $element) { | |
| $currentContent = $element->innertext; | |
| break; | |
| } | |
| $currentContent = preg_replace("/\n|\t/"," ",$currentContent); | |
| if(file_exists($savePath.$fileName)) { | |
| $oldContent = file_get_contents($savePath.$fileName); | |
| } | |
| if($oldContent && $oldContent != $currentContent) { | |
| //do stuff | |
| //we can tweet to an address | |
| //we can send a simple email | |
| //we can text ourselves | |
| $emailContent .="Adrian, the following page has changed! \n\n" . $url . "\n\n"; | |
| } | |
| file_put_contents($savePath.$fileName,$currentContent); | |
| } | |
| //if we have contents then mail it | |
| if($emailContent) { | |
| $to = "mergesortv@gmail.com"; | |
| $subject = "Sites have changed!"; | |
| $message = " | |
| <h2>Parse Web Pages with PHP Simple HTML DOM</h2> | |
| <p>Armed with the ability to pick content from DOM nodes with PHP, it's time to analyze websites for changes!</p> | |
| <table> | |
| <tr><td>$emailContent</td></tr> | |
| </table>"; | |
| //to send HTML mail must be set Content-type header | |
| $headers = 'MIME-Version: 1.0' . "\r\n"; | |
| $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; | |
| $headers .= 'From: [Reflexive] <adrian@thinkphp.ro>' . "\r\n"; | |
| //mail it | |
| //mail($to, $subject, $message, $headers); | |
| //for debug | |
| echo$emailContent; | |
| } else { | |
| echo"<h2>Everything is Up-to-date</h2>"; | |
| } | |
| ?> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment