You are here

function wsclient_tester_prepare_request_callback in Web service client 7

Callback for the test from 'prepare' button.

1 string reference to 'wsclient_tester_prepare_request_callback'
wsclient_tester_operation_test in wsclient_tester/wsclient_tester.inc
FAPI form used to display the options and the results of calling a web service.

File

wsclient_tester/wsclient_tester.inc, line 103
Utility functions for running the tester UI for web services.

Code

function wsclient_tester_prepare_request_callback($form, $form_state) {
  $service = $form_state['build_info']['args'][0];
  $operation = $form_state['build_info']['args'][1];

  // Convert the form values into a data structure suitable for making the query.
  // Magic?
  // service::invoke will cast this array into the correct paramaterized objects
  // According to the $operation['parameter'] schema. Nifty.
  $args = array();
  if (!empty($form_state['values']['parameters'])) {
    $args = $form_state['values']['parameters'];
  }
  if ($service->type == 'soap' || $service->type == 'soap 1.2') {

    // The service will have an endpoint that will have a SOAPClient.
    // Settings on the service->options may be passed to the SOAPClient.
    // @see WSClientSOAPEndpoint::client()
    // @see http://www.php.net/manual/en/soapclient.soapclient.php
    $service->settings['options']['trace'] = TRUE;

    // disable cache when testing!
    $service->settings['options']['cache_wsdl'] = WSDL_CACHE_NONE;
  }

  // Ready to actually invoke the call
  $timer_before = microtime();
  try {
    $response = $service
      ->invoke($operation['name'], $args);
  } catch (Exception $e) {
    $response = $e
      ->getMessage();
  }
  $timer_duration = $timer_before - microtime();
  $element = $form['transaction'];

  // Place the trace data into the display.
  if ($service->type == 'rest') {
    $operation = $service->operations[$operation['name']];
    $parameters = '';
    foreach ($args as $para_name => $para_value) {
      $parameters .= $para_name . '=' . $para_value . '<br>';
    }
    $reqInfo = 'Uri:' . $service->url . $operation['url'] . '<br>' . 'Method:' . (isset($operation['type']) ? $operation['type'] : 'GET') . '<br>' . 'Parameter:' . $parameters;
    $element['request']['packet']['#markup'] = $reqInfo;
    $element['response']['header']['#markup'] = $service
      ->endpoint()
      ->client()->lastResponse->headers;
    $element['response']['packet']['#markup'] = $service
      ->endpoint()
      ->client()->lastResponse->body;
  }
  elseif ($service->type == 'soap' || $service->type == 'soap 1.2') {
    $element['request']['header']['#markup'] = $service
      ->endpoint()
      ->client()
      ->__getLastRequestHeaders();
    $element['request']['packet']['#markup'] = htmlspecialchars(wsclient_tester_prettify_xml($service
      ->endpoint()
      ->client()
      ->__getLastRequest()));
    $element['response']['header']['#markup'] = $service
      ->endpoint()
      ->client()
      ->__getLastResponseHeaders();
    $element['response']['packet']['#markup'] = htmlspecialchars(wsclient_tester_prettify_xml($service
      ->endpoint()
      ->client()
      ->__getLastResponse()));
  }
  if (module_exists('devel')) {
    $element['request']['data']['#markup'] = kpr($args, 1);
    $element['response']['data']['#markup'] = kpr($response, 1);
  }
  $element['#value'] = t("Ran at %time, took %duration to execute", array(
    '%time' => time(),
    '%duration' => $timer_duration,
  ));
  return $element;
}