You are here

public static function WebServiceUtilityBase::sendRequest in Smart IP 8.2

Same name and namespace in other branches
  1. 8.4 src/WebServiceUtilityBase.php \Drupal\smart_ip\WebServiceUtilityBase::sendRequest()
  2. 8.3 src/WebServiceUtilityBase.php \Drupal\smart_ip\WebServiceUtilityBase::sendRequest()

Perform HTTP request to the Smart IP's data source module web service.

Parameters

string $url: URL provided by Smart IP's data source module web service for geolocation query.

Return value

string Raw Geolocation data returned by Smart IP's data source module web. service.

Overrides WebServiceUtilityInterface::sendRequest

1 call to WebServiceUtilityBase::sendRequest()
WebServiceUtility::getGeolocation in modules/smart_ip_ipinfodb_web_service/src/WebServiceUtility.php
Perform HTTP request and decoding the raw Geolocation data returned by Smart IP's data source module web.

File

src/WebServiceUtilityBase.php, line 19
Contains \Drupal\smart_ip\WebServiceUtilityBase.

Class

WebServiceUtilityBase
Web service utility methods class wrapper.

Namespace

Drupal\smart_ip

Code

public static function sendRequest($url = NULL) {
  if (!empty($url)) {
    try {
      $response = \Drupal::httpClient()
        ->get($url, array(
        'headers' => array(
          'Accept' => 'application/json',
        ),
      ));
      $data = (string) $response
        ->getBody();
      if (empty($data)) {
        \Drupal::logger('smart_ip')
          ->error(t('Empty response from @url', [
          '@url' => $url,
        ]));
      }
      else {
        return $data;
      }
    } catch (\Exception $e) {
      \Drupal::logger('smart_ip')
        ->error(t('Sending request failed: @error', [
        '@error' => $e
          ->getMessage(),
      ]));
    }
  }
  return '';
}