插件41:检查链接

<?php // Plug-in 41: Lookup Links (Formerly Check Links)

// This is an executable example with additional code supplied
// To obtain just the plug-ins please click on the Download link

$url   = "http://pluginphp.com";
$links = array("http://pluginphp.blogspot.com",
               "http://lpmj.net",
               "http://doesnotexist.com/index.html");

echo "<b>Verifying '$url' for these links:</b><br /><br />";
for ($j = 0 ; $j < count($links) ; ++$j)
   echo $links[$j] . "<br />";

$result = PIPHP_LookupLinks($url, $links);
if ($result[0]) echo "<br /><b>All links verified</b>";
else
{
   echo "<br /><b>Verification failed for:</b><br /><br />";
   
   for ($j = 0 ; $j < count($result[1]) ; ++$j)
      echo $result[1][$j] . "<br />";
}

function PIPHP_LookupLinks($url, $links)
{
   // Plug-in 41: Lookup Links
   //
   // Note: This initially had the function name of PIPHP_
   // CheckLinks(), but that clashed with the name of function 23
   // so this one is now called PIPHP_LookupLinks().
   //
   // This plug-in takes the URL of a page to check, along
   // with an array of links that should appear at that URL.
   // It returns an array with the value TRUE if all links
   // are in place, otherwise it returns a two element array
   // the first of which is FALSE and the second is an array
   // of all missing links. The arguments required are:
   //
   //    $url:   URL of a web page to check
   //    $links: Array of links to verify

   $results = PIPHP_GetLinksFromURL($url);
   $missing = array();
   $failed  = 0;
   
   foreach($links as $link)
      if (!in_array($link, $results))
         $missing[$failed++] = $link;
         
   if ($failed == 0) return array(TRUE);
   else return array(FALSE, $missing);
}

function PIPHP_GetLinksFromURL($page)
{
   // Plug-in 22: get Links From URL
   //
   // This plug-in accepts the URL or a web page and returns
   // an array of all the links found in it. The argument is:
   //
   //    $page: The web site's main URL

   $contents = @file_get_contents($page);
   if (!$contents) return NULL;
   
   $urls    = array();
   $dom     = new domdocument();
   @$dom    ->loadhtml($contents);
   $xpath   = new domxpath($dom);
   $hrefs   = $xpath->evaluate("/html/body//a");

   for ($j = 0 ; $j < $hrefs->length ; $j++)
      $urls[$j] = PIPHP_RelToAbsURL($page,
         $hrefs->item($j)->getAttribute('href'));

   return $urls;
}

// The below function is repeated here to ensure that it's
// available to the main function which relies on it

function PIPHP_RelToAbsURL($page, $url)
{
   // Plug-in 21: Relative To Absolute URL
   //
   // This plug-in accepts the absolute URL of a web page
   // and a link featured within that page. The link is then
   // turned into an absolute URL which can be independently
   // accessed. Only applies to http:// URLs. Arguments are:
   //
   //    $page: The web page containing the URL
   //    $url:  The URL to convert to absolute

   if (substr($page, 0, 7) != "http://") return $url;
   
   $parse = parse_url($page);
   $root  = $parse['scheme'] . "://" . $parse['host'];
   $p     = strrpos(substr($page, 7), '/');
   
   if ($p) $base = substr($page, 0, $p + 8);
   else $base = "$page/";
   
   if (substr($url, 0, 1) == '/')           $url = $root . $url;
   elseif (substr($url, 0, 7) != "http://") $url = $base . $url;
   
   return $url;
}

?>

插件说明:

插件41接受一个需要检查链接的网页URL地址和一组需要且必须出现在此网页上的链接地址。如果这些链接都出现在此网页上,则返回一个值为TRUE的数组,否则返回一个两元素的数组。其中第一个元素的值为FALSE,第二个元素保存了所有没有出现在此网页上的链接地址。这个插件具体参数如下:

$url 字符串,表示需要检查的网页的URL地址。

$links 数组,出现在$url页面上的链接地址

你可能感兴趣的:(Web,function,Scheme,null,url,download)