HTMLeando: para desarrolladores Web

Pregunta


¿Cómo usar la API de TinyURL? 

Responder esta pregunta por dudin el 2009-01-27
Estoy planeando usar la API de un servicio para acortar URLs, pues a veces colocan URLs demasiado largas en los comentarios de mi blog. ¿Alguna idea de cómo podría hacerse con PHP usando TinyURL.com u otro servicio?

Respuestas

Thumb_up
Thumb_down

0%
0%
He encontrado esta función que tal vez te resulte útil:
function tinyUrl($large_url){	
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, "http://tinyurl.com/api-create.php?url=".($large_url));
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_TIMEOUT, 5);
	$short_url = curl_exec($ch);
	curl_close($ch);
	if(empty($short_url)) return $large_url; else return $short_url;
	}
por Anónimo el 2009-01-27

Thumb_up
Thumb_down

0%
0%
También puedes intentarlo usando el servicio de http://bit.ly, usando esta clase para PHP5:
<?php
/* 
Tweetly Updater requires PHP 5 >= 5.2.0 and the curl module for PHP. 
	$bitly = new Bitly('bitlyuser', 'bitlyapikey');
	 $shortlink = $bitly->getBitlyUrl($thispostlink);
Basado en Plugin URI: http://www.zepan.org/software/tweetly-updater
*/
class Bitly {
    public $bitlyApiUrl = "http://api.bit.ly";
    public $debug = false;
    private $bitlyUser;
    private $bitlyApiKey;
 
    function __construct( $bitlyUser, $bitlyApiKey) {
        $this->bitlyUser = $bitlyUser;
        $this->bitlyApiKey = $bitlyApiKey;
    }
 
 
    public function bitlyVerifyCredentials() {
        $bitlyObject = json_decode($this->curlGet($this->bitlyApiUrl . "/shorten?version=" . $this->bitlyApiVersion . "&longUrl=http://zepan.org&login=" . $this->bitlyUser . "&apiKey=" . $this->bitlyApiKey , '', ''));
        if ($bitlyObject->{'statusCode'} == 'OK') {
            return true;	
        }
        return false;
    }
 
    public function getBitlyUrl($longurl) {
        $shortUrl = $longurl;
 
        if($this->bitlyUser != '' && $this->bitlyApiKey != '') {
            $bitlyResp = $this->curlGet($this->bitlyApiUrl . "/shorten?version=" . $this->bitlyApiVersion . "&history=1&longUrl=" . $longurl . "&login=" . $this->bitlyUser . "&apiKey=" . $this->bitlyApiKey, '', '');
            $bitlyObject = json_decode($bitlyResp);
            if ($bitlyObject->{'statusCode'} == 'OK') {
                $shortUrl = $bitlyObject->{'results'}->{$longurl}->{'shortUrl'}; 	
            } else {
                error_log("bit.ly shorten request failed");                
            }
        } else {
                error_log("bit.ly login data incomplete");        
        }
 
        return $shortUrl;
    }
 
    private function curlGet($url, $user, $password) {
        if ($this->debug) {
                error_log("curlGet with url: " . $url);
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        if ($user != '' && $password != '') {
            if ($this->debug) {    
                error_log("Using authentication");
            }
            curl_setopt($ch, CURLOPT_USERPWD, $user . ":" . $password);
            curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($ch);
        if ($this->debug) {
                error_log("curl GET response: " . $data);
        }
        curl_close($ch);                 
        return $data;
    }
 
    private function curlPost($url, $fields, $user, $password) {
        if ($this->debug) {
                error_log("curlPOST with url: " . $url);
        }
        foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }  
        rtrim($fields_string,'&');  
 
        $ch = curl_init();  
 
        curl_setopt($ch,CURLOPT_URL,$url);  
        curl_setopt($ch,CURLOPT_POST,count($fields));  
        curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
 
        if ($user != '' && $password != '') {
            if ($this->debug) {    
                error_log("Using authentication");
            }
            curl_setopt($ch, CURLOPT_USERPWD, $user . ":" . $password);
            curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
        }
 
        $result = curl_exec($ch);
        if ($this->debug) {
                error_log("curl POST response: " . $result);
        }    
        curl_close($ch);
        return $result;
    }
 
}
?>
por Anónimo el 2009-01-27

Thumb_up
Thumb_down

0%
0%
Otro encode y decode de TinyUrl:
<?php
 
function reverse_tinyurl($url){
   $url = explode('.com/', $url);
   $url = 'http://preview.tinyurl.com/'.$url[1];
   $preview = file_get_contents($url);
   preg_match('/redirecturl" href="(.*)">/',$preview,$matches);
   return $matches[1];
}
 
function tinyurl($url){
   $html = file_get_contents("http://tinyurl.com/create.php?url=".$url);
   preg_match('/http:\/\/preview\.tinyurl\.com\/(.*)<\/b>/',$html,$matches);
   return "http://tinyurl.com/".$matches[1];
}
 
echo reverse_tinyurl("http://tinyurl.com/1c2");
 
echo tinyurl("http://www.google.com");
 
?>
por maikel el 2009-02-03

Anónimo:   Entrar


Consejo: Sé tan específico y descriptivo como puedas. Comparte tu experiencia personal o tus conocimientos.
Puedes investigar para completar tu respuesta.

Puedes poner directamente trozos de código usando las etiquetas [code=php] y [/code] o incrustar desde Snipplr.com [snippet=id_snippet]

Compartir: WebeameMeneame Delicious Digg

Preguntas relacionadas