djekl
11/10/2012 - 8:11 PM

PHP class to access PlayStation Network friends list.

PHP class to access PlayStation Network friends list.

<?php
// Disable libxml errors and allow user to fetch error information as needed
libxml_use_internal_errors(true);

class PSN_friends
{
	public $log_file, $session, $frineds;

	function __construct()
	{
		$this->log_file = fopen("log", 'a+'); // Logging purposes only
		$this->session = curl_init(); // create a new cURL resource
	}

	function __destruct()
	{
		curl_close($this->session); // Closes a cURL session and frees all resources.
		fclose($this->log_file);
	}

	function login($username, $password)
	{
		$loginURL = "https://store.playstation.com/external/login.action?returnURL=https://secure.eu.playstation.com/sign-in/confirmation";

		curl_setopt_array($this->session,array(
			CURLOPT_POST => 1,
			CURLOPT_POSTFIELDS => array("loginName"=>$username, "password"=>$password), // The full data to post in a HTTP "POST" operation.
			CURLOPT_URL => $loginURL,
			CURLOPT_SSL_VERIFYPEER => FALSE,
			CURLOPT_SSL_VERIFYHOST => FALSE,
			CURLOPT_RETURNTRANSFER => 1,
			//CURLOPT_HTTPGET => 1,
			CURLOPT_COOKIEFILE => realpath('cookie.txt'),
			CURLOPT_COOKIEJAR => realpath('cookie.txt'),
			CURLOPT_VERBOSE => 1,
			CURLOPT_STDERR => $this->log_file
		));

		return curl_exec($this->session);
	}

	function refresh()
	{
		$dataURL = "https://secure.eu.playstation.com/ajax/mypsn/friend/presence";

		// set cURL options
		curl_setopt ($this->session, CURLOPT_URL, $dataURL); // The URL to fetch
		curl_setopt ($this->session, CURLOPT_POST, 0); // regular HTTP POST
		curl_setopt ($this->session, CURLOPT_RETURNTRANSFER, 1); // return the transfer as a string of the return value of curl_exec() instead of outputting it out directly
		curl_setopt ($this->session, CURLOPT_FOLLOWLOCATION, 1); // follow any "Location: " header that the server sends as part of the HTTP header
		curl_setopt ($this->session, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'); // The contents of the "User-Agent: " header to be used in a HTTP request.
		curl_setopt ($this->session, CURLOPT_SSL_VERIFYPEER, 0); // stop cURL from verifying the peer's certificate.
		curl_setopt ($this->session, CURLOPT_REFERER, $dataURL); // The contents of the "Referer: " header to be used in a HTTP request.
		curl_setopt ($this->session, CURLOPT_VERBOSE, 1); // output verbose information.
		curl_setopt ($this->session, CURLOPT_STDERR, $this->log_file); // An alternative location to output errors to instead of STDERR. 
		curl_setopt ($this->session, CURLOPT_COOKIEFILE, realpath('cookie')); // The name of the file containing the cookie data.

		$response = curl_exec($this->session); // Perform a cURL session

		$this->frineds = simplexml_load_string($response);

		return $this->frineds;
	}

	function get_list($status = null)
	{
		// status: offline, online, online-away, online-ingame

		$friends_list = array();

		foreach($this->frineds->psn_friend as $friend)
			if (isset($status))
			{
				if($friend->current_presence == $status)
					array_push($friends_list, $friend->onlineid);
			}
			else
				array_push($friends_list, $friend->onlineid);	

		return $friends_list;
	}

	function get_online()
	{
		$online_list = array();

		foreach($this->frineds->psn_friend as $friend)
			if (substr($friend->current_presence, 0, 6) == "online")
				array_push($online_list, $friend->onlineid);	

		return $online_list;
	}

	function get_friend($username, $info = null)
	{
		// info: current_presence, current_game, current_avatar, comment, trophy

		foreach($this->frineds->psn_friend as $friend)
			if ($friend->onlineid == $username)
				if (isset($info))
					return $friend->$info;
				else
					return $friend;

		else false;
	}
}
?>