mysql查url,从MySQL表自动检查URL

Okay, I have a list of URLs in a MySQL table. I want the script to automatically check each link in the table for 404, and afterward I want it to store whether the URL was 404'd or not, as well as store a time for last checked.

Is this even possible to do automatically, even if no one runs the script? ie, no one visits the page for a few days, but even with no one visiting the page, it automatically ran the test.

If its possible, how could I go about making a button to do this?

解决方案

No need to use CURL, file_get_contents($url); will return false if the request fails (any other HTTP code other than 2xx), which might be more useful for what you're trying to do, an example:

function urlExists($url)

{

return (bool) @file_get_contents($url);

}

Will return true if the URL returns useful content, false otherwise.

EDIT: Here is a faster way (it only requests the headers) and the first byte instead of the whole page:

function urlExists($url)

{

return (bool) @file_get_contents($url, false, null, 0, 1);

}

urlExists('https://stackoverflow.com/iDontExist'); // false

However, in combination with your other question it may be wiser to use something like this:

function url($url)

{

return @file_get_contents($url);

}

$content = url('https://stackoverflow.com/');

// request has failed (404, 5xx, etc...)

if ($content === false)

{

// delete or store as "failed" in the DB

}

// request was successful

else

{

$hash = md5($content); // md5() should be enough but you can also use sha1()

// store $hash in the DB to keep track of changes

}

Or if you're using PHP 5.1+ you only have to do:

$hash = @md5_file($url);

$hash will be false when the URL fails to load, otherwise it will return the MD5 hash of the contents.

This way you only have to make one request instead of two. =)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值