You are here

protected function Avatax::doRequest in Drupal Commerce Connector for AvaTax 7.5

Performs a request.

Parameters

string $method: The HTTP method to use. One of: 'GET', 'POST', 'PUT', 'DELETE'.

string $path: The remote path. The base URL will be automatically appended.

array $fields: An array of fields to include with the request. Optional.

Return value

array An array with the 'success' boolean and the result. If 'success' is FALSE the result will be an error message. Otherwise it will be an array of returned data.

6 calls to Avatax::doRequest()
Avatax::addressesResolve in lib/Avatax.php
Retrieve geolocation information for a specified address.
Avatax::ping in lib/Avatax.php
Tests connectivity and version of the service.
Avatax::transactionsAdjust in lib/Avatax.php
Correct a previously created transaction.
Avatax::transactionsCommit in lib/Avatax.php
Commit a transaction for reporting.
Avatax::transactionsCreate in lib/Avatax.php
Create a new transaction.

... See full list

File

lib/Avatax.php, line 271
Defines a class for consuming the Avatax API.

Class

Avatax
Defines the Avatax class.

Code

protected function doRequest($method, $path, array $fields = array()) {
  $return = array();
  $url = $this
    ->getApiUrl() . $path;

  // Set the request URL and method.
  curl_setopt($this->ch, CURLOPT_URL, $url);
  curl_setopt($this->ch, CURLINFO_HEADER_OUT, TRUE);
  curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, $method);
  if (!empty($fields)) {

    // JSON encode the fields and set them to the request body.
    $fields = json_encode($fields);
    curl_setopt($this->ch, CURLOPT_POSTFIELDS, $fields);

    // Log the API request with the JSON encoded fields.
  }
  $result = curl_exec($this->ch);
  $response_code = curl_getinfo($this->ch, CURLINFO_HTTP_CODE);
  $success = in_array($response_code, array(
    200,
    201,
  ));

  // Log information about the request.
  $this
    ->logMessage('Request info: !url !headers !response !meta', array(
    '!url' => "<pre>URL : {$method} {$url}</pre>",
    '!headers' => "<pre>Request Headers:\n" . var_export(curl_getinfo($this->ch, CURLINFO_HEADER_OUT), TRUE) . '</pre>',
    '!response' => "<pre>Response:\n" . var_export($result, TRUE) . '</pre>',
    '!meta' => "<pre>Response Meta:\n" . var_export(curl_getinfo($this->ch), TRUE) . '</pre>',
  ));
  if (!$success) {

    // Return the error message if it exists.
    if (!empty($result)) {
      $decoded_result = json_decode($result, TRUE);

      // Return the error message if it's there.
      if (isset($decoded_result['error'])) {
        $return['error'] = $decoded_result['error'];
      }
    }
    $result = 'Error ' . $response_code;
  }
  elseif ($success && !empty($result)) {
    $result = json_decode($result, TRUE);
  }
  $return += array(
    'success' => $success,
    'result' => $result,
    'response_code' => $response_code,
  );
  return $return;
}