Php 链接检查器-无效链接的邮件
我得到了这个链接检查脚本,我想它给我一个邮件时,链接不工作。 我需要它记住,它给我发送了一封关于链接的电子邮件,这样我就不会收到关于同一链接的多封电子邮件 如果有人帮我做这件事,我会同情的,因为这对我来说太难了Php 链接检查器-无效链接的邮件,php,hyperlink,Php,Hyperlink,我得到了这个链接检查脚本,我想它给我一个邮件时,链接不工作。 我需要它记住,它给我发送了一封关于链接的电子邮件,这样我就不会收到关于同一链接的多封电子邮件 如果有人帮我做这件事,我会同情的,因为这对我来说太难了 <? function checklink($links) { $working = 0; $link = $links; $links = preg_replace('/\s+/', '', $links); if(strstr($links,"
<?
function checklink($links) {
$working = 0;
$link = $links;
$links = preg_replace('/\s+/', '', $links);
if(strstr($links,"rapidshare.com")){
$var = file_get_contents($links);
if(strpos($var,"You want to download the file ")) {
$working = 1;
}
}
elseif (strstr($links,"megaupload.com")) {
$var1 = file_get_contents($links);
if(strpos($var1,"Please enter")) {
$working = 1;
}
}
elseif (strstr($links,"megashares.com")) {
$var2 = file_get_contents($links);
if(strpos($var2,"Filename:")) {
$working = 1;
}
}
elseif (strstr($links,"sendspace.com")) {
$var3 = file_get_contents($links);
if(strpos($var3,"404 Page Not Found")) {
$working = 0;
}
elseif(strpos($var3,"Sorry, the file you requested is not available.")){
$working = 0;
}
else {
$working = 1;
}
}
elseif(strstr($links,"rapidshare.de")) {
$var5 = file_get_contents($links);
if(strpos($var,"You want to download the file ")){
$working = 1;
}
}
elseif (strstr($links,"mediafire.com")) {
$var4 = file_get_contents($links);
if(strpos($var4,"Sharing")) {
$working = 1;
}
}
if ($working == 1) {
echo "<a href=\"". $link . "\" target=\"_blank\">". $link . "</a>";
}
else {
echo "The link is not working. Please let me know about it and I'll fix it.";
}
}
?>
我认为最好的方法是收集链接并将其存储在数据库表中
然后有一个系统,通过链接和检查,如果它工作,它会将其标记为工作链接,如果它不工作,它会将其标记为断开的链接,并向您发送电子邮件
然后,您必须检查数据库中是否存在链接(因为您不能使用mysql的varchar作为唯一项,因为它的最大值为255,链接可能更长)
如果在数据库中,则检查扫描结果
顺便说一句,您使用文件内容的方式是一个缓慢的过程。因为它下载整个页面。我建议使用我同意Olafur,但是如果您没有访问数据库的权限,您可以使用服务器的文件系统将URL的统计信息保存在组合的配置/日志文件中,如逗号分隔的文件。假设您有这样一个文件:
rapidshare.com,You want to download the file,0,0
megaupload.com,Please enter,0,0
megashares.com,Filename:,0,0
$file = "myfile.txt";
// open the file
$fh = fopen($filename, "r");
// read the full file contents into a string
$contents = fread($fh, filesize($file));
// close the file
fclose($fh);
// split the string into an array of lines
$lines = split("\n", $contents);
// split each line into its fields for processing
$i = 0;
foreach ($lines as $line) {
$checkarray[$i] = split(",", $line);
$i++;
}
这四个字段是“URL”、“要检查的文本”、“上次检查结果”和“已发送邮件”。代码可以是这样的:
rapidshare.com,You want to download the file,0,0
megaupload.com,Please enter,0,0
megashares.com,Filename:,0,0
$file = "myfile.txt";
// open the file
$fh = fopen($filename, "r");
// read the full file contents into a string
$contents = fread($fh, filesize($file));
// close the file
fclose($fh);
// split the string into an array of lines
$lines = split("\n", $contents);
// split each line into its fields for processing
$i = 0;
foreach ($lines as $line) {
$checkarray[$i] = split(",", $line);
$i++;
}
现在,您可以在数组中循环并执行任何您想要的操作,在执行过程中写回包括“mail sent”状态字段在内的信息。使用$fields[0]作为URL,使用$fields[1]作为要检查的文本,您可以使用$fields[2]读取最后状态,使用$fields[3]读取“已发送邮件”状态
foreach($checkarray as $fields) {
// insert code to do your checks here
...
// write back the results
$fh = fopen($filename, "w");
fwrite($fh, $fields[0] . "," . $fields[1] . "," . $working . "," . $mailsent . "\n";
fclose($fh);
}
希望这对您有所帮助。这是您需要的代码:
function StatusCheck($url)
{
$urlparts=parse_url($url);
$curl=new CCurl($url);
$headers=$curl->execute();
$headers=$curl->close();
$headers=$curl->getHeader();
$headers=split("\r\n",$headers);
$status=$headers[0];
print_r($headers);
if (strpos($status,"HTTP/1.1 200 OK")===FALSE)
{
echo date("d.m.Y H:i:s").$url,': bad'."\n";
return 0;
}
else
{
echo date("d.m.Y H:i:s").$url,': good'."\n";
return 1;
}
}
它检查提供的URL(链接),如果URL不好(不工作)或好(状态200 OK),则打印出标题+信息
PS:设置卷曲选项以跟随重定向
编辑:这是CCurl类,很抱歉忘记了:
class CCurl {
var $m_handle;
var $m_header;
var $m_body;
function CCurl($sUrl) {
$this->m_handle = curl_init();
curl_setopt($this->m_handle, CURLOPT_URL, $sUrl);
curl_setopt($this->m_handle, CURLOPT_HEADER, 1);
curl_setopt($this->m_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->m_handle, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($this->m_handle, CURLOPT_USERAGENT, "StatusCheckBot 0.1");
return;
}
function getHeader() {
return $this->m_header;
}
function execute() {
$sResponse = curl_exec($this->m_handle);
$this->m_body = substr($sResponse, strpos($sResponse, "\r\n\r\n") + 4);
$this->m_header = substr($sResponse, 0, -strlen($this->m_body));
return $this->m_body;
}
function close() {
curl_close($this->m_handle);
return;
}
}
只是一句评论,没有回答。不要使用0和1作为“布尔”值。像$working==1这样的东西会让你的代码很难看并且不容易阅读。此外,声明$var1、$var2等是在您自己的代码中迷失方向的最佳方式。您介意帮我解决一些问题吗?我遇到了这个错误致命错误:在第5行的/home/pema2201/public_html/downloads/checklinks.php中找不到类“CCurl”