I have a php file which grabs an xml file from another site, it then chucks that information into my database.
The problem I am having is that their site only allows 360 requests in any 1 hour period, so am trying to code it to check the header information whilst grabbing the file.
I have it checking the status of the page using
$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';
}
At the moment, it checks the main 'teams' page to see if the requests limit has been reached, and then grabs each xml for the competitions set. The issue is that if when on first check, there is only 1 request available, when it gets to the next stage, it will fail. How can I check the header info when loading the xml file, without having to call the page to check the header, then call the page to grab the xml file?
Basically load the xml file if the header code is between 200 and 300 in 1 call, so as not to waste 2 requests to grab 1 xml page.
You could perhaps employ a method similar to the following, forget the first call to the base url as it is redundant and instead use the return value from the function to determine if further processing should be done: