Performs one or more XML-RPC requests.
string $url: An absolute URL of the XML-RPC endpoint, e.g., http://example.com/xmlrpc.php
array $args: An associative array whose keys are the methods to call and whose values are the arguments to pass to the respective method. If multiple methods are specified, a system.multicall is performed.
array $headers: (optional) An array of HTTP headers to pass along.
A single response (single request) or an array of responses (multicall request). Each response is the return value of the method, just as if it has been a local function call, on success, or FALSE on failure. If FALSE is returned, see xmlrpc_errno() and xmlrpc_error_msg() to get more information.
function _xmlrpc($url, array $args, array $headers = array()) {
xmlrpc_clear_error();
if (count($args) > 1) {
$multicall_args = array();
foreach ($args as $method => $call) {
$multicall_args[] = array(
'methodName' => $method,
'params' => $call,
);
}
$method = 'system.multicall';
$args = array(
$multicall_args,
);
}
else {
$method = key($args);
$args = $args[$method];
}
$xmlrpc_request = xmlrpc_request($method, $args);
$request = Drupal::httpClient()
->post($url, $headers, $xmlrpc_request->xml);
$request
->setHeader('Content-Type', 'text/xml');
try {
$response = $request
->send();
} catch (BadResponseException $exception) {
$response = $exception
->getResponse();
xmlrpc_error($response
->getStatusCode(), $response
->getReasonPhrase());
return FALSE;
} catch (RequestException $exception) {
xmlrpc_error(NULL, $exception
->getMethod());
return FALSE;
}
$message = xmlrpc_message($response
->getBody(TRUE));
// Now parse what we've got back
if (!xmlrpc_message_parse($message)) {
// XML error
xmlrpc_error(-32700, t('Parse error. Not well formed'));
return FALSE;
}
// Is the message a fault?
if ($message->messagetype == 'fault') {
xmlrpc_error($message->fault_code, $message->fault_string);
return FALSE;
}
// We now know that the message is well-formed and a non-fault result.
if ($method == 'system.multicall') {
// Return per-method results or error objects.
$return = array();
foreach ($message->params[0] as $result) {
if (array_keys($result) == array(
0,
)) {
$return[] = $result[0];
}
else {
$return[] = xmlrpc_error($result['faultCode'], $result['faultString']);
}
}
}
else {
$return = $message->params[0];
}
return $return;
}