2013-01-21 51 views
0

我想弄清楚一个PHP脚本来检查从一个单独的文本文件的链接一堆,如果他们中的任何人显示404然后脚本应该触发一个PHP文件,它会发送短信通知。我已经准备好了短信文件,只需触发即可。检查链接是否404,如果是,然后发送短信

例如,有在links.txt几个链接(也上载到服务器上),如

http://example.com/link1 
http://example.com/link2 
http://example.com/link3 

这些链接不一定离线,它们可以被重定向到一个不存在的页面,而主要网站是活着的。

If example.com/link1 is down smsnotice1.php should be triggered 
If example.com/link2 is down smsnotice2.php should be triggered 
If example.com/link3 is down smsnotice3.php should be triggered 

的参考smsnotice1.php,smsnotice2.php,smsnotice3.php可以是在主脚本或另一个PHP文件。

如果没有链接关闭,则不应发送通知。

我可以从一台cron上的php服务器上运行这个脚本进行频繁检查。 非常感谢您的帮助!

+3

[你有什么尝试?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) –

+0

为什么不从[HTTP GET](http:// in1 .php.net/manual/en/function.http-get.php)?请尝试! – SparKot

+0

用户'curl'发出请求,只返回头文件并对其进行评估。 – arkascha

回答

2
  1. 使用cURL要求页面的副本。
  2. 检查返回的HTTP状态与$http_status = curl_getinfo($http, CURLINFO_HTTP_CODE);
  3. 如果它不是200以外的任何东西发送警报。

至于如何发送短信,几乎所有的移动运营商有一个电子邮件域,你可以用它来短信发送到您的手机,即:[email protected]

+0

+1使用cURL – Tchoupi

+0

为什么建议通过PHP HTTP函数进行cURL扩展? – SparKot

+1

@SparKot因为PHP没有体面的内置HTTP功能。当然,没有任何回馈代码。 – Sammitch

1
  1. 打开与file_get_contents()
  2. 对于每个URL文本文件,做一个get_headers()
  3. 检查$headers[0]包含200 OK
+0

通过将URL传递到get_headers(),您可以在不执行file_get_contents()的情况下一步完成此操作。例如:if(get_headers($ url)[0] =='HTTP/1.1 404 Not Found'){//发送文本} – Levi

+0

@Levi您需要先解析包含链接的文件'links.txt' – Tchoupi

+0

Mathieu,我的道歉,在阅读这个问题时,我完全错过了这一行。 – Levi

1
<?php 
$url="http://site.com"; 
$handle = curl_init($url); 
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE); 

/* Get the HTML or whatever is linked in $url. */ 
$response = curl_exec($handle); 
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE); 
/* Check for 404 (file not found). */ 

if ($httpCode == '404') 
     { echo 'yes';} 
else  
     {echo 'no';} 

?>