Advertisement
Guest User

Untitled

a guest
May 17th, 2014
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 KB | None | 0 0
  1. <?php
  2. /**
  3. * The object of this class are generic jsonRPC 1.0 clients
  4. * http://json-rpc.org/wiki/specification
  5. *
  6. * @author sergio <jsonrpcphp@inservibile.org>
  7. */
  8. class jsonRPCClient {
  9.  
  10. /**
  11. * Debug state
  12. *
  13. * @var boolean
  14. */
  15. private $debug;
  16.  
  17. /**
  18. * The server URL
  19. *
  20. * @var string
  21. */
  22. private $url;
  23. /**
  24. * The request id
  25. *
  26. * @var integer
  27. */
  28. private $id;
  29. /**
  30. * If true, notifications are performed instead of requests
  31. *
  32. * @var boolean
  33. */
  34. private $notification = false;
  35.  
  36. /**
  37. * Takes the connection parameters
  38. *
  39. * @param string $url
  40. * @param boolean $debug
  41. */
  42. public function __construct($url,$debug = false) {
  43. // server URL
  44. $this->url = $url;
  45. // proxy
  46. empty($proxy) ? $this->proxy = '' : $this->proxy = $proxy;
  47. // debug state
  48. empty($debug) ? $this->debug = false : $this->debug = true;
  49. // message id
  50. $this->id = 1;
  51. }
  52.  
  53. /**
  54. * Sets the notification state of the object. In this state, notifications are performed, instead of requests.
  55. *
  56. * @param boolean $notification
  57. */
  58. public function setRPCNotification($notification) {
  59. empty($notification) ?
  60. $this->notification = false
  61. :
  62. $this->notification = true;
  63. }
  64.  
  65. /**
  66. * Performs a jsonRCP request and gets the results as an array
  67. *
  68. * @param string $method
  69. * @param array $params
  70. * @return array
  71. */
  72. public function __call($method,$params) {
  73.  
  74. // check
  75. if (!is_scalar($method)) {
  76. throw new Exception('Method name has no scalar value');
  77. }
  78.  
  79. // check
  80. if (is_array($params)) {
  81. // no keys
  82. $params = array_values($params);
  83. } else {
  84. throw new Exception('Params must be given as array');
  85. }
  86.  
  87. // sets notification or request task
  88. if ($this->notification) {
  89. $currentId = NULL;
  90. } else {
  91. $currentId = $this->id;
  92. }
  93.  
  94. // prepares the request
  95. $request = array(
  96. 'method' => $method,
  97. 'params' => $params,
  98. 'id' => $currentId
  99. );
  100. $request = json_encode($request);
  101. $this->debug && $this->debug.='***** Request *****'."\n".$request."\n".'***** End Of request *****'."\n\n";
  102.  
  103. // performs the HTTP POST
  104. $opts = array ('http' => array (
  105. 'method' => 'POST',
  106. 'header' => 'Content-type: application/json',
  107. 'content' => $request
  108. ));
  109. $context = stream_context_create($opts);
  110. if ($fp = fopen($this->url, 'r', false, $context)) {
  111. $response = '';
  112. while($row = fgets($fp)) {
  113. $response.= trim($row)."\n";
  114. }
  115. $this->debug && $this->debug.='***** Server response *****'."\n".$response.'***** End of server response *****'."\n";
  116. $response = json_decode($response,true);
  117. } else {
  118. throw new Exception('Unable to connect to '.$this->url);
  119. }
  120.  
  121. // debug output
  122. if ($this->debug) {
  123. echo nl2br($debug);
  124. }
  125.  
  126. // final checks and return
  127. if (!$this->notification) {
  128. // check
  129. if ($response['id'] != $currentId) {
  130. throw new Exception('Incorrect response id (request id: '.$currentId.', response id: '.$response['id'].')');
  131. }
  132. if (!is_null($response['error'])) {
  133. throw new Exception('Request error: '.$response['error']);
  134. }
  135.  
  136. return $response['result'];
  137.  
  138. } else {
  139. return true;
  140. }
  141. }
  142. }
  143. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement