Advertisement
Guest User

jsonRPCClient.php

a guest
Aug 3rd, 2012
2,215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.77 KB | None | 0 0
  1. <?php
  2. /*
  3.                     COPYRIGHT
  4.  
  5. Copyright 2007 Sergio Vaccaro <[email protected]>
  6.  
  7. This file is part of JSON-RPC PHP.
  8.  
  9. JSON-RPC PHP is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or
  12. (at your option) any later version.
  13.  
  14. JSON-RPC PHP is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. GNU General Public License for more details.
  18.  
  19. You should have received a copy of the GNU General Public License
  20. along with JSON-RPC PHP; if not, write to the Free Software
  21. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  22. */
  23.  
  24. /**
  25.  * The object of this class are generic jsonRPC 1.0 clients
  26.  * http://json-rpc.org/wiki/specification
  27.  *
  28.  * @author sergio <[email protected]>
  29.  */
  30. class jsonRPCClient {
  31.    
  32.     /**
  33.      * Debug state
  34.      *
  35.      * @var boolean
  36.      */
  37.     private $debug;
  38.    
  39.     /**
  40.      * The server URL
  41.      *
  42.      * @var string
  43.      */
  44.     private $url;
  45.     /**
  46.      * The request id
  47.      *
  48.      * @var integer
  49.      */
  50.     private $id;
  51.     /**
  52.      * If true, notifications are performed instead of requests
  53.      *
  54.      * @var boolean
  55.      */
  56.     private $notification = false;
  57.    
  58.     /**
  59.      * Takes the connection parameters
  60.      *
  61.      * @param string $url
  62.      * @param boolean $debug
  63.      */
  64.     public function __construct($url,$debug = false) {
  65.         // server URL
  66.         $this->url = $url;
  67.         // proxy
  68.         empty($proxy) ? $this->proxy = '' : $this->proxy = $proxy;
  69.         // debug state
  70.         empty($debug) ? $this->debug = false : $this->debug = true;
  71.         // message id
  72.         $this->id = 1;
  73.     }
  74.    
  75.     /**
  76.      * Sets the notification state of the object. In this state, notifications are performed, instead of requests.
  77.      *
  78.      * @param boolean $notification
  79.      */
  80.     public function setRPCNotification($notification) {
  81.         empty($notification) ?
  82.                             $this->notification = false
  83.                             :
  84.                             $this->notification = true;
  85.     }
  86.    
  87.     /**
  88.      * Performs a jsonRCP request and gets the results as an array
  89.      *
  90.      * @param string $method
  91.      * @param array $params
  92.      * @return array
  93.      */
  94.     public function __call($method,$params) {
  95.        
  96.         // check
  97.         if (!is_scalar($method)) {
  98.             throw new Exception('Method name has no scalar value');
  99.         }
  100.        
  101.         // check
  102.         if (is_array($params)) {
  103.             // no keys
  104.             $params = array_values($params);
  105.         } else {
  106.             throw new Exception('Params must be given as array');
  107.         }
  108.        
  109.         // sets notification or request task
  110.         if ($this->notification) {
  111.             $currentId = NULL;
  112.         } else {
  113.             $currentId = $this->id;
  114.         }
  115.        
  116.         // prepares the request
  117.         $request = array(
  118.                         'method' => $method,
  119.                         'params' => $params,
  120.                         'id' => $currentId
  121.                         );
  122.         $request = json_encode($request);
  123.         $this->debug && $this->debug.='***** Request *****'."\n".$request."\n".'***** End Of request *****'."\n\n";
  124.        
  125.         // performs the HTTP POST
  126.         $ch = curl_init($this->url);
  127.         curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
  128.         curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
  129.         curl_setopt($ch, CURLOPT_POST, true);
  130.         curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
  131.         $response = json_decode(curl_exec($ch),true);
  132.         curl_close($ch);
  133.         // debug output
  134.         if ($this->debug) {
  135.             echo nl2br($debug);
  136.         }
  137.        
  138.         // final checks and return
  139.         if (!$this->notification) {
  140.             // check
  141.             if ($response['id'] != $currentId) {
  142.                 throw new Exception('Incorrect response id (request id: '.$currentId.', response id: '.$response['id'].')');
  143.             }
  144.             if (!is_null($response['error'])) {
  145.                 throw new Exception('Request error: '.$response['error']);
  146.             }
  147.            
  148.             return $response['result'];
  149.            
  150.         } else {
  151.             return true;
  152.         }
  153.     }
  154. }
  155. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement