You are here

function _tweet_get_url in Tweet 5.2

Same name and namespace in other branches
  1. 6 tweet.module \_tweet_get_url()

Gets an abbreviated URL using either CURL or PHP from the appropriate service. Times out after three (3) seconds.

Parameters

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

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

Return value

An abbreviated URL.

See also

_tweet_make_url()

1 call to _tweet_get_url()
_tweet_make_url in ./tweet.module
Retrieves and beautifies the abbreviated URL.

File

./tweet.module, line 238
Builds links to post pages to twitter.

Code

function _tweet_get_url($original, $service = '') {
  if (!$service) {
    $service = variable_get('tweet_service', 'is.gd');
  }
  $skip = FALSE;
  $services = module_invoke_all('tweet_service', $original);
  foreach ($services as $name => $api) {
    if ($service == $name) {
      if (is_string($api)) {
        $url = $api . $original;
        break;
      }
      else {
        if (is_array($api) && $api['custom'] == FALSE) {
          $url = $api['url'] . $original;
          break;
        }
        else {
          if (is_array($api) && $api['custom'] == TRUE) {
            $url = $api['url'];
            $skip = TRUE;
          }
        }
      }
    }
  }

  //If the service isn't found, use the original.
  if (!$url) {
    return $original;
  }
  if (variable_get('tweet_method', 'curl') == 'php' && !$skip) {
    $context = stream_context_create(array(
      'http' => array(
        'timeout' => 3,
      ),
    ));
    $contents = file_get_contents($url, 0, $context);
  }
  else {
    if (variable_get('tweet_method', 'curl') == 'curl' && !$skip) {
      $c = curl_init();
      curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 3);
      curl_setopt($c, CURLOPT_URL, $url);
      $contents = curl_exec($c);
      curl_close($c);
    }
    else {
      $contents = $url;
    }
  }
  if ($contents && drupal_substr($contents, 0, 7) == 'http://') {
    return $contents;
  }
  $method = drupal_strtoupper(variable_get('tweet_method', 'curl'));
  if ($skip) {
    $method = t('A custom method');
  }
  watchdog('tweet', t('%method failed to return an abbreviated URL from %service.', array(
    '%method' => $method,
    '%service' => $service,
  )), WATCHDOG_NOTICE, $url);
  return FALSE;
}