title: PHP script to check for broken links on a page
body: <p>I have been trying to get this script to work for literally days and I think that I'm almost there but it isn't quite working. My understanding of PHP is limited and I have hacked this script together from a lot of other scripts.</p>
  
  <p>I have separated the script in to two parts. Each of these parts work when run on their own but when they are placed together the page never loads.</p>
  
  <p>The first part checks a URL for any external links ignoring any nofollow links.</p>
  
  <p>The second part checks the server response header for a url</p>
  
  <p>The whole script should find any external, followed links on a web page and then check if any of links are broken.</p>
  
  <p>Any help to get this working would be very much appreciated.</p>
  
  <pre><code>&lt;?php
  
  // This is the first part of the script to get a list of external links from a web page ignoring any nofollow links
  
  // Set the parent URL
  $url = 'http://www.example.com';
  $pUrl = parse_url($url);
  
  // Load the HTML into a DOMDocument
  $doc = new DOMDocument;
  @$doc-&gt;loadHTMLFile($url);
  
  // Look for all the 'a' elements
  $links = $doc-&gt;getElementsByTagName('a');
  
  
  $numLinks = 0;
  foreach ($links as $link) {
  
  // Exclude if not a link or has 'nofollow'
  preg_match_all('/\S+/', strtolower($link-&gt;getAttribute('rel')), $rel);
  if (!$link-&gt;hasAttribute('href') || in_array('nofollow', $rel[0])) {
      continue;
  }
  
  // Exclude if internal link
  $href = $link-&gt;getAttribute('href');
  
  if (substr($href, 0, 2) === '//') {
      // Deal with protocol relative URLs as found on Wikipedia
      $href = $pUrl['scheme'] . ':' . $href;
  }
  
  $pHref = @parse_url($href);
  if (!$pHref || !isset($pHref['host']) ||
      strtolower($pHref['host']) === strtolower($pUrl['host'])
  ) {
      continue;
  }
  
  // Increment counter otherwise
  echo $link-&gt;getAttribute('href') . " - ";
  $numLinks++;
  
  
  
  // This is the second part of the script to check to see if the link returns no response or a 404 response.
  
  // Reset $checkurl
  $checkurl = '';
  
  // Set the URL to check server response code
  $checkurl = $link-&gt;getAttribute('href');
  
  
  // Check header response for URL
  file_get_contents($checkurl);
  $response = $http_response_header[0];
  
  // If 404 exists in response then set as 404
  if (strpos($response,'404') !== false) {
  $server_response = '404';
  }
  
  // If there is no response then set as 404
  if ($response == '') {
  $server_response = '404';
  }
  
  echo $server_response;
  echo '&lt;br&gt;';
  
  } 
  
  ?&gt;
  </code></pre>