Jump to content
XboxLeaders

Search the Community

Showing results for tags 'wrapper'.



More search options

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Forums

  • Xbox Games
    • Game Completions
  • Community
    • The Lounge
  • Xbox LIVE API
    • Xbox Leaders Project
    • Xbox Leaders Support

Find results in...

Find results that contain...


Date Created

  • Start

    End


Last Updated

  • Start

    End


Filter by number of...

Joined

  • Start

    End


Group


AIM


MSN


Website URL


ICQ


Yahoo


Jabber


Skype


Location


Interests

Found 2 results

  1. <?php // This try/catch can be placed in the file where you wish to process the received data // The $data variable will contain the data if exceptions are not thrown try { $requestData = array(); $api = new LeadersApi(); foreach ($api->_allowedRequests as $var) { $requestData[$var] = (isset($_GET[$var])) ? $_GET[$var] : null; } $data = $api->processRequest($requestData); } catch (Exception $e) { echo $e->getMessage(); } // Below can go in a new file but remember to include it!! Class LeadersApi { /** * A list of all possible request variables * * @var array */ public $_allowedRequests = array('type', 'tag', 'gameId'); /** * The base URL for the API * * @var string */ protected $_baseUri = 'http://www.xboxleaders.com/api/'; /** * Gets the data from a URL build with supplied parameters * * @param $file String * @param $requestVars array */ protected function getData($file, array $requestVars) { try { $url = $this->_baseUri . strtolower($file) . ".json?" . http_build_query($requestVars, null, '&'); return json_decode(file_get_contents($url)); } catch (Exception $e) { return $e; } } /** * Construct method to ensure user has correct libs */ public function __construct() { if (!function_exists('json_decode')) { throw new Exception('This wrapper requires the JSON library'); } } /** * Processed the variables collected from the view * * @param $options array */ public function processRequest(array $options) { if(null !== $options['type']) { $type = ($options['type'] = ucfirst(strtolower($options['type']))); if(method_exists(get_class($this), 'get' . $type)) { $method = "get" . $type; return $this->$method($options); } else { throw new Exception("The type: " . $options['type'] . ", does not exist!"); } } else { throw new Exception('The request type has not been set'); } } /** * Ensures the parameters for games have been set and can therefore process data * Will otherwise throw an exception * * @param $option array */ protected function getGames($option) { if (null !== $option['tag']) { $parameters = array( 'gamertag' => $option['tag'] ); return $this->getData($option['type'], $parameters); } else { $message = $this->exceptionMessage(__FUNCTION__); throw new Exception($message); } } /** * Ensures the parameters for profile have been set and can therefore process data * Will otherwise throw an exception * * @param $option array */ protected function getProfile($option) { if (null !== $option['tag']) { $parameters = array( 'gamertag' => $option['tag'] ); return $this->getData($option['type'], $parameters); } else { $message = $this->exceptionMessage(__FUNCTION__); throw new Exception($message); } } /** * Ensures the parameters for achievements have been set and can therefore process data * Will otherwise throw an exception * * @param $option array */ protected function getAchievements($option) { if(null !== $option['tag'] && null !== $option['gameId'] ) { $parameters = array( 'gamertag' => $option['tag'], 'gameid' => $option['gameId'] ); return $this->getData($option['type'], $parameters); } else { $message = $this->exceptionMessage(__FUNCTION__); throw new Exception($message); } } /** * Ensures the parameters for friends have been set and can therefore process data * Will otherwise trow an exception * * @param $option array */ protected function getFriends($option) { if(null !== $option['tag']) { $parameters = array( 'gamertag' => $option['tag'] ); return $this->getData($option['type'], $parameters); } else { $message = $this->exceptionMessage(__FUNCTION__); throw new Exception($message); } } /** * Generic message for all of the 'get' * @param $message string (the function name it was initiated on) */ public function exceptionMessage($message) { return "Unable to process $message request, the variables have not been set"; } } Here's a wrapper I quickly knocked up tonight and its loosely based on the wrapper I'm sure you've all seen on Github. It only returns decoded JSON Object Array (Not XML, PHP Blah Blah Blah). The idea is that URL or the POST request variables determine what action is to be done when the Class is instantiated. There is however, as of yet, no validation around the gamertag which to be honest is in the wrapper on github so feel free to mash this and that together if desired (I would advise you to do it!). Anyway, it's nothing that I am using, just thought I would try knocking one up. Also, It doesn't use cURL, not sure if this is a good thing or a bad thing as I'm quite a noob in terms of coding experience Erm, what else.... The $_allowedRequests are the variable keys that must be passed for example: www.mywebsite.com?type=profile&tag=gamertag when 2 params are required such as the achievements request, this might look like: www.mywebsite.com?type=achievements&tag=gamertag&gameId=1096157379 type can be: profile, achievements, games, friends. Which then maps to the relevant method in the Class. Regards Andrew Any questions, either post on here of course, or, you can send me a message on twitter @addictedtoxbox
  2. Hi all, I'm sort of new to this, although I use git on my dev machine at work as a local repo but I don't use it at home and therefore have never used the github site itself. I copied the code from the lib file here: https://github.com/j...api.wrapper.php and plopped the code in a new file on my machine. I had a few small problems getting the code to work at first but its now working really well. Quick question, the code in the linked file above, is this what I would have pulled from github? If so, I am willing to post my fixes on here or, get to grips with github and contribute. Keep up the good work guys
×
×
  • Create New...