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 creating a JSON-RPC server.
 16:  *
 17:  * @category Jyxo
 18:  * @package Jyxo_Rpc
 19:  * @subpackage Json
 20:  * @copyright Copyright (c) 2005-2011 Jyxo, s.r.o.
 21:  * @license https://github.com/jyxo/php/blob/master/license.txt
 22:  * @author Jan Pěček
 23:  */
 24: class Jyxo_Rpc_Json_Server extends Jyxo_Rpc_Server
 25: {
 26:     /**
 27:      * Definition of error codes and appropriate error messages.
 28:      *
 29:      * @var array
 30:      */
 31:     private static $jsonErrors = array(
 32:         JSON_ERROR_DEPTH => 'Maximum stack depth exceeded.',
 33:         JSON_ERROR_CTRL_CHAR => 'Unexpected control character found.',
 34:         JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON.'
 35:     );
 36: 
 37:     /**
 38:      * List of registered methods.
 39:      *
 40:      * @var array
 41:      */
 42:     private $methods = array();
 43: 
 44:     /**
 45:      * Actually registers a function to a server method.
 46:      *
 47:      * @param string $func Function definition
 48:      */
 49:     protected function register($func)
 50:     {
 51:         $this->methods[] = $func;
 52:     }
 53: 
 54:     /**
 55:      * Processes a request and sends a JSON-RPC response.
 56:      */
 57:     public function process()
 58:     {
 59:         $requestId = '';
 60:         try {
 61:             $data = file_get_contents('php://input');
 62:             $data = trim($data);
 63:             if (empty($data)) {
 64:                 throw new Jyxo_Rpc_Json_Exception('No data received.', -32700);
 65:             }
 66:             $data = json_decode($data, true);
 67: 
 68:             // Request decoding error
 69:             if ($data === null && ($faultCode = json_last_error()) != JSON_ERROR_NONE) {
 70:                 throw new Jyxo_Rpc_Json_Exception(self::$jsonErrors[$faultCode], $faultCode);
 71:             }
 72: 
 73:             $requestId = isset($data['id']) ? $data['id'] : '';
 74: 
 75:             // Parsing request data error
 76:             if (empty($data['method']) || !isset($data['id'])) {
 77:                 throw new Jyxo_Rpc_Json_Exception('Parse error.', 10);
 78:             }
 79: 
 80:             // Non-existent method call
 81:             if (!in_array($data['method'], $this->methods)) {
 82:                 throw new Jyxo_Rpc_Json_Exception('Server error. Method not found.', -32601);
 83:             }
 84: 
 85:             // Request processing
 86:             $params = !empty($data['params']) ? (array) $data['params'] : array();
 87:             $response = $this->call($data['method'], $params);
 88:             $response = array('result' => $response, 'id' => $data['id']);
 89: 
 90:         } catch (Jyxo_Rpc_Json_Exception $e) {
 91:             $response = array(
 92:                 'error' => array(
 93:                     'message' => $e->getMessage(),
 94:                     'code' => $e->getCode()
 95:                 ),
 96:                 'id' => $requestId
 97:             );
 98:         }
 99: 
100:         header('Content-Type: application/json; charset="utf-8"');
101:         echo json_encode($response);
102:     }
103: }
104: 
Jyxo PHP Library API documentation generated by ApiGen 2.3.0