You are here

public function InstapageCmsPluginDrupal7Connector::remoteRequest in Instapage plugin 8.3

Same name and namespace in other branches
  1. 7.3 core/connectors/InstapageCmsPluginDrupal7Connector.php \InstapageCmsPluginDrupal7Connector::remoteRequest()

Performs remote request in a way specific for Drupal 7.

Parameters

string $url URL for the request.:

array $data Data that will be passed in the request.:

array $headers Headers for the request.:

string $method Method of the request. 'POST' or 'GET'.:

Return value

array Request result in a form of associative array.

2 calls to InstapageCmsPluginDrupal7Connector::remoteRequest()
InstapageCmsPluginDrupal7Connector::remoteGet in core/connectors/InstapageCmsPluginDrupal7Connector.php
Performs remote GET request.
InstapageCmsPluginDrupal7Connector::remotePost in core/connectors/InstapageCmsPluginDrupal7Connector.php
Performs remote POST request.

File

core/connectors/InstapageCmsPluginDrupal7Connector.php, line 396

Class

InstapageCmsPluginDrupal7Connector
Class that utilizes native Drupal 7 functions to perform actions like remote requests and DB operations.

Code

public function remoteRequest($url, $data, $headers = array(), $method = 'POST') {
  $dataString = '';
  if (!isset($headers['Content-type'])) {
    $headers['Content-Type'] = 'application/x-www-form-urlencoded';
    InstapageCmsPluginHelper::writeDiagnostics($data, 'Request (' . $method . ') data empty. Ping added.');
  }
  if ($method == 'POST' && (!is_array($data) || !count($data))) {
    $data = array(
      'ping' => true,
    );
  }
  $dataString = http_build_query($data, '', '&');
  if ($method == 'GET') {
    $url .= '?' . urldecode($dataString);
    $dataString = '';
  }
  $cookies = isset($data['cookies']) ? $data['cookies'] : array();
  $cookieString = '';
  foreach ($cookies as $key => $cookie) {
    $cookieString .= $key . '=' . urlencode($cookie) . ';';
  }
  if ($cookieString) {
    $headers['Cookie'] = $cookieString;
  }
  $options = array(
    'headers' => $headers,
    'method' => $method,
    'data' => $dataString,
    'max_redirects' => 5,
    'timeout' => 45,
  );
  $request = drupal_http_request($url, $options);
  if (isset($request->code) && $request->code == 200) {
    return $this
      ->prepareResponse($request);
  }
  else {
    return [
      'body' => json_encode([
        'status' => 'ERROR',
        'message' => InstapageCmsPluginConnector::lang('Request failed with status %s.', $request->code),
      ]),
    ];
  }
}