You are here

function _xmlrpc in xmlrpc 8

Performs one or more XML-RPC requests.

Parameters

string $url: Absolute URL of the XML-RPC endpoint, e.g. http://example.com/xmlrpc .

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.

Return value

mixed 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.

1 call to _xmlrpc()
xmlrpc in ./xmlrpc.module
Performs one or more XML-RPC request(s).

File

./xmlrpc.inc, line 581
Drupal XML-RPC library.

Code

function _xmlrpc($url, array $args, array $headers = []) {
  xmlrpc_clear_error();
  if (count($args) > 1) {
    $multicall_args = [];
    foreach ($args as $method => $call) {
      $multicall_args[] = [
        'methodName' => $method,
        'params' => $call,
      ];
    }
    $method = 'system.multicall';
    $args = [
      $multicall_args,
    ];
  }
  else {
    $method = key($args);
    $args = $args[$method];
  }
  $xmlrpc_request = xmlrpc_request($method, $args);
  $headers['Content-Type'] = 'text/xml; charset=utf-8';
  try {
    $response = \Drupal::httpClient()
      ->post($url, [
      'headers' => $headers,
      'body' => $xmlrpc_request->xml,
    ]);
  } catch (RequestException $exception) {
    xmlrpc_error(-32300, $exception
      ->getMessage());
    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 = [];
    foreach ($message->params[0] as $result) {
      if (array_keys($result) == [
        0,
      ]) {
        $return[] = $result[0];
      }
      else {
        $return[] = xmlrpc_error($result['faultCode'], $result['faultString']);
      }
    }
  }
  else {
    $return = $message->params[0];
  }
  return $return;
}