Home > Development, Gaming, PSN > PlayStation 3 Trophies API

PlayStation 3 Trophies API

November 14th, 2009 john Leave a comment Go to comments

If you would like to retrieve your PlayStation 3 trophies data in an easy to read format, say, for reading into a mysql database or your own JAVA or iPhone computer program etc, then read on.

The PS3 Trophies API is a single PHP file, which takes as input, your PSN id, and the churns out all of your current PS3 Trophies achieved. The API is available from the following url, http://www.thegamecollective.co.uk/psn.php. All that’s needed is to pass your PSN id, for example, dunnestar, to the url:

http://www.thegamecollective.co.uk/psn.php?psnid=dunnestar

And you’ll receive the data back in an xml format.

You can pass the parameter format=key to receive the data as a properties file, like so:

http://www.thegamecollective.co.uk/psn.php?psnid=dunnestar&format=key

This API is a single PHP file and is currently being improved upon to include a variety of other features too. Leave a comment and I’ll post the API if anyone is interested!

Categories: Development, Gaming, PSN Tags:
  1. Sy
    November 28th, 2009 at 23:37 | #1

    This is great, how could i make this a viewable page for people to see and just load up for example.. a standar domain.com/psn and have a page with all the info on it which is readable..

  2. Sy
    November 28th, 2009 at 23:43 | #2

    Another thing ever thought of putting it into a wordpress style plugin or something?

  3. Eivind teig
    November 28th, 2009 at 23:58 | #3

    Brilliant, just the thing i was looking for :)

  4. November 29th, 2009 at 13:26 | #4

    @Sy
    Hi Sy, thanks for your comment! Do you know how to program in PHP? If you do, you could parse the results and then use these in the page you speak of. If you need more info, let me know.

  5. November 29th, 2009 at 13:26 | #5

    @Sy
    Wordstyle template? Nope, but it could be done! I’d be happy to help with it…

  6. mmmj
    January 13th, 2010 at 02:13 | #6

    Would love to see the php for this?
    Very neat!

  7. aur8l
    January 18th, 2010 at 18:39 | #7

    Sorry to revive this post from the depths of last year, but this php file is just what I need for my current devs. Is there any way to get my hands on this?
    that would be so nice, and would save me some trouble.
    thanks a lot.

  8. January 18th, 2010 at 19:08 | #8

    Hi aur8l, I’ll post the code shortly. It’s not perfect and may still have some bugs in it! Let me know how you get on with it, and if you make any improvements, I’d appreciate a look!


    < ?php
    function parsePsn( $psnid, $output = "array" )
    {
    if( strlen( $psnid ) === 0 )
    {
    die("Unknown PSN id");
    }
    if( $output === "array" )
    {
    $return_data = array();
    }
    else
    {
    $return_data = "";
    }
    $url = "http://profiles.us.playstation.com/playstation/psn/visit/profiles/".$psnid;

    $debug = false;

    $user_agent = $_SERVER['HTTP_USER_AGENT'];
    if( strlen( $useragent ) == 0 )
    $useragent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)";

    $cc = new mycurl( $url, $useragent );
    $cc->createCurl();
    $content = $cc;

    $look = array("Platinum","Gold","Silver","Bronze");
    $trophies = array();
    foreach( $look as $t )
    {
    $platinum_match = "/([0-9]*) ".$t."/";
    preg_match($platinum_match,$content,$matches);
    $trophies[ strtolower($t) ] = $matches[1];
    }

    $platinum_match = "/

    [ ]*([0-9]*)/";
    preg_match_all($image_match,$content,$matches);
    if( $debug ) print_r( $matches );

    if( $output === "xml" )
    {
    #header('Content-type: text/xml');
    $return_data .= "< ?xml version='1.0' encoding='ISO-8859-1'?>\n";
    $return_data .= "\n";
    $return_data .= " \n";
    } else if( $output === "key" )
    {
    #header('Content-type: text/plain');
    $return_data .= "name=". $psnid ."\n";
    $return_data .= "avatar=". $matches[1][0]."\n";
    $return_data .= "level=". $trophies['level']."\n";
    $return_data .= "totalbronze=". $trophies['bronze']."\n";
    $return_data .= "totalsilver=". $trophies['silver']."\n";
    $return_data .= "totalgold=". $trophies['gold']."\n";
    $return_data .= "totalplatinum=". $trophies['platinum']."\n";
    } else if( $output === "array" )
    {
    $return_data['name'] = $psnid;
    $return_data['avatar'] = $matches[1][0];
    $return_data['level'] = $trophies['level'];
    $return_data['totalbronze'] = $trophies['bronze'];
    $return_data['totalsilver'] = $trophies['silver'];
    $return_data['totalgold'] = $trophies['gold'];
    $return_data['totalplatinum'] = $trophies['platinum'];
    } else {
    die("No output defined");
    }

    $cc = new mycurl( "http://profiles.us.playstation.com/playstation/psn/profile/".$psnid."/get_ordered_trophies_data", $useragent );
    $cc->createCurl();
    $content = str_replace( " ", "", $cc );

    $platinum_match = "/(.*)< \/span>/";
    preg_match_all($platinum_match,$content,$games);

    $content = str_replace( "\n", "", $content );
    $content = str_replace( "\r", "", $content );

    $platinum_match = "/ ([^< ]*)<\/div>/";
    preg_match_all($platinum_match,$content,$matches);

    if( $debug ) print_r( $games );
    $game_icon_match = "//";
    preg_match_all($game_icon_match,$content,$game_icons);
    if( $debug ) print_r( $game_icons );

    $game_trophies_match = "/ /";
    preg_match_all($game_trophies_match,$content,$game_trophies);
    if( $debug ) print_r( $game_trophies );

    $game_completed_match = "/([^< ]*)<\/span>%/";
    preg_match_all($game_completed_match,$content,$game_completed);
    if( $debug ) print_r( $game_completed );

    if( $output === "key" )
    {
    $return_data .= "numgames=".count($games[1])."\n";
    } else if( $output === "array" )
    {
    $return_data['numgames'] = count($games[1]);
    }

    #
    #
    # Replace HTTP:// from urls for xml
    $search_http = array('http://','HTTP://');
    $replace_http = array('');
    $search_colon = array(':','&');
    $replace_colon = array('-','and');
    $users_games = array();

    $counter = 0;
    $num = 0;

    if( $output === "array" )
    {
    $all_games = array();
    }

    foreach ( $games[1] as $game )
    {
    $gametitle = str_replace( $search_colon, $replace_colon, html_entity_decode( $game ) );
    $trophies_url = "http://profiles.us.playstation.com".$game_trophies[1][$num];
    $completed = $game_completed[1][$num];
    if( $output === "xml" )
    {
    $return_data .= "";
    $num++;
    } else if( $output === "key" )
    {
    $num++;
    $return_data .= "name_". $num ."=". $gametitle."\n";
    $return_data .= "bronze_". $num."=". $matches[1][$counter]."\n";
    $return_data .= "silver_". $num."=". $matches[1][$counter+1]."\n";
    $return_data .= "gold_". $num."=". $matches[1][$counter+2]."\n";
    $return_data .= "platinum_". $num."=". $matches[1][$counter+3]."\n";
    $return_data .= "image_". $num."=". $game_icons[1][$num-1]."\n";
    $return_data .= "trophiesurl_". $num."=". $trophies_url."\n";
    } else if( $output === "array" )
    {
    $num++;
    $sub_game = array();
    $sub_game['name'] = $gametitle;
    $sub_game['gametitle'] = $gametitle;
    $sub_game['bronze'] = $matches[1][$counter];
    $sub_game['silver'] = $matches[1][$counter+1];
    $sub_game['gold'] = $matches[1][$counter+2];
    $sub_game['platinum'] = $matches[1][$counter+3];
    $sub_game['image'] = $game_icons[1][$num-1];
    $sub_game['trophiesurl'] = $trophies_url;
    $sub_game['completed'] = $completed;
    array_push( $all_games, $sub_game );
    }
    $counter+=4;
    }
    if( $debug ) print_r( $trophies );

    if( $output === "array" )
    {
    $return_data['games'] = $all_games;
    }

    if( $output === "xml" )
    {
    $return_data.= "";
    $return_data.= "";
    }
    return $return_data;
    }

    class mycurl {
    protected $_useragent;
    protected $_url;
    protected $_followlocation;
    protected $_timeout;
    protected $_maxRedirects;
    protected $_cookieFileLocation = './cookie.txt';
    protected $_post;
    protected $_postFields;
    protected $_referer ="http://www.thegamecollective.co.uk";

    protected $_session;
    protected $_webpage;
    protected $_includeHeader;
    protected $_noBody;
    protected $_status;
    protected $_binaryTransfer;
    public $authentication = 0;
    public $auth_name = '';
    public $auth_pass = '';

    public function useAuth($use){
    $this->authentication = 0;
    if($use == true) $this->authentication = 1;
    }

    public function setName($name){
    $this->auth_name = $name;
    }
    public function setPass($pass){
    $this->auth_pass = $pass;
    }

    public function __construct($url,$useragent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1",$followlocation = true,$timeOut = 30,$maxRedirecs = 4,$binaryTransfer = false,$includeHeader = false,$noBody = false)
    {
    $this->_url = $url;
    $this->_followlocation = $followlocation;
    $this->_timeout = $timeOut;
    $this->_maxRedirects = $maxRedirecs;
    $this->_noBody = $noBody;
    $this->_includeHeader = $includeHeader;
    $this->_binaryTransfer = $binaryTransfer;
    $this->_useragent = $useragent;

    $this->_cookieFileLocation = dirname(__FILE__).'/cookie.txt';

    }

    public function setReferer($referer){
    $this->_referer = $referer;
    }

    public function setCookiFileLocation($path)
    {
    $this->_cookieFileLocation = $path;
    }

    public function setPost ($postFields)
    {
    $this->_post = true;
    $this->_postFields = $postFields;
    }
    public function setUserAgent($userAgent)
    {
    $this->_useragent = $userAgent;
    }

    public function createCurl($url = 'nul')
    {
    if($url != 'nul'){
    $this->_url = $url;
    }

    $s = curl_init();

    curl_setopt($s,CURLOPT_URL,$this->_url);
    curl_setopt($s,CURLOPT_HTTPHEADER,array('Expect:'));
    curl_setopt($s,CURLOPT_TIMEOUT,$this->_timeout);
    curl_setopt($s,CURLOPT_MAXREDIRS,$this->_maxRedirects);
    curl_setopt($s,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($s,CURLOPT_FOLLOWLOCATION,$this->_followlocation);
    curl_setopt($s,CURLOPT_COOKIEJAR,$this->_cookieFileLocation);
    curl_setopt($s,CURLOPT_COOKIEFILE,$this->_cookieFileLocation);

    if($this->authentication == 1){
    curl_setopt($s, CURLOPT_USERPWD, $this->auth_name.':'.$this->auth_pass);
    }
    if($this->_post)
    {
    curl_setopt($s,CURLOPT_POST,true);
    curl_setopt($s,CURLOPT_POSTFIELDS,$this->_postFields);

    }

    if($this->_includeHeader)
    {
    curl_setopt($s,CURLOPT_HEADER,true);
    }

    if($this->_noBody)
    {
    curl_setopt($s,CURLOPT_NOBODY,true);
    }
    /*
    if($this->_binary)
    {
    curl_setopt($s,CURLOPT_BINARYTRANSFER,true);
    }
    */
    curl_setopt($s,CURLOPT_USERAGENT,$this->_useragent);
    curl_setopt($s,CURLOPT_REFERER,$this->_referer);

    $this->_webpage = curl_exec($s);
    $this->_status = curl_getinfo($s,CURLINFO_HTTP_CODE);
    curl_close($s);

    }

    public function getHttpStatus()
    {
    return $this->_status;
    }

    public function __tostring(){
    return $this->_webpage;
    }
    }
    ?>

  9. aur8l
    January 18th, 2010 at 20:25 | #9

    But, it’s not functionnal right now using the link :
    http://www.thegamecollective.co.uk/psn.php?psnid=dunnestar
    ?

  10. January 18th, 2010 at 21:41 | #10

    It’s working for me at the moment – it might be a timeout issue, maybe the psn server didn’t respond in time? Can you tell me the error you saw?

  11. aur8l
    January 20th, 2010 at 18:32 | #11

    No, actually it works, but not on Google Chrome… My bad !
    But, is there any way to have “clean” games’ names ?

  12. January 21st, 2010 at 08:02 | #12

    There is a way to fix the games names issue, the script isn’t interpreting the string name correctly. There’s encoding of some type, which I’ve not gotten round to. I’ll look into this. Also, it should work on Chrome :O So my bad!

  1. No trackbacks yet.