【发布时间】:2016-01-24 12:07:59
【问题描述】:
我有一个 php 文件,它从另一个站点获取一个 xml 文件,然后将该信息放入我的数据库中。
我遇到的问题是他们的网站在任何 1 小时内只允许 360 个请求,因此我尝试对其进行编码以在获取文件时检查标题信息。
我让它检查页面的状态使用
$requesttest = 'http://www.footballwebpages.co.uk/teams.xml';
if($requesttest == NULL) return false;
$ch = curl_init($requesttest);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($httpcode == 429){
return 'Try again later, too many requests recieved.';
} else if($httpcode>=200 && $httpcode<300){
/* run code to grab xml file */
$comps = array ( 0 => 1, /* Premier_League */
1 => 2 /* Championship */
);
$comps_total = count($comps);
$comps_no = 0;
while ($comps_no < $comps_total) {
$url = 'http://www.footballwebpages.co.uk/teams.xml?comp=' . $comps[$comps_no];
$full_list = simplexml_load_file($url);
/* Code for grabbing and storing info from XML */
} else {
return 'Football Web Pages Offline';
}
目前,它会检查主“团队”页面以查看是否已达到请求限制,然后获取比赛集的每个 xml。问题是,如果在第一次检查时,只有 1 个请求可用,当它进入下一个阶段时,它将失败。如何在加载xml文件时查看header信息,而不需要调用页面查看header,然后调用页面抓取xml文件?
如果header code在1调用200到300之间基本就加载xml文件,以免浪费2个请求抢1个xml页面。
【问题讨论】:
-
while ($comps_no < $comps_total) {~ 循环没有增量器 - 它会继续运行......而且你也不会关闭循环 -
是的,我把代码剪掉了,因为它很长:) 完整代码中有一个增量器