11.12 Getting New Links from a Web Page


You want to get the latest links for your favorite Web page.

Technique

Save the links in a file, and every time you check the Web page, have the file add new links and report them to you:

 <?php include_once 'Snoopy.class.inc'; $url_list = array(); $snoopy = new Snoopy; $snoopy->fetchlinks('http://www.yahoo.com/'); $links = $snoopy->results; $links = expand_links($links); $fp = @fopen('yahoo_links', 'r'); if (!$fp) {     die('Cannot open yahoo_links in read mode'); } while ($link = @fgets($fp, 1024)) {     $link = trim($link);     if (!in_array($link, $links))         array_push($url_list, $link); } print "<b>New URL's on Yahoo:</b>\n<br>\n"; for ($idx = 0; $idx < count($url_list); $idx++) {     print "<a href=\"$url_list[$idx]\">$url_list[$idx]</a>\n<br>\n"; } @fclose($fp);  $fp = @fopen('yahoo_links', 'w'); if (!$fp) {     die('Cannot open yahoo_links in write mode'); } @fwrite($fp, implode("\n", $links)); @fclose($fp); function expand_links($links, $base_url) {     foreach ($links as $link) {         if (!preg_match('!^([a-z])*\://!i', $link)) {             $link = ($link[0] == '/') ?                     $baseurl . $link  :                     $baseurl . '/' . $link;         }         $ret[] = $link;     }     return($ret); } ?> 

Comments

Creating a list of new links on your Web site can be useful for everything from creating a what's new script to sending an email to yourself every time Yahoo updates its content. To do this, we create a plain text file that keeps track of all the URLs we have already seen. We then extract all the URLs from the current page and put them in the $links array. After we have all the URLs from the page in the $links array, we loop through them. If the link does not already exist in the preexisting link file, we add the link to the $url_list array. We then loop through all the new links (contained in the $url_list array) and print them. Finally, we write the links in the $links array to the link file ( yahoo_links ).



PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net