插件47:网页更新

<?php // Plug-in 47: Page Updated

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

$page     = "http://pluginphp.com";
$datafile = "urldata.txt";
$result   = PIPHP_PageUpdated($page, $datafile);
echo      "<pre>(1st call) The URL '$page' is ";

if     ($result == -1) echo "New";
elseif ($result == 1)  echo "Changed";
elseif ($result == 0)  echo "Unchanged";
else                   echo "Inaccessible";

$result   = PIPHP_PageUpdated($page, $datafile);
echo      "<br />(2nd call) The URL '$page' is ";

if     ($result == -1) echo "New";
elseif ($result == 1)  echo "Changed";
elseif ($result == 0)  echo "Unchanged";
else                   echo "Inaccessible";

function PIPHP_PageUpdated($page, $datafile)
{
   // Plug-in 47: Page Updated
   //
   // This plug-in takes a URL as an argument which it then
   // checks to see if it is different to the last time it
   // was loaded. If so it returns 1, otherwise it returns 0
   // if the page is unchanged, -1 if the page is new to
   // the data file, or -2 if the page could not be loaded.
   // The arguments required are:
   //
   //    $url:      URL of a page to check
   //    $datafile: File in which to store the database

   $contents = @file_get_contents($page);
   if (!$contents) return FALSE;

   $checksum = md5($contents);

   if (file_exists($datafile))
   {
      $rawfile  = file_get_contents($datafile);
      $data     = explode("\n", rtrim($rawfile));
      $left     = array_map("PIPHP_PU_F1", $data);
      $right    = array_map("PIPHP_PU_F2", $data);
      $exists   = -1;

      for ($j = 0 ; $j < count($left) ; ++$j)
      {
         if ($left[$j] == $page)
         {
            $exists = $j;
            if ($right[$j] == $checksum) return 0;
         }
      }

      if ($exists > -1)
      {
         $rawfile = str_replace($right[$exists],
            $checksum, $rawfile);
         file_put_contents($datafile, $rawfile);
         return 1;
      }
   }
   else $rawfile = "";

   file_put_contents($datafile, $rawfile .
      "$page!1!$checksum\n");

   return -1;
}

// The two functions below are used exclusively by the main
// function and are not intended to be called by your programs

function PIPHP_PU_F1($s)
{
   list($a, $b) = explode("!1!", $s);
   return $a;
}

function PIPHP_PU_F2($s)
{
   list($a, $b) = explode("!1!", $s);
   return $b;
}

?>

插件说明:

插件接受一个web页面的URL地址,告诉我们这个网页是否发生变化。如果已经发生变化,返回1,没有发生变化,返回0,如果他是一个新网页,数据文件里还没有它的记录,则返回-1,如果这个网页无法访问,则返回-2,它需要以下参数:

$page 需要检查网页的URL地址

$datafile 数据文件名

urldata.txt内容:

http://pluginphp.com!1!e06e60bff424f1033034c420869d6bfd

你可能感兴趣的:(function,list,File,url,database,download)