You are here

function _shorten_googl in Shorten URLs 7

Same name and namespace in other branches
  1. 8.2 shorten.module \_shorten_googl()
  2. 8 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 312
Shortens URLs via external services.

Code

function _shorten_googl($original) {
  $url = 'https://www.googleapis.com/urlshortener/v1/url?key=' . variable_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;
  }
  watchdog('shorten', 'Error fetching shortened URL from goo.gl.', array(), WATCHDOG_ERROR, $url);
  return FALSE;
}