我设计了一个网站,从其他网站的RSS获取新闻并将其保存到我的数据库中。为此,我必须将请求发送到另一个站点并将更改保存到我的数据库。 这是我发送请求的代码:
$q = $d->query("select * from site_setting where id=1");
$setting = $d->fetch($q);
$start = $setting['last'];
$q = $d->query("select *from newsagency order by id limit ".$start.",1");
while($agency = $d->fetch($q)){
$url = $agency['link'];
$size = 20;
$agencyId = $agency['id'];
$time = time();
RSS_RetrieveLinks($url);
if($size > 0)
$recents = array_slice($RSS_Content, 0, $size + 1);
$MaxTime = $d->getrowvalue("time","select `time` from news where agency={$agencyId} order by time DESC limit 0,1",true);
foreach($recents as $article)
{
$articleTime = strtotime($article["date"]);
if($articleTime > ($MaxTime+1)){
$d->iquery("news",array(
'title'=>htmlspecialchars($article["title"]),
'time'=>$articleTime,
'link'=>$article["link"],
'type'=>$article["type"],
'img'=>$article["media"],
'regTime'=>$time,
'agency'=>$agencyId
));
$return .= $article['title']."<br>";
}
}
}
$last = $start +1;
$agencyCount = $d->getrowvalue("agenC","select count(id) as agenC from newsagency",true);
$agencyCount--;
if($last>$agencyCount) $last = 0;
$d->uquery("site_setting",array('last'=>($last)),"id=1");
此代码从db逐个获取rss链接,然后从rss获取数据并将其保存到db,并将所有内容保存在OK中。要刷新php页面,我们有这样的代码:
<script type="text/javascript">
$(document).ready(function() {
$.ajaxSetup({ cache: false }); // This part addresses an IE bug. without it, IE will only load the first number and will never refresh
setInterval(function() {
$.ajax({
url: "load.php",
context: document.body,
success: function(vars){
document.getElementById("imge").innerHTML+=vars;
}
})
}, 500);
});
</script>
</head>
<body dir="rtl">
<div id="imge"></p>
</body>
我每500毫秒刷新一次这段代码并从rss获取内容。问题是这个页面必须永远打开,我不知道要解决这个问题。如何在不让客户端计算机忙的情况下发送此请求?