You are here

function _shorten_googl in Shorten URLs 8.2

Same name and namespace in other branches
  1. 8 shorten.module \_shorten_googl()
  2. 7 shorten.module \_shorten_googl()

Helps get a shortened URL from Goo.gl.

1 string reference to '_shorten_googl'
shorten_shorten_service in ./shorten.module
Implements hook_shorten_service().

File

./shorten.module, line 251

Code

function _shorten_googl($original) {
  $url = 'https://www.googleapis.com/urlshortener/v1/url?key=' . \Drupal::config('shorten.settings')
    ->get('shorten_googl');
  $context = stream_context_create();
  stream_context_set_option($context, 'ssl', 'verify_host', TRUE);
  $options = [
    'method' => 'POST',
    'data' => json_encode([
      'longUrl' => $original,
    ]),
    'context' => $context,
    'headers' => [
      'Content-type' => 'application/json',
    ],
  ];
  $googl = shorten_fetch($url, 'id', 'json', $options);
  if ($googl) {
    return $googl;
  }
  \Drupal::logger('shorten')
    ->error('Error fetching shortened URL from goo.gl.', []);
  return FALSE;
}