(PHP 5)
Raccourci une URL avec le service Google URL Shortener.
<?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; } ?>
string make_googl_url ( string $url )
Retourne l'URL raccourcie ou FALSE si une erreur est survenue.
Exemple #1 Exemple avec make_googl_url()
<?php $url = "http://seebz.net/"; $url = make_googl_url($url); echo $url; // http://goo.gl/zZb2 ?>
Commentaire(s)
Poster un commentaire