0
我有一個php文件,它從另一個站點抓取一個xml文件,然後將該信息卡入到我的數據庫中。加載外部XML文件並在1個電話中獲取html頭信息
我遇到的問題是,他們的網站只允許在任何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爲比賽設置。問題是,如果首次檢查時,只有一個請求可用,當它進入下一階段時,它將失敗。如何在加載xml文件時檢查標題信息,而不必調用頁面來檢查標題,然後調用頁面來獲取xml文件?
在1次調用中,如果頭代碼在200到300之間,基本上會加載xml文件,以免浪費2次請求來抓取1個xml頁面。
'while($ comps_no <$ comps_total){'〜循環中沒有遞增器 - 它將繼續前進和結束......並且您不關閉循環 – RamRaider
是的,我將代碼截取爲相當長:)完整代碼中有一個增量器 – Dean84