You are here

function commerce_ups_api_request in Commerce UPS 7

Same name and namespace in other branches
  1. 7.2 includes/commerce_ups.xml.inc \commerce_ups_api_request()

Submits an API request to the Progistics XML Processor.

Parameters

$xml: An XML string to submit to the Progistics XML Processor.

$message: Optional log message to use for logged API requests.

1 call to commerce_ups_api_request()
commerce_ups_service_rate_order in ./commerce_ups.module
Shipping service callback: returns a base price array for a shipping service calculated for the given order.

File

./commerce_ups.xml.inc, line 132
Handles XML-related stuff for Commerce UPS module.

Code

function commerce_ups_api_request($xml, $message = '') {

  // Log the API request if specified.
  if (in_array('request', variable_get('commerce_ups_log', array()))) {
    if (empty($message)) {
      $message = t('Submitting API request to the UPS');
    }
    watchdog('ups', '@message:<pre>@xml</pre>', array(
      '@message' => $message,
      '@xml' => $xml,
    ));
  }
  $ch = curl_init('https://www.ups.com/ups.app/xml/Rate');
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
  $result = curl_exec($ch);

  // Log any errors to the watchdog.
  if ($error = curl_error($ch)) {
    watchdog('ups', 'cURL error: @error', array(
      '@error' => $error,
    ), WATCHDOG_ERROR);
    return FALSE;
  }
  curl_close($ch);

  // If we received data back from the server...
  if (!empty($result)) {

    // Extract the result into an XML response object.
    $response = new SimpleXMLElement($result);

    // Log the API request if specified.
    if (in_array('response', variable_get('commerce_ups_log', array()))) {
      watchdog('ups', 'API response received:<pre>@xml</pre>', array(
        '@xml' => $response
          ->asXML(),
      ));
    }
    return $response;
  }
  else {
    return FALSE;
  }
}