0

I am trying to figure out a php script to check a bunch of links from a separate text file and if anyone of them is showing 404 then the script should trigger a php file, which will send sms notification. I already have sms file ready and just need to trigger it.

For example, there are several links in links.txt (which is also uploaded on the server), such as

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

These links are not necessarily offline, they may be redirecting to a non-existent page, while the main site is alive.

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

The reference to smsnotice1.php, smsnotice2.php, smsnotice3.php can be either in the main script or another php file.

If none of the links is down, then no notice should be sent.

I can run this script from a php server on a cron for frequent check. Thank you very much for the help!

4

3 回答 3

2
  1. 使用cURL请求页面的副本。
  2. 检查返回的 HTTP 状态$http_status = curl_getinfo($http, CURLINFO_HTTP_CODE);
  3. 如果是 200 以外的任何值,请向自己发送警报。

至于如何发送短信,几乎所有移动提供商都有一个电子邮件域,您可以使用它向您的手机发送短信,即:1235555555@mymobile.tld

于 2013-01-21T16:04:58.453 回答
1
  1. 打开你的文本文件file_get_contents()
  2. 对于每个 URL,执行get_headers()
  3. 检查$headers[0]包含200 OK
于 2013-01-21T16:05:42.063 回答
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';}

?>
于 2013-03-27T10:34:01.003 回答