You are here

function _shorten_get_url in Shorten URLs 7

Same name and namespace in other branches
  1. 8.2 shorten.module \_shorten_get_url()
  2. 8 shorten.module \_shorten_get_url()
  3. 6 shorten.module \_shorten_get_url()
  4. 7.2 shorten.module \_shorten_get_url()

Shortens URLs. Times out after three (3) seconds.

Parameters

$original: The URL of the page for which to retrieve the abbreviated URL.

$api: A string or array used to retrieve a shortened URL. If it is an array, it can have the elements 'custom,' 'url,' 'tag,' 'json,' and 'args.'

$service: The service to use to abbreviate the URL. For services available by default, see shorten_shorten_service().

Return value

An abbreviated URL.

1 call to _shorten_get_url()
shorten_url in ./shorten.module
Retrieves and beautifies the abbreviated URL. This is the main API function of this module.

File

./shorten.module, line 206
Shortens URLs via external services.

Code

function _shorten_get_url($original, $api, $service) {
  $method = drupal_strtoupper(variable_get('shorten_method', _shorten_method_default()));
  $service = t('an unknown service');
  if (is_string($api)) {
    $url = shorten_fetch($api . urlencode($original));
    $service = $api;
  }
  elseif (is_array($api)) {

    // Merge in defaults.
    $api += array(
      'custom' => FALSE,
      'json' => FALSE,
    );
    if (!empty($api['url'])) {
      $original = urlencode($original);

      // Typically $api['custom'] == 'xml' although it doesn't have to.
      if (!empty($api['tag'])) {
        $url = shorten_fetch($api['url'] . $original, $api['tag']);
      }
      elseif (!empty($api['json'])) {
        $url = shorten_fetch($api['url'] . $original, $api['json'], 'json');
      }
      elseif (!$api['custom']) {
        $url = shorten_fetch($api['url'] . $original);
      }
      $service = $api['url'];
    }
    elseif (is_string($api['custom']) && function_exists($api['custom'])) {
      $method = t('A custom method: @method()', array(
        '@method' => $api['custom'],
      ));
      if (!empty($api['args']) && is_array($api['args'])) {
        $args = $api['args'];
        array_unshift($args, $original);
        $url = call_user_func_array($api['custom'], $args);
      }
      else {
        $url = call_user_func($api['custom'], $original);
      }
    }
  }
  if ($url) {
    if (drupal_substr($url, 0, 7) == 'http://' || drupal_substr($url, 0, 8) == 'https://') {
      return $url;
    }
  }
  watchdog('shorten', '%method failed to return an abbreviated URL from %service.', array(
    '%method' => $method,
    '%service' => $service,
  ), WATCHDOG_NOTICE, $url);
  return FALSE;
}