You are here

public function InstapageCmsPluginWPConnector::remoteRequest in Instapage plugin 8.3

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

Performs remote request in a way specific for WordPress.

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 InstapageCmsPluginWPConnector::remoteRequest()
InstapageCmsPluginWPConnector::remoteGet in core/connectors/InstapageCmsPluginWPConnector.php
Performs remote GET request.
InstapageCmsPluginWPConnector::remotePost in core/connectors/InstapageCmsPluginWPConnector.php
Performs remote POST request.

File

core/connectors/InstapageCmsPluginWPConnector.php, line 200

Class

InstapageCmsPluginWPConnector
Class that utilizes native WordPress functions to perform actions like remote requests and DB operations.

Code

public function remoteRequest($url, $data, $headers = array(), $method = 'POST') {
  $body = is_array($data) ? $data : (array) $data;
  if ($method == 'POST' && (!is_array($body) || !count($body))) {
    $body = array(
      'ping' => true,
    );
    InstapageCmsPluginHelper::writeDiagnostics($body, 'Request (' . $method . ') data empty. Ping added.');
  }
  if ($method == 'GET' && is_array($data)) {
    $dataString = http_build_query($body, '', '&');
    $url .= '?' . urldecode($dataString);
    $body = null;
    InstapageCmsPluginHelper::writeDiagnostics($url, 'GET Request URL');
  }
  $cookies = isset($data['cookies']) ? $data['cookies'] : array();
  $args = array(
    'method' => $method,
    'timeout' => 45,
    'redirection' => 5,
    'httpversion' => '1.0',
    'sslverify' => false,
    'blocking' => true,
    'headers' => $headers,
    'body' => $body,
    'cookies' => $cookies,
  );
  switch ($method) {
    case 'POST':
      $response = wp_remote_post($url, $args);
      break;
    case 'GET':
      $response = wp_remote_get($url, $args);
      break;
    default:
      $response = null;
  }
  if (is_wp_error($response)) {
    return array(
      'body' => json_encode([
        'status' => 'ERROR',
        'message' => $response
          ->get_error_message(),
      ]),
    );
  }
  else {
    return $this
      ->prepareResponse($response);
  }
}