A-A+
PHP监控网上商城价格的简单脚本
近日打算买一台华为U8860手机,经过观察,网上的新蛋、卓越等各家商城的价格都波动得很厉害,有时2000元以上,有时1500元以下。可是每次降到1500元以下都是转瞬即变回2000元,很难抓住这个机会。于是我写了一个php脚本,用来监控价格,一旦降价立即发邮件通知我。
<?php
header("Content-type: text/html; charset=utf-8");
//从etao抓取价格
$html = file_get_contents('http://s.etao.com/search?q=U8860');
$html = mb_convert_encoding($html, "UTF-8", "GBK");
preg_match_all('/price">([0-9.]{1,})元</span>/i', $html, $m);
foreach($m[1] as $price){
//如果价格低于或等于1700元,就发Email
if($price<=1700){
set_time_limit(30);
echo $price.'<br />';
$to = "xxx@xxx.xxx";
$headers = 'MIME-Version: 1.0' . "rn";
$headers .= 'Content-type: text/html; charset=utf-8' . "rn";
$headers .= "To: $to rn";
$headers .= 'From: ' . $to . "rn";
$subject = "价格:".$price;
$subject = "=?UTF-8?B?".base64_encode($subject)."?=";
$content = "价格:".$price.'<br />'.date("Y-m-d H:i:s").'<br /><a href="http://s.etao.com/search?q=U8860">http://s.etao.com/search?q=U8860</a>';
$result = mail($to, $subject, $content, $headers);
}
}
echo 'OK';
最后,弄一个crontab来定时执行这个脚本就可以了。