You are here

function commerce_usps_api_request in Commerce USPS 7

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

Submits an API request to USPS.

Parameters

string $request: A request string.

string $message: Optional log message.

Return value

string XML string response from USPS

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

File

./commerce_usps.xml.inc, line 81
Handles rate request/response related stuff for the Commerce USPS module.

Code

function commerce_usps_api_request($request, $message = '') {

  // Log the API request if specified.
  // @todo: Add ability for admin to toggle logging of request.
  // if (in_array('request', variable_get('commerce_usps_log', array()))) {
  if (empty($message)) {
    $message = t('Submitting API request to USPS.');
  }
  watchdog('usps', '@message:<pre>@request</pre>', array(
    '@message' => $message,
    '@request' => $request,
  ));

  // }
  // Send the request.
  $response = drupal_http_request(variable_get('commerce_usps_connection_address', 'http://Production.ShippingAPIs.com/ShippingAPI.dll'), array(
    'method' => 'POST',
    'data' => $request,
  ));

  // If we received a response.
  if (!empty($response->data)) {

    // Log the API request if specified.
    // @todo: Add ability for admin to toggle logging of response.
    // if (in_array('response', variable_get('commerce_usps_log', array()))) {
    watchdog('usps', 'Response code:@code<br />Response:<pre>@response</pre>', array(
      '@code' => $response->code,
      '@response' => $response->data,
    ));

    // }
    // Return the response as an XML response object.
    return new SimpleXMLElement($response->data);
  }
  return FALSE;
}