Re: Test links?

From: Date: Sat, 30 Nov 2002 19:02:38 +0000
Subject: Re: Test links?
References: 1 2 3 4 5  Groups: php.general 
Request: Send a blank email to php-general+get-126560@lists.php.net to get a copy of this message
Rob Packer wrote:
Okay, I'm confused... file, fopen, and fsockopen seem to say not found on alot valid URLs... does this look to be correct usage? $url = $row[0]; // just get the url from the db $fp = implode ('', file ($url)); if (!$fp) {echo "<font color=red><b>Unable to access file</b></font>"; } else { fclose($fp); echo "The link is working!"; } It seems I always get this warning... Warning: Supplied argument is not a valid File-Handle resource in /web/home/nrc.net/www/robert/links4.php on line 11 If someone can tell me what I'm doing wrong, I'd appreciate it. Thanks, Robert When fopen successfully opens a file it populates an array $http_response_header, which you can examine to see if the link works or not - I don't believe you can actually get the file itself, but since that's not what we're after that's not a problem!
Having said that, fopen produces a very annoying error if it doesn't find the page, but I found the following works: <?php $SQL = "SELECT linkurl, linkID FROM links"; $result = mysql_query($SQL); while($linkdata = mysql_fetch_array($result)){
       $fp = fopen($linkdata['linkurl'],"r");
       if($fp)          {
           echo "<p>".$linkdata['linkurl']." is still valid.</p>";
                 }else{
       echo "<p>".$linkdata['linkurl']." is invalid. Updating database... ";
             $SQL = "UPDATE links SET status = 0 WHERE linkID = '".$linkdata['linkID']."'";
               $result2 = mysql_query($SQL);
           if($result2)
               {
                   echo "Database updated</p>";
               }
       }
} ?> Rather than deleting the link, it's probably better to set a flag to show it was invalid last time you checked, but check it again next time. Or you could keep a count of the number of failed attempts, and delete if it goes beyond 3 or so. Not sure how to supress the warning message that PHP automatically does when you haven't got a valid URL though. Hope this works for you! Beth Gore -- http://bethanoia.dyndns.org

« previous php.general (#126560) next »