So here is the deal, I have a website with just under 1,000 Users. I found an API that pulls Xbox Live Info as an XML File. What I am trying to do is create a Cache System and store this information in my Database.
Currently the script does work, but it pulls the data from the API and stores the data very slowly. I get somewhere around 75 users before the script times out.
I am wondering if there is a better way to go about doing this. I'd like to have the process run quickly and run it as a corn job every 15 minutes or so if possible.
Before I paste my script here is a real basic example:
If you goto this site:
http://duncanmackenzie.net/services/GetXboxInfo.aspx?GamerTag=Festive+Turkey
I am parsing the following:
<Valid>true</Valid>
<Info>Last seen 12 hours ago playing Xbox 360 Dashboard</Info>
<Info2/>
<LastSeen>2008-04-22T04:03:43-05:00</LastSeen>
<Online>false</Online>
<StatusText>Offline</StatusText>
<Title>Xbox 360 Dashboard</Title>
I will save it to my database and use it later on.
Here is my script.
<?php
//ini_set('max_execution_time', 120);
//#!/hsphere/local/bin/php -q
set_time_limit(0);
$host="localhost";
$user="database_username";
$pass="databasepassword";
$database="database_name";
/* Crontabbed Stats */
function haloapi($contents, $get_attributes=1) {
if(!$contents) return array();
if(!function_exists('xml_parser_create')) {
//print "'xml_parser_create()' function not found!";
return array();
}
//Get the XML parser of PHP - PHP must have this module for the parser to work
$parser = xml_parser_create();
xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, 0 );
xml_parser_set_option( $parser, XML_OPTION_SKIP_WHITE, 1 );
xml_parse_into_struct( $parser, $contents, $xml_values );
xml_parser_free( $parser );
if(!$xml_values) return;//Hmm...
//Initializations
$xml_array = array();
$parents = array();
$opened_tags = array();
$arr = array();
$current = &$xml_array;
//Go through the tags.
foreach($xml_values as $data) {
unset($attributes,$value);//Remove existing values, or there will be trouble
//This command will extract these variables into the foreach scope
// tag(string), type(string), level(int), attributes(array).
extract($data);//We could use the array by itself, but this cooler.
$result = '';
if($get_attributes) {//The second argument of the function decides this.
$result = array();
if(isset($value)) $result['value'] = $value;
//Set the attributes too.
if(isset($attributes)) {
foreach($attributes as $attr => $val) {
if($get_attributes == 1) $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
/** :TODO: should we change the key name to '_attr'? Someone may use the tagname 'attr'. Same goes for 'value' too */
}
}
} elseif(isset($value)) {
$result = $value;
}
//See tag status and do the needed.
if($type == "open") {//The starting of the tag '<tag>'
$parent[$level-1] = &$current;
if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
$current[$tag] = $result;
$current = &$current[$tag];
} else { //There was another element with the same tag name
if(isset($current[$tag][0])) {
array_push($current[$tag], $result);
} else {
$current[$tag] = array($current[$tag],$result);
}
$last = count($current[$tag]) - 1;
$current = &$current[$tag][$last];
}
} elseif($type == "complete") { //Tags that ends in 1 line '<tag />'
//See if the key is already taken.
if(!isset($current[$tag])) { //New Key
$current[$tag] = $result;
} else { //If taken, put all things inside a list(array)
if((is_array($current[$tag]) and $get_attributes == 0)//If it is already an array...
or (isset($current[$tag][0]) and is_array($current[$tag][0]) and $get_attributes == 1)) {
array_push($current[$tag],$result); // ...push the new element into that array.
} else { //If it is not an array...
$current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value
}
}
} elseif($type == 'close') { //End of tag '</tag>'
$current = &$parent[$level-1];
}
}
return($xml_array);
}
$connection=mysql_connect("$host", "$user", "$pass") or die("Failed to connect to mysql server");
mysql_select_db("$database",$connection) or die("Database Error: Could Not Connect");
$q=mysql_query("SELECT user_id,user_username FROM se_users");
while($row=mysql_fetch_array($q)) {
/* Get the stats from bungie ere */
$timestamp=time();
$page = file_get_contents("http://duncanmackenzie.net/services/GetXboxInfo.aspx?GamerTag=".urlencode(trim($row['user_username'])));
$stats = haloapi($page,0);
//echo $row['user_username']."<br/>";
if(!$stats) {
break; /* To Break Out Of The Script and not kill the cpu in a loop!! */
}
//mysql_query();
$update="INSERT INTO `xbl_status`
(
`user_id`,
`gamertag`,
`valid`,
`info`,
`info2`,
`status_text`,
`country`,
`timestamp`
)
VALUES
(
'".$row['user_id']."',
'".$stats['XboxInfo']['Gamertag']."',
'".$stats['XboxInfo']['PresenceInfo']['Valid']."',
'".$stats['XboxInfo']['PresenceInfo']['Info']."',
'".$stats['XboxInfo']['PresenceInfo']['Info2']."',
'".$stats['XboxInfo']['PresenceInfo']['StatusText']."',
'".$stats['XboxInfo']['Country']."',
'".$timestamp."'
)
";
mysql_query($update);
//mysql_query("UPDATE `se_users` SET `last_update`='$timestamp' WHERE use_id='$row[user_id]'");
}
?>
I also attached the database structure, just in case. With a sample a sample user.