ProfesseurCraft

Class JSONAPI pour Minecraft

Aug 4th, 2013
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.23 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * A PHP class for access Minecraft servers that have Bukkit with the {@link http://github.com/alecgorge/JSONAPI JSONAPI} plugin installed.
  5.  *
  6.  * This class handles everything from key creation to URL creation to actually returning the decoded JSON as an associative array.
  7.  *
  8.  * @author Alec Gorge <[email protected]>
  9.  * @version Alpha 5
  10.  * @link http://github.com/alecgorge/JSONAPI
  11.  * @package JSONAPI
  12.  * @since Alpha 5
  13.  */
  14. class JSONAPI {
  15.     public $host;
  16.     public $port;
  17.     public $salt;
  18.     public $username;
  19.     public $password;
  20.     private $urlFormats = array(
  21.         "call" => "http://%s:%s/api/call?method=%s&args=%s&key=%s",
  22.         "callMultiple" => "http://%s:%s/api/call-multiple?method=%s&args=%s&key=%s"
  23.     );
  24.    
  25.     /**
  26.      * Creates a new JSONAPI instance.
  27.      */
  28.     public function __construct ($host, $port, $uname, $pword, $salt) {
  29.         $this->host = $host;
  30.         $this->port = $port;
  31.         $this->username = $uname;
  32.         $this->password = $pword;
  33.         $this->salt = $salt;
  34.        
  35.         if(!extension_loaded("cURL")) {
  36.             throw new Exception("JSONAPI requires cURL extension in order to work.");
  37.         }
  38.     }
  39.    
  40.     /**
  41.      * Generates the proper SHA256 based key from the given method suitable for use as the key GET parameter in a JSONAPI API call.
  42.      *
  43.      * @param string $method The name of the JSONAPI API method to generate the key for.
  44.      * @return string The SHA256 key suitable for use as the key GET parameter in a JSONAPI API call.
  45.      */
  46.     public function createKey($method) {
  47.         if(is_array($method)) {
  48.             $method = json_encode($method);
  49.         }
  50.         return hash('sha256', $this->username . $method . $this->password . $this->salt);
  51.     }
  52.    
  53.     /**
  54.      * Generates the proper URL for a standard API call the given method and arguments.
  55.      *
  56.      * @param string $method The name of the JSONAPI API method to generate the URL for.
  57.      * @param array $args An array of arguments that are to be passed in the URL.
  58.      * @return string A proper standard JSONAPI API call URL. Example: "http://localhost:20059/api/call?method=methodName&args=jsonEncodedArgsArray&key=validKey".
  59.      */
  60.     public function makeURL($method, array $args) {
  61.         return sprintf($this->urlFormats["call"], $this->host, $this->port, rawurlencode($method), rawurlencode(json_encode($args)), $this->createKey($method));
  62.     }
  63.    
  64.     /**
  65.      * Generates the proper URL for a multiple API call the given method and arguments.
  66.      *
  67.      * @param array $methods An array of strings, where each string is the name of the JSONAPI API method to generate the URL for.
  68.      * @param array $args An array of arrays, where each array contains the arguments that are to be passed in the URL.
  69.      * @return string A proper multiple JSONAPI API call URL. Example: "http://localhost:20059/api/call-multiple?method=[methodName,methodName2]&args=jsonEncodedArrayOfArgsArrays&key=validKey".
  70.      */
  71.     public function makeURLMultiple(array $methods, array $args) {
  72.         return sprintf($this->urlFormats["callMultiple"], $this->host, $this->port, rawurlencode(json_encode($methods)), rawurlencode(json_encode($args)), $this->createKey($methods));
  73.     }
  74.    
  75.     /**
  76.      * Calls the single given JSONAPI API method with the given args.
  77.      *
  78.      * @param string $method The name of the JSONAPI API method to call.
  79.      * @param array $args An array of arguments that are to be passed.
  80.      * @return array An associative array representing the JSON that was returned.
  81.      */
  82.     public function call($method, array $args = array()) {
  83.         if(is_array($method)) {
  84.             return $this->callMultiple($method, $args);
  85.         }
  86.        
  87.         $url = $this->makeURL($method, $args);
  88.  
  89.         return json_decode($this->curl($url), true);
  90.     }
  91.    
  92.     private function curl($url) {
  93.         $c = curl_init($url);
  94.         curl_setopt($c, CURLOPT_PORT, $this->port);
  95.         curl_setopt($c, CURLOPT_HEADER, false);
  96.         curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
  97.         curl_setopt($c, CURLOPT_TIMEOUT, 10);      
  98.         $result = curl_exec($c);
  99.         curl_close($c);
  100.         return $result;
  101.     }
  102.    
  103.     /**
  104.      * Calls the given JSONAPI API methods with the given args.
  105.      *
  106.      * @param array $methods An array strings, where each string is the name of a JSONAPI API method to call.
  107.      * @param array $args An array of arrays of arguments that are to be passed.
  108.      * @throws Exception When the length of the $methods array and the $args array are different, an exception is thrown.
  109.      * @return array An array of associative arrays representing the JSON that was returned.
  110.      */
  111.     public function callMultiple(array $methods, array $args = array()) {
  112.         if(count($methods) !== count($args)) {
  113.             throw new Exception("The length of the arrays \$methods and \$args are different! You need an array of arguments for each method!");
  114.         }
  115.        
  116.         $url = $this->makeURLMultiple($methods, $args);
  117.  
  118.         return json_decode($this->curl($url), true);
  119.     }
  120.     /**
  121.      * The default function called if no one matched for JSONAPI.
  122.      *
  123.      * @param string $method The name of the JSONAPI API method to call.
  124.      * @param array $params An array of arguments that are to be passed.
  125.      * @return array An associative array representing the JSON that was returned.
  126.      */
  127.     function __call($method, $params) {
  128.                 if(is_array($params)) {
  129.                     return $this->call($method, $params);
  130.                 } else {
  131.                     return $this->call($method, array($params));   
  132.                 }
  133.     }
  134. }
Add Comment
Please, Sign In to add comment