gpt4 book ai didi

PHP 简单 HTML DOM 解析器 : make it loop until no error

转载 作者:行者123 更新时间:2023-11-28 00:20:33 26 4
gpt4 key购买 nike

我有一个名为 GrabUrTime 的应用程序,它是一个时间表查看实用程序,可以从另一个站点(我大学的网站空间)获取时间表。每隔凌晨 2 点,我都会运行一个脚本,使用解析器抓取所有时间表并将其转储到我的数据库中。

但是今天 uni 的服务器运行不正常,我的脚本在 uni 的服务器上一直给我错误 500,导致脚本无法继续运行。它是周期性的,并非总是如此。但是我尝试了几次,它只是随机出现,根本没有规律。

因此我想让我的脚本来处理错误并让它循环直到它获取数据。

function grabtable($intakecode, $week) {
$html = file_get_html("http://webspace.apiit.edu.my/schedule/intakeview_intake.jsp?Intake1=".$intakecode."&Week=" . $week);
$dumb = $html->find('table[border=1] tr');
$thatarray = array();
for ($i=1; $i < sizeof($dumb);++$i){
$arow = $html->find('table[border=1] tr', $i);
$date = $arow->find('td font', 0)->innertext;
$time = $arow->find('td font', 1)->innertext;
$room = $arow->find('td font', 2)->innertext;
$loca = $arow->find('td font', 3)->innertext;
$modu = $arow->find('td font', 4)->innertext;
$lect = $arow->find('td font', 5)->innertext;
$anarray = array($date, $time, $room, $loca, $modu, $lect);
$thatarray[$i] = $anarray;

//echo "arraylol";
}
//echo serialize($tablearray)."<br/>";
$html->clear();
return $thatarray;
}

最佳答案

尝试这样的事情:

function getHttpCode($url)
{
$agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
$ch=curl_init();
curl_setopt ($ch, CURLOPT_URL,$url );
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch,CURLOPT_VERBOSE,false);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$page=curl_exec($ch);

//echo curl_error($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if($httpcode>=200 && $httpcode<300)
{
// YOUR CODE
}
else
{
// What you want to do should it fail
// perhaps this will serve you better as while loop, e.g.
// while($httpcode>=200 && $httpcode<300) { ... }
}

用法

 getHttpCode($url);

它可能无法按原样整齐地插入您的代码,但我相信它可以帮助进行一些重构以适应您现有的代码结构。

关于PHP 简单 HTML DOM 解析器 : make it loop until no error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9274230/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com