/** * Send a JSON-RPC message and optional parameter arguments to the server. * * Use the API functions if possible. This method remains public to support * changes being made to the API before this libarary can be updated. * * @param string $message * @param mixed $args, ... * @return mixed * @throws BitcoinClientException * @see xmlrpc.inc:php_xmlrpc_decode() */ public function query($message) { if (!$message || empty($message)) { throw new BitcoinClientException("Bitcoin client query requires a message"); } $msg = new jsonrpcmsg($message); if (func_num_args() > 1) { for ($i = 1; $i < func_num_args(); $i++) { $msg->addParam(self::query_arg_to_parameter(func_get_arg($i))); } } $response = $this->send($msg); if ($response->faultCode()) { throw new BitcoinClientException($response->faultString()); } return php_xmlrpc_decode($response->value()); }
/** * Convert the json representation of a jsonrpc method call, jsonrpc method response * or single json value into the appropriate object (a.k.a. deserialize). * Please note that there is no way to distinguish the serialized representation * of a single json val of type object which has the 3 appropriate members from * the serialization of a method call or method response. * In such a case, the function will return a jsonrpcresp or jsonrpcmsg * @param string $json_val * @param array $options * @return mixed false on error, or an instance of jsonrpcval, jsonrpcresp or jsonrpcmsg * @access public * @todo add options controlling character set encodings */ function php_jsonrpc_decode_json($json_val, $options = array()) { $src_encoding = array_key_exists('src_encoding', $options) ? $options['src_encoding'] : $GLOBALS['xmlrpc_defencoding']; $dest_encoding = array_key_exists('dest_encoding', $options) ? $options['dest_encoding'] : $GLOBALS['xmlrpc_internalencoding']; //$GLOBALS['_xh'] = array(); $GLOBALS['_xh']['isf'] = 0; if (!json_parse($json_val, false, $src_encoding, $dest_encoding)) { error_log($GLOBALS['_xh']['isf_reason']); return false; } else { $val = $GLOBALS['_xh']['value']; // shortcut if ($GLOBALS['_xh']['value']->kindOf() == 'struct') { if ($GLOBALS['_xh']['value']->structSize() == 3) { if ($GLOBALS['_xh']['value']->structMemExists('method') && $GLOBALS['_xh']['value']->structMemExists('params') && $GLOBALS['_xh']['value']->structMemExists('id')) { /// @todo we do not check for correct type of 'method', 'params' struct members... $method = $GLOBALS['_xh']['value']->structMem('method'); $msg = new jsonrpcmsg($method->scalarval(), null, php_jsonrpc_decode($GLOBALS['_xh']['value']->structMem('id'))); $params = $GLOBALS['_xh']['value']->structMem('params'); for ($i = 0; $i < $params->arraySize(); ++$i) { $msg->addparam($params->arrayMem($i)); } return $msg; } else { if ($GLOBALS['_xh']['value']->structMemExists('result') && $GLOBALS['_xh']['value']->structMemExists('error') && $GLOBALS['_xh']['value']->structMemExists('id')) { $id = php_jsonrpc_decode($GLOBALS['_xh']['value']->structMem('id')); $err = php_jsonrpc_decode($GLOBALS['_xh']['value']->structMem('error')); if ($err == null) { $resp = new jsonrpcresp($GLOBALS['_xh']['value']->structMem('result')); } else { if (is_array($err) && array_key_exists('faultCode', $err) && array_key_exists('faultString', $err)) { if ($err['faultCode'] == 0) { // FAULT returned, errno needs to reflect that $err['faultCode'] = -1; } } else { $err = array('faultCode' => -1, 'faultString' => serialize_jsonrpcval($GLOBALS['_xh']['value']->structMem('error'))); } $resp = new jsonrpcresp(0, $err['faultCode'], $err['faultString']); } $resp->id = $id; return $resp; } } } } // not a request msg nor a response: a plain jsonrpcval obj return $GLOBALS['_xh']['value']; } }
if (class_exists('Zend_Json')) { begin_test('Data encoding (large array)', 'Zend encoding'); for ($i = 0; $i < $num_tests; $i++) { $out = Zend_Json::encode($data); } end_test('Data encoding (large array)', 'Zend encoding', $out); } if (function_exists('json_encode')) { begin_test('Data encoding (large array)', 'native encoding'); for ($i = 0; $i < $num_tests; $i++) { $out = json_encode($data); } end_test('Data encoding (large array)', 'native encoding', $out); } // test 'old style' data decoding vs. 'automatic style' decoding $dummy = new jsonrpcmsg(''); $out = new jsonrpcresp($value); $in = $out->serialize(); begin_test('Data decoding (large array)', 'manual decoding'); for ($i = 0; $i < $num_tests; $i++) { $response =& $dummy->ParseResponse($in, true); $value = $response->value(); $result = array(); for ($k = 0; $k < $value->arraysize(); $k++) { $val1 = $value->arraymem($k); $out = array(); while (list($name, $val) = $val1->structeach()) { $out[$name] = array(); for ($j = 0; $j < $val->arraysize(); $j++) { $data = $val->arraymem($j); $out[$name][] = $data->scalarval();
/** * @access private */ function parseRequest($data, $content_encoding = '') { $GLOBALS['_xh'] = array(); if (!jsonrpc_parse_req($data, $this->functions_parameters_type == 'phpvals' || $this->functions_parameters_type == 'epivals', false, $content_encoding)) { $r = new jsonrpcresp(0, $GLOBALS['xmlrpcerr']['invalid_request'], $GLOBALS['xmlrpcstr']['invalid_request'] . ' ' . $GLOBALS['_xh']['isf_reason']); } else { if ($this->functions_parameters_type == 'phpvals' || $this->functions_parameters_type == 'epivals' || isset($this->dmap[$GLOBALS['_xh']['method']]['parameters_type']) && $this->dmap[$GLOBALS['_xh']['method']]['parameters_type'] == 'phpvals') { if ($this->debug > 1) { $this->debugmsg("\n+++PARSED+++\n" . var_export($GLOBALS['_xh']['params'], true) . "\n+++END+++"); } $r = $this->execute($GLOBALS['_xh']['method'], $GLOBALS['_xh']['params'], $GLOBALS['_xh']['pt'], $GLOBALS['_xh']['id']); } else { // build an xmlrpcmsg object with data parsed from xml $m = new jsonrpcmsg($GLOBALS['_xh']['method'], 0, $GLOBALS['_xh']['id']); // now add parameters in /// @todo for more speeed, we could just substitute the array... for ($i = 0; $i < sizeof($GLOBALS['_xh']['params']); $i++) { $m->addParam($GLOBALS['_xh']['params'][$i]); } if ($this->debug > 1) { $this->debugmsg("\n+++PARSED+++\n" . var_export($m, true) . "\n+++END+++"); } $r = $this->execute($m); } } return $r; }