You are here

WebServiceUtilityBase.php in Smart IP 8.4

Same filename and directory in other branches
  1. 8.2 src/WebServiceUtilityBase.php
  2. 8.3 src/WebServiceUtilityBase.php

Namespace

Drupal\smart_ip

File

src/WebServiceUtilityBase.php
View source
<?php

/**
 * @file
 * Contains \Drupal\smart_ip\WebServiceUtilityBase.
 */
namespace Drupal\smart_ip;


/**
 * Web service utility methods class wrapper.
 *
 * @package Drupal\smart_ip
 */
abstract class WebServiceUtilityBase implements WebServiceUtilityInterface {

  /**
   * {@inheritdoc}
   */
  public static function sendRequest($url = NULL) {
    if (!empty($url)) {
      try {
        $response = \Drupal::httpClient()
          ->get($url, [
          'headers' => [
            '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 '';
  }

}

Classes

Namesort descending Description
WebServiceUtilityBase Web service utility methods class wrapper.