Overview

Packages

  • Jyxo_Beholder
  • Jyxo_Charset
  • Jyxo_Color
  • Jyxo_Css
  • Jyxo_ErrorHandling
  • Jyxo_FirePhp
  • Jyxo_Gettext
    • Parser
  • Jyxo_Html
  • Jyxo_Input
    • Chain
    • Filter
    • Validator
  • Jyxo_Mail
    • Email
    • Parser
    • Sender
  • Jyxo_Rpc
    • Json
    • Xml
  • Jyxo_Shell
  • Jyxo_SpamFilter
  • Jyxo_Spl
  • Jyxo_String
  • Jyxo_Svn
  • Jyxo_Time
  • Jyxo_Timer
  • Jyxo_Webdav
  • Jyxo_XmlReader
  • PHP

Classes

  • Jyxo_Rpc_Json_Client
  • Jyxo_Rpc_Json_Server

Exceptions

  • Jyxo_Rpc_Json_Exception
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
 1: <?php
 2: 
 3: /**
 4:  * Jyxo PHP Library
 5:  *
 6:  * LICENSE
 7:  *
 8:  * This source file is subject to the new BSD license that is bundled
 9:  * with this package in the file license.txt.
10:  * It is also available through the world-wide-web at this URL:
11:  * https://github.com/jyxo/php/blob/master/license.txt
12:  */
13: 
14: /**
15:  * Class for sending requests using JSON-RPC.
16:  * Requires json and curl PHP extensions.
17:  *
18:  * @category Jyxo
19:  * @package Jyxo_Rpc
20:  * @subpackage Json
21:  * @copyright Copyright (c) 2005-2011 Jyxo, s.r.o.
22:  * @license https://github.com/jyxo/php/blob/master/license.txt
23:  * @author Jan Pěček
24:  */
25: class Jyxo_Rpc_Json_Client extends Jyxo_Rpc_Client
26: {
27:     /**
28:      * Sends a request and fetches server's response.
29:      *
30:      * @param string $method Method name
31:      * @param array $params Method parameters
32:      * @return mixed
33:      * @throws BadMethodCallException If no server address was provided
34:      * @throws Jyxo_Rpc_Json_Exception On error
35:      */
36:     public function send($method, array $params)
37:     {
38:         // Start profiling
39:         $this->profileStart();
40: 
41:         // Generates ID
42:         $id = md5(uniqid(rand(), true));
43: 
44:         try {
45:             // Prepare JSON-RPC request
46:             $data = json_encode(
47:                 array(
48:                     'method' => $method,
49:                     'params' => $params,
50:                     'id' => $id
51:                 )
52:             );
53: 
54:             // Fetch response
55:             $response = $this->process('application/json', $data);
56: 
57:             // Process response
58:             $response = json_decode($response, true);
59: 
60:         } catch (Jyxo_Rpc_Exception $e) {
61:             // Finish profiling
62:             $this->profileEnd('JSON', $method, $params, $e->getMessage());
63: 
64:             throw new Jyxo_Rpc_Json_Exception($e->getMessage(), 0, $e);
65:         }
66: 
67:         // Finish profiling
68:         $this->profileEnd('JSON', $method, $params, $response);
69: 
70:         // Error in response
71:         if (!is_array($response) || !isset($response['id'])) {
72:             throw new Jyxo_Rpc_Json_Exception('Invalid response data.');
73:         }
74: 
75:         if ($id !== $response['id']) {
76:             throw new Jyxo_Rpc_Json_Exception('Response ID does not correspond to request ID.');
77:         }
78: 
79:         if (isset($response['error'])) {
80:             throw new Jyxo_Rpc_Json_Exception(preg_replace('~\s+~', ' ', $response['error']['message']), $response['error']['code']);
81:         }
82: 
83:         if (!isset($response['result'])) {
84:             throw new Jyxo_Rpc_Json_Exception('No response data.');
85:         }
86: 
87:         return $response['result'];
88:     }
89: }
90: 
Jyxo PHP Library API documentation generated by ApiGen 2.3.0