Dernière mise à jour: jeu 30 sep 2010

make_googl_url()

(PHP 5)

Raccourci une URL avec le service Google URL Shortener.

Source

<?php

function make_googl_url($url)
{
    $parse_url = parse_url($url);
    if( empty($parse_url['scheme']) ) return FALSE;
    
    $content = http_build_query(array('url' => $url));
    $headers  = 'Content-Type: application/x-www-form-urlencoded' . "\r\n";
    $headers .= 'Content-Length: ' . strlen($content);
    
    // Création du flux
    $opts = array(
        'http'=>array(
            'method'        => 'POST',
            'header'        => $headers,
            'content'       => $content,
            'timeout'       => 1,
            'max_redirects' => 1,
        )
    );
    $context = stream_context_create($opts);
    
    // Récupération du contenu
    @file_get_contents('http://goo.gl/api/shorten', false, $context);
    
    // Recherche de l'url raccourcie
    foreach ($http_response_header as $header_response) {
        if (stripos($header_response, 'Location:') === 0) {
            return preg_replace('`Location:[\s]*`i', '', $header_response);
        }
    }
    
    // Erreur
    return false;
}

?>

Syntaxe

string make_googl_url ( string $url )

Arguments

  1. url - L'URL à raccourcir.

Valeurs de retour

Retourne l'URL raccourcie ou FALSE si une erreur est survenue.

Exemples

Exemple #1 Exemple avec make_googl_url()

<?php

$url = "http://seebz.net/";
$url = make_googl_url($url);
echo $url; // http://goo.gl/zZb2

?>

Voir aussi

  • make_bitly_url() - Raccourci une URL en utilisant le service « bit.ly URL Shortener »
  • make_isgd_url() - Raccourci une URL en utilisant le service « is.gd URL Shortener »
  • make_tiny_url() - Raccourci une URL en utilisant le service « TinyURL Shortener »
  • make_unu_url() - Raccourci une URL en utilisant le service « u.nu URL Shortener »

Commentaire(s)

Il n'y a aucun commentaire pour cette page.

Poster un commentaire