You are here

public function XmlRpcExampleClientForm::submitAdd in xmlrpc 8

Submit handler to query xmlrpc_example.add.

Submit: query the XML-RPC endpoint for the method xmlrpc_example.add and report the result as a Drupal message.

Parameters

array $form: Form array.

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

See also

xmlrpc()

xmlrpc_errno()

xmlrpc_error_msg()

File

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

Class

XmlRpcExampleClientForm
Form demonstrating XML-RPC client.

Namespace

Drupal\xmlrpc_example\Form

Code

public function submitAdd(array &$form, FormStateInterface $form_state) {

  // First define the endpoint of the XML-RPC service. In this case this is
  // our own server.
  $server = $this
    ->getEndpoint();

  // Then we should define the method to call. xmlrpc() requires that all the
  // information related to the called method is passed as an array in the
  // form of 'method_name' => arguments_array.
  $options = [
    'xmlrpc_example.add' => [
      (int) $form_state
        ->getValue('num1'),
      (int) $form_state
        ->getValue('num2'),
    ],
  ];

  // Make the xmlrpc 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: @response', [
      '@response' => print_r($result, TRUE),
    ]));
  }
}