You are here

function _shorten_googl in Shorten URLs 8

Same name and namespace in other branches
  1. 8.2 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 241
Shortens URLs via external services.

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 = array(
    'method' => 'POST',
    'data' => json_encode(array(
      'longUrl' => $original,
    )),
    'context' => $context,
    'headers' => array(
      '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.', array());
  return FALSE;
}