<?php
/**
*
* @author Matt Johnston DEC 2009
* @todo implement last 3 games played probably clean up the code a little
* @version 0.1
*
* /----------------------VARIABLE NAMES TO USE----------------------\
* | total_trophies | users total trophies |
* | bronze | users bronze trophies |
* | silver | users silver trophies |
* | gold | users gold trophies |
* | platinum | users platinum trophies |
* | level | users trophy level |
* | progress | users progress to next level (in percent form) |
* \-----------------------------------------------------------------/
*
*/
class PSNCard
{
protected $psnName;
protected $psnHTML;
protected $psnURL;
protected $pregArray;
protected $valuesToLoad;
protected $dom;
function __construct($psnName) {
$this->psnName = $psnName; //PSN username
$this->psnURL = 'http://profiles.us.playstation.com/playstation/psn/profiles/'
. $this->psnName; //PSN stats URL to parse
$this->pregArray = array('/Bronze/', '/Silver/', '/Gold/', '/Platinum/', '/text/',
'/\%/', '/\s/'); //What values we need to replace in the data
$this->valuesToLoad = array('text', 'leveltext', 'progresstext', 'text bronze',
'text silver', 'text gold', 'text platinum');
//^^The name of the div classes/ids that we need to get data from^^
/* We have to use CURL because file_get_contents won't work on the PSN URL */
$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, $this->psnURL);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$this->psnHTML = curl_exec($ch);
curl_close($ch);
/* END CURL */
//Set up our DOM and load the HTML
$this->dom = new DOMDocument();
@$this->dom->loadHTML($this->psnHTML);
$this->__parse(); //Time to parse!
}
protected function __parse() {
//Basically, just grabs all the DIV elements for us to go through
foreach ($this->dom->getElementsByTagName('div') as $element) {
//Go through all of the divs and we'll check through their class values
foreach ($element->attributes as $key => $node) {
/*
* Here we'll just run through all the values we need to load instead of having
* to do multiple if statements
*/
foreach ($this->valuesToLoad as $value) {
/*
* if the value of the class or the id matches what we need to load
* then that's some data we are looking for!
*/
if ($element->getAttribute($key) == $value) {
/*
* varName is the name of the variable we will store in our class so that we
* can provide one step access to all the data for instance we will create a
* new class variable called "silver" so that all you have to do is access that
* variable to get the value of your silver trophies. Sorry if thats a little
* confusing.
*/
$varName = $value == "text" ? "total_trophies" : preg_replace($this->pregArray, '', $value);
//Just set our class variable with the data from the current element
$this->$varName = preg_replace($this->pregArray, '', $element->nodeValue);
}
}
}
}
/*
* We return $this so that the method is chainable and we could essentially call
* $var = new PSNCard();
* echo $var->silver;
* That would echo the silver trophies the user has.
*/
return $this;
}
}