11.14. Program: Finding Stale Links
The stale-links.php program in Example 11-5 produces a list of links in a page and their status. It tells you if the links are okay, if they’ve been moved somewhere else, or if they’re bad. Run the program by passing it a URL to scan for links:
% stale-links.php http://www.oreilly.com/
http://www.oreilly.com/index.html: OK
http://www.oreillynet.com: OK
http://conferences.oreilly.com: OK
http://international.oreilly.com: OK
http://safari.oreilly.com: MOVED: mainhom.asp?home
...The stale-links.php program uses the
cURL
extension to retrieve web pages. First, it retrieves the URL
specified on the command line. Once a page has been retrieved, the
program uses the pc_link_extractor( )
function from Recipe 11.9 to get a list of links in the page. Then,
after prepending a base URL to each link if necessary, the link is
retrieved. Because we need just the headers of these responses, we
use the
HEAD
method instead of GET by setting the
CURLOPT_NOBODY option. Setting
CURLOPT_HEADER tells curl_exec( ) to include the response headers in the string it returns.
Based on the response code, the status of the link is printed, along
with its new location if it’s been moved.
Example 11-5. stale-links.php
function_exists('curl_exec') or die('CURL extension required'); function pc_link_extractor($s) { $a = array(); if (preg_match_all('/<A\s+.*?HREF=[\"\']?([^\"\' >]*)[\"\']?[^>]*>(.*?)<\/A>/i', $s,$matches,PREG_SET_ORDER)) { foreach($matches as $match) { array_push($a,array($match[1],$match[2])); ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access