/** * Constructs a new XML-RPC client view * @param zibo\xmlrpc\form\ClientForm $form The form of the client * @param zibo\library\xmlrpc\Request $request * @param zibo\library\xmlrpc\Response $response * @param string $responseString * @param float $time Time spent on the request in seconds * @return null */ public function __construct(ClientForm $form, Request $request = null, Response $response = null, $responseString = null, $time = null) { parent::__construct(self::TEMPLATE); $this->set('form', $form); $this->set('time', $time); $this->set('requestString', null); $this->set('responseString', null); if ($request) { $this->set('requestString', String::addLineNumbers(trim($request->getXmlString()))); } if ($responseString && !$response) { $this->set('responseString', String::addLineNumbers(trim($responseString))); } elseif ($response) { $this->set('responseString', String::addLineNumbers(trim($response->getXmlString()))); $errorCode = $response->getErrorCode(); if ($errorCode) { $this->set('error', $errorCode . ': ' . $response->getErrorMessage()); } else { $parser = new ParameterParser(); $result = $parser->unparse($response->getValue(), true); $this->set('result', $result); } } $this->addStyle(self::STYLE); }
public function testInvokePhpxmlrpcTestServerExampleEchoesString() { $client = new Client(self::PHPXMLRPC_TEST_SERVER); $request = new Request(self::PHPXMLRPC_TEST_METHOD); $string = 'testing 1 2 3'; $request->addParameter($string); $response = $client->invoke($request); $this->assertSame($string, $response->getValue()); }
/** * Invokes the provided request on the XML-RPC server * @param Request $request XML-RPC request * @return Response The response from the server */ public function invoke(Request $request) { $requestXml = $request->getXmlString(); $connection = @fsockopen($this->host, $this->port, $errno, $errstr); if (!$connection) { throw new ConnectionException($this->host, $this->port, $errstr, $errno); } $contentLength = strlen($requestXml); $headers = array("POST {$this->path} HTTP/1.0", "User-Agent: Zibo", "Host: {$this->host}", "Connection: close", "Content-Type: text/xml", "Content-Length: {$contentLength}"); fwrite($connection, implode("\r\n", $headers)); fwrite($connection, "\r\n\r\n"); fwrite($connection, $requestXml); $response = ''; while (!feof($connection)) { $response .= fgets($connection, 1024); } fclose($connection); #strip headers off of response $responseXml = substr($response, strpos($response, "\r\n\r\n") + 4); $response = Response::fromXMLString($responseXml); return $response; }
/** * Get a XML-RPC request for the provided method and parameters * @param string $method full name of the method * @param string $parameters submitted parameters in string format, ready to be parsed * @return zibo\library\xmlrpc\Request the request for the XML RPC server * @throws zibo\library\validation\exception\ValidationException when the parameters could not be parsed */ private function getXmlrpcRequest($method, $parameters) { $request = new Request($method); if (!$parameters) { return $request; } $parser = new ParameterParser(); try { $parameters = $parser->parse($parameters); } catch (ZiboException $exception) { $error = new ValidationError(self::TRANSLATION_ERROR_PARAMETERS, 'Could not parse the parameters: %error%', array('error' => $exception->getMessage())); $validationException = new ValidationException(); $validationException->addErrors(ClientForm::FIELD_PARAMETERS, array($error)); throw $validationException; } foreach ($parameters as $parameter) { $request->addParameter($parameter); } return $request; }
/** * Gets a module from the repository * @param string $namespace The namespace of the module * @param string $name The name of the module * @return Module * @throws Exception when the module could not be retrieved */ public function getModule($namespace, $name) { $request = new Request(RepositoryModule::SERVICE_PREFIX . RepositoryModule::SERVICE_MODULE_INFO); $request->addParameter(new Value($namespace)); $request->addParameter(new Value($name)); $exception = null; $module = null; foreach ($this->clients as $url => $client) { $response = $client->invoke($request); if ($response->getErrorCode() !== 0) { if (!$exception) { $exception = new ZiboException('Repository (' . $url . ') returned a fault with code ' . $response->getErrorCode() . ' and message: ' . $response->getErrorMessage()); } else { $exception = new ZiboException('Repository (' . $url . ') returned a fault with code ' . $response->getErrorCode() . ' and message: ' . $response->getErrorMessage(), 0, $exception); } continue; } $module = $this->getModuleFromArray($response->getValue()); $module->setRepository($url); break; } if ($exception) { throw $exception; } return $module; }
/** * Invokes the provided request * @param Request $request The XML-RPC request * @return Response The response with the resulting value or a error message */ public function invokeRequest(Request $request) { $zibo = Zibo::getInstance(); $methodName = $request->getMethodName(); if (!isset($this->services[$methodName])) { $error = 'Unknown method ' . $methodName; $zibo->runEvent(Zibo::EVENT_LOG, $error, '', 1, self::LOG_NAME); return new Response(null, 1, $error); } $callback = $this->services[$methodName][self::SERVICE_CALLBACK]; $returnType = $this->services[$methodName][self::SERVICE_RETURN_TYPE]; $parameterTypes = $this->services[$methodName][self::SERVICE_PARAMETERS_TYPES]; try { $parameters = $this->getCallbackParameters($request->getParameters(), $parameterTypes); } catch (XmlRpcException $e) { $zibo->runEvent(Zibo::EVENT_LOG, 'Invalid call to ' . $methodName, $e->getMessage(), 1, self::LOG_NAME); return new Response(null, 3, $methodName . ': ' . $e->getMessage()); } try { $parameterString = ''; if ($parameters) { foreach ($parameters as $parameter) { if (is_scalar($parameter)) { $parameterString .= $parameter . ', '; continue; } $parameterString .= gettype($parameter) . ', '; } $parameterString = substr($parameterString, 0, -2); } $zibo->runEvent(Zibo::EVENT_LOG, 'Invoking ' . $callback . '(' . $parameterString . ')', '', 0, self::LOG_NAME); $result = $callback->invokeWithArrayArguments($parameters); $response = new Response(new Value($result, $returnType)); } catch (Exception $exception) { $error = $exception->getMessage(); if (!$error) { $error = get_class($exception); } $error = $methodName . ': ' . $error; $zibo->runEvent(Zibo::EVENT_LOG, $error, $exception->getTraceAsString(), 1, self::LOG_NAME); $response = new Response(null, 200 + $exception->getCode(), $error); } return $response; }
/** * Gets the request from the provided request structure * @param array $requestStruct Array with a key methodName and a key params with their respective values * @return Request */ protected function getRequestFromStruct(array $requestStruct) { if (!isset($requestStruct[self::MULTICALL_METHOD_NAME])) { throw new XmlRpcException('Could not find element ' . self::MULTICALL_METHOD_NAME . ' in the request struct.'); } $methodName = $requestStruct[self::MULTICALL_METHOD_NAME]; if (empty($methodName) || !is_string($methodName)) { throw new XmlRpcException('Provided method name is empty or invalid.'); } $parameters = array(); if (isset($requestStruct[self::MULTICALL_PARAMS])) { if (!is_array($requestStruct[self::MULTICALL_PARAMS])) { throw new XmlRpcException('Provided parameters are invalid, array expected.'); } $parameters = $requestStruct[self::MULTICALL_PARAMS]; } $request = new Request($methodName); foreach ($parameters as $parameter) { $request->addParameter($parameter); } return $request; }