You are here

public function XmlRpcExampleClientForm::submitAddSubtract in xmlrpc 8

Submit a multicall request.

Submit a multicall request: query the XML-RPC endpoint for the methods xmlrpc_example.add and xmlrpc_example.subtract and report the result as a Drupal message. Drupal's XML-RPC client builds the system.multicall request automatically when there is more than one method to call.

Parameters

array $form: Form array.

\Drupal\Core\Form\FormStateInterface $form_state: Form_state object.

See also

xmlrpc()

xmlrpc_errno()

xmlrpc_error_msg()

xmlrpc_example_client_multicall_submit()

File

xmlrpc_example/src/Form/XmlRpcExampleClientForm.php, line 247

Class

XmlRpcExampleClientForm
Form demonstrating XML-RPC client.

Namespace

Drupal\xmlrpc_example\Form

Code

public function submitAddSubtract(array &$form, FormStateInterface $form_state) {
  $server = $this
    ->getEndpoint();

  /*
   * The XML-RPC server built in the xmlrpc module supports system.multicall.
   *
   * To make a multicall request, the main invoked method should be the
   * function 'system.multicall', and the arguments to make this call must be
   * defined as an array of single method calls, being the array keys the
   * service methods to be called, and the array elements being the method
   * arguments.
   *
   * See the code below this comment as example.
   */

  // Build an array of several calls, The built-in XML-RPC support will
  // construct the correct system.multicall request for the server.
  $options = [
    'xmlrpc_example.add' => [
      (int) $form_state
        ->getValue('num1'),
      (int) $form_state
        ->getValue('num2'),
    ],
    'xmlrpc_example.subtract' => [
      (int) $form_state
        ->getValue('num1'),
      (int) $form_state
        ->getValue('num2'),
    ],
  ];

  // Make the XML-RPC request and process the results.
  $result = xmlrpc($server, $options);
  if ($result === FALSE) {
    $this
      ->messenger()
      ->addError($this
      ->t('Error return from xmlrpc(): Error: @errno, Message: @message', [
      '@errno' => xmlrpc_errno(),
      '@message' => xmlrpc_error_msg(),
    ]));
  }
  else {
    $this
      ->messenger()
      ->addStatus($this
      ->t('The XML-RPC server returned this response: <pre>@response</pre>', [
      '@response' => print_r($result, TRUE),
    ]));
  }
}