gilnd Posted March 30, 2010 Share Posted March 30, 2010 Hey everyone , I'm currently working on a project that involves getting trophy information from the PSN. I have searched the forum and have come up with this thread : http://www.phpfreaks.com/forums/index.php?topic=282638.0 as you can see , I couldn't find the solution I was looking for in that thread. I have tried to use the PSN trophy data 0.2 class but I ended up having these errors: Notice: Undefined property: PSNCard::$images in C:\wamp\www\class.PSNCard.php on line 169 Warning: Invalid argument supplied for foreach() in C:\wamp\www\class.PSNCard.php on line 169 Notice: Undefined variable: gameList in C:\wamp\www\class.PSNCard.php on line 178 Notice: Undefined variable: gameList in C:\wamp\www\class.PSNCard.php on line 183 Notice: Undefined property: PSNCard::$progress in C:\wamp\www\try.php on line 6 /Bronze/ Bronze Trophies the list continues but it is preety much more of the same... here is the class: class PSNCard { protected $psnName; protected $psnHTML; protected $gameListHTML; protected $psnURL; protected $gameListURL; public $pregArray; protected $valuesToLoad; protected $dom; protected $parseGameList; public $gameList; function __construct($psnName, $parseGameList = true) { $this->parseGameList = $parseGameList; /* PSN username */ $this->psnName = $psnName; /* PSN stats URL to parse */ $this->psnURL = 'http://profiles.us.playstation.com/playstation/psn/profiles/' . $this->psnName; /* What values we need to replace in the data */ $this->pregArray = array('/Bronze/', '/Silver/', '/Gold/', '/Platinum/', '/text/', '/\%/', '/\s/'); /* The name of the div classes/ids that we need to get data from */ $this->valuesToLoad = array('text', 'leveltext', 'progresstext', 'text bronze', 'text silver', 'text gold', 'text platinum'); /* 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 */ /* If we need to fetch the games list */ if ($this->parseGameList == true) { /* Game listing URL */ $this->gameListURL = 'http://profiles.us.playstation.com/playstation/psn/profile/' . $this->psnName . '/get_ordered_trophies_data'; /* Need to use CURL */ $ch = curl_init(); $timeout = 5; /* set to zero for no timeout */ curl_setopt ($ch, CURLOPT_URL, $this->gameListURL); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $this->gameListHTML = 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); } } } } /* This may seem like a little much, but I couldn't get the child nodes to work in dom * so I ended up having to do it nitty-gritty. */ if (isset($this->gameListURL)) { /* Load the HTML to parse */ @$this->dom->loadHTML($this->gameListHTML); /* We loop through all the images to get the game image */ foreach ($this->dom->getElementsByTagName('img') as $element) { $this->images[] = $element->getAttribute('src'); } /* We loop through all the span elements */ foreach ($this->dom->getElementsByTagName('span') as $element) { /* We get the value of the gameTitleSortField element, this is the title of the game */ if ($element->getAttribute('class') == 'gameTitleSortField') { /* Because of the html entities we must encode them, replace them, then decode them to * make it look nice */ $this->games[] = urldecode(preg_replace(array('/%26Atilde/', '/%26cent/', '/%26Acirc/', '/%3B/', '/%84/'), '', urlencode(htmlentities($element->nodeValue)))); } } /* We loop through all of the A elements */ foreach ($this->dom->getElementsByTagName('a') as $element) { /* we only want one link, so we make sure the parent of the a node has the class of titlelogo */ if ($element->parentNode->getAttribute('class') == 'titlelogo') { /* Place it in the url array to setup another array later */ $this->urls[] = 'http://profiles.us.playstation.com'.$element->getAttribute('href'); } } /* Since the image amount will always be the same as the game/url amount we only need * one foreach loop */ foreach ($this->images as $key => $image) { static $i = 1; /* We create the gameList array so that we can change it to an stdClass object later */ $var = 'game'.$i; $gameList['game'][] = array('image' => $image, 'title' => $this->games[$key], 'trophy_url' => $this->urls[$key]); ++$i; } /* We count the total number of games played just for convienence */ $this->totalGames = count($gameList); /* We conver the gameList array to an stdClass object so that you can call * upon the variables stored within the gameList object * i.e the title for game 1 would be $foo->gameList->game[0]['title'] */ $this->gameList = (object)$gameList; } /* * 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; } } can anyone please halp me? thanks in advance... ***EDIT: I just saw that the writer of this class is part of the forum , so if he can help me with this , I would be very greatful. Link to comment https://forums.phpfreaks.com/topic/196939-retrieving-trophy-data-from-psn/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.