Advertisement
Guest User

Untitled

a guest
May 17th, 2014
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 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. $ch = curl_init($this->url);
  105. curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
  106. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
  107. curl_setopt($ch, CURLOPT_POST, true);
  108. curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
  109. $response = json_decode(curl_exec($ch),true);
  110. curl_close($ch);
  111.  
  112. // debug output
  113. if ($this->debug) {
  114. echo nl2br($debug);
  115. }
  116.  
  117. // final checks and return
  118. if (!$this->notification) {
  119. // check
  120. if ($response['id'] != $currentId) {
  121. throw new Exception('Incorrect response id (request id: '.$currentId.', response id: '.$response['id'].')');
  122. }
  123. if (!is_null($response['error'])) {
  124. throw new Exception('Request error: '.$response['error']);
  125. }
  126.  
  127. return $response['result'];
  128.  
  129. } else {
  130. return true;
  131. }
  132. }
  133. }
  134. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement