/** * Perform an XML-RPC request and return a response. * * @param Zend\XmlRpc\Request $request * @param null|Zend\XmlRpc\Response $response * @return void * @throws Zend\XmlRpc\Client\HttpException */ public function doRequest($request, $response = null) { $this->_lastRequest = $request; iconv_set_encoding('input_encoding', 'UTF-8'); iconv_set_encoding('output_encoding', 'UTF-8'); iconv_set_encoding('internal_encoding', 'UTF-8'); $http = $this->getHttpClient(); $httpRequest = $http->getRequest(); if ($httpRequest->getUri() === null) { $http->setUri($this->_serverAddress); } $headers = $httpRequest->headers(); $headers->addHeaders(array('Content-Type: text/xml; charset=utf-8', 'Accept: text/xml')); if (!$headers->get('user-agent')) { $headers->addHeaderLine('user-agent', 'Zend_XmlRpc_Client'); } $xml = $this->_lastRequest->__toString(); $http->setRawBody($xml); $httpResponse = $http->setMethod('post')->send(); if (!$httpResponse->isSuccess()) { /** * Exception thrown when an HTTP error occurs */ throw new Client\Exception\HttpException($httpResponse->getReasonPhrase(), $httpResponse->getStatusCode()); } if ($response === null) { $response = new Response(); } $this->_lastResponse = $response; $this->_lastResponse->loadXml($httpResponse->getBody()); }
/** * Perform an XML-RPC request and return a response. * * @param Zend\XmlRpc\Request $request * @param null|Zend\XmlRpc\Response $response * @return void * @throws Zend\XmlRpc\Client\HTTPException */ public function doRequest($request, $response = null) { $this->_lastRequest = $request; iconv_set_encoding('input_encoding', 'UTF-8'); iconv_set_encoding('output_encoding', 'UTF-8'); iconv_set_encoding('internal_encoding', 'UTF-8'); $http = $this->getHttpClient(); if ($http->getUri() === null) { $http->setUri($this->_serverAddress); } $http->setHeaders(array('Content-Type: text/xml; charset=utf-8', 'Accept: text/xml')); if ($http->getHeader('user-agent') === null) { $http->setHeaders(array('User-Agent: Zend_XmlRpc_Client')); } $xml = $this->_lastRequest->__toString(); $http->setRawData($xml); $httpResponse = $http->request(HTTP\Client::POST); if (!$httpResponse->isSuccessful()) { /** * Exception thrown when an HTTP error occurs */ throw new Client\HTTPException($httpResponse->getMessage(), $httpResponse->getStatus()); } if ($response === null) { $response = new Response(); } $this->_lastResponse = $response; $this->_lastResponse->loadXml($httpResponse->getBody()); }
/** * Multicall - boxcar feature of XML-RPC for calling multiple methods * in a single request. * * Expects a an array of structs representing method calls, each element * having the keys: * - methodName * - params * * Returns an array of responses, one for each method called, with the value * returned by the method. If an error occurs for a given method, returns a * struct with a fault response. * * @see http://www.xmlrpc.com/discuss/msgReader$1208 * @param array $methods * @return array */ public function multicall($methods) { $responses = array(); foreach ($methods as $method) { $fault = false; if (!is_array($method)) { $fault = $this->server->fault('system.multicall expects each method to be a struct', 601); } elseif (!isset($method['methodName'])) { $fault = $this->server->fault('Missing methodName: ' . var_export($methods, 1), 602); } elseif (!isset($method['params'])) { $fault = $this->server->fault('Missing params', 603); } elseif (!is_array($method['params'])) { $fault = $this->server->fault('Params must be an array', 604); } else { if ('system.multicall' == $method['methodName']) { // don't allow recursive calls to multicall $fault = $this->server->fault('Recursive system.multicall forbidden', 605); } } if (!$fault) { try { $request = new \Zend\XmlRpc\Request(); $request->setMethod($method['methodName']); $request->setParams($method['params']); $response = $this->server->handle($request); if ($response instanceof \Zend\XmlRpc\Fault || $response->isFault()) { $fault = $response; } else { $responses[] = $response->getReturnValue(); } } catch (\Exception $e) { $fault = $this->server->fault($e); } } if ($fault) { $responses[] = array('faultCode' => $fault->getCode(), 'faultString' => $fault->getMessage()); } } return $responses; }
include __DIR__ . '/../vendor/autoload.php'; ini_set('date.timezone', 'UTC'); $start = 0; $limit = 40; $args = array(); for ($a = 0; $a < 1000; $a++) { $args[] = array('test_string' => array($a => str_repeat('a', $a)), 'test_integer' => (int) rand(), 'test_float' => (double) rand(), 'test_datetime' => new DateTime(), 'test_base64' => fXmlRpc\Value\Base64::serialize(str_repeat('a', $a))); $args[] = $a; $args[] = str_repeat('ä', $a); } $r = null; $request = null; $serializer = null; $start = microtime(true); for ($a = 0; $a < $limit; ++$a) { $request = new Zend\XmlRpc\Request(); $request->setMethod('test'); $request->setParams($args); $r = $request->saveXml(); } $end = microtime(true); printf("Zend\\XmlRpc\\Request (ZF2): %s sec for %d passes\n", $end - $start, $limit); $start = microtime(true); for ($a = 0; $a < $limit; ++$a) { $request = new Zend_XmlRpc_Request(); $request->setMethod('test'); $request->setParams($args); $r = $request->saveXml(); } $end = microtime(true); printf("Zend_XmlRpc_Request (ZF1): %s sec for %d passes\n", $end - $start, $limit);