You are here

public function WSConnectorSOAP::call in Web Service Data 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/WSConnector/WSConnectorSOAP.php \Drupal\wsdata\Plugin\WSConnector\WSConnectorSOAP::call()

Make the connector call.

Overrides WSConnectorSimpleHTTP::call

File

src/Plugin/WSConnector/WSConnectorSOAP.php, line 143

Class

WSConnectorSOAP
REST Connector.

Namespace

Drupal\wsdata\Plugin\WSConnector

Code

public function call($options, $method, $replacements = [], $data = null, array $tokens = []) {
  $token_service = \Drupal::token();
  $uri = $this->endpoint . '/' . $options['path'];

  // Perform the token replace on the headers.
  if (!empty($options['headers'])) {
    for ($i = 0; $i < count($options['headers']); $i++) {
      if (!empty($options['headers'][$i]['key_' . $i])) {
        $options['headers'][$options['headers'][$i]['key_' . $i]] = $token_service
          ->replace($options['headers'][$i]['value_' . $i], $tokens);
      }
      unset($options['headers'][$i]['key_' . $i]);
      unset($options['headers'][$i]['value_' . $i]);
      unset($options['headers'][$i]);
    }
    if (count($replacements) == count($replacements, COUNT_RECURSIVE)) {
      $payload = array_merge($replacements, $options['headers']);
    }
    else {

      // array is multidimensional
      $payload = $replacements;
      foreach ($options['headers'] as $k => $v) {
        $payload[array_keys($replacements)[0]][$k] = $v;
      }
    }
  }
  if (isset($options['method']) && !empty($options['method'])) {
    $method = $options['method'];
  }
  if (isset($options['wsdl']) && !empty($options['wsdl'])) {
    $wsdl = DRUPAL_ROOT . $options['wsdl'];
  }
  else {
    $wsdl = $uri;
  }
  $result = FALSE;
  try {
    if (isset($options["user"]) && isset($options["key"])) {
      $service = new \SoapClient($wsdl, array(
        'login' => $options["user"],
        'password' => $options["key"],
      ));
    }
    else {
      $service = new \SoapClient($wsdl);
    }
    if (isset($options['wsdl']) && !empty($options['wsdl']) && isset($options['path']) && !empty($options['path'])) {
      $service
        ->__setLocation($uri);
    }
    if (!is_soap_fault($service)) {
      $result = $service
        ->__soapCall($method, !empty($options['headers']) ? array(
        $payload,
      ) : array(
        $replacements,
      ));
    }
  } catch (\Throwable $th) {
    $message = $this
      ->t('SOAP call: Could not call endpoint: :uri and method: @method', [
      ':uri' => $uri,
      '@method' => $method,
    ]);
    $this
      ->setError(1, $message);
  }
  return $result;
}