Micaela 22 de Enero de 2009 a las 09.00
   Imprimir artículo
elWebmaster.com

C贸mo crear un acortador de URLs

Seguramente alguna vez has utilizando un acortador de direcciones URLs, son servicios que nos dan la posibilidad de generar direcciones web cortas para poder recordarlas m谩s f谩cilmente.

Desde el sitio web de David Walsh nos ense帽an una muy sencilla forma para que podamos crear nuestro propio acortador de URLs utilizando simplemente PHP, MooTools-JavaScript- y TinyURL.

El XHTML:

1.聽聽聽 <p><strong>URL:</strong> <input type="text" id="url" size="40" /> <input type="button" id="geturl" value="Get URL" /></p>
2.聽聽聽 <p id=”newurl”></p>

El c贸digo PHP:

1.聽聽聽 if(isset($_GET['url']))
2.聽聽聽 {
3.聽聽聽 聽聽聽 die(get_tiny_url(urldecode($_GET['url'])));
4.聽聽聽 }
5.
6.聽聽聽 //gets the data from a URL
7.聽聽聽 function get_tiny_url($url)
8.聽聽聽 {
9.聽聽聽 聽聽聽 $ch = curl_init();
10.聽聽聽 聽聽聽 $timeout = 5;
11.聽聽聽 聽聽聽 curl_setopt($ch,CURLOPT_URL,’http://tinyurl.com/api-create.php?url=’.$url);
12.聽聽聽 聽聽聽 curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
13.聽聽聽 聽聽聽 curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
14.聽聽聽 聽聽聽 $data = curl_exec($ch);
15.聽聽聽 聽聽聽 curl_close($ch);
16.聽聽聽 聽聽聽 return $data;
17.聽聽聽 }

El Javascript MooTools:

1.聽聽聽 window.addEvent('domready',function() {
2.聽聽聽 聽聽聽 var TinyURL = new Class({
3.聽聽聽 聽聽聽聽聽聽聽 //implements
4.聽聽聽 聽聽聽聽聽聽聽 Implements: [Options],
5.聽聽聽 聽聽聽聽聽聽聽 //options
6.聽聽聽 聽聽聽聽聽聽聽 options: {
7.聽聽聽 聽聽聽聽聽聽聽聽聽聽聽 checkURL: ”
8.聽聽聽 聽聽聽聽聽聽聽 },
9.聽聽聽 聽聽聽聽聽聽聽 //initialization
10.聽聽聽 聽聽聽聽聽聽聽 initialize: function(options) {
11.聽聽聽 聽聽聽聽聽聽聽聽聽聽聽 //set options
12.聽聽聽 聽聽聽聽聽聽聽聽聽聽聽 this.setOptions(options);
13.聽聽聽 聽聽聽聽聽聽聽 },
14.聽聽聽 聽聽聽聽聽聽聽 //a method that does whatever you want
15.聽聽聽 聽聽聽聽聽聽聽 createURL: function(url,complete) {
16.聽聽聽 聽聽聽聽聽聽聽聽聽聽聽 var req = new Request({
17.聽聽聽 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 url: this.options.checkURL + ‘?url=’ + url,
18.聽聽聽 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 method: ‘get’,
19.聽聽聽 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 async: false,
20.聽聽聽 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 onComplete: function(response) { complete(response); }
21.聽聽聽 聽聽聽聽聽聽聽聽聽聽聽 }).send();
22.聽聽聽 聽聽聽聽聽聽聽 }
23.聽聽聽 聽聽聽 });
24.
25.
26.聽聽聽 聽聽聽 // usage //
27.聽聽聽 聽聽聽 var new_tiny_url = new TinyURL({
28.聽聽聽 聽聽聽聽聽聽聽 checkURL: ‘grab-tiny-url.php’
29.聽聽聽 聽聽聽 });
30.
31.聽聽聽 聽聽聽 $(’geturl’).addEvent(’click’,function() {
32.聽聽聽 聽聽聽聽聽聽聽 if($(’url’).value) {
33.聽聽聽 聽聽聽聽聽聽聽聽聽聽聽 var newu = new_tiny_url.createURL($(’url’).value,function(resp) {
34.聽聽聽 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 $(’newurl’).set(’html’,'The TinyURL is <a href=”%27%20+%20resp%20+%20%27″>’ + resp + ‘</a>.聽 Go ahead, try it!’).setStyle(’color’,'green’);
35.聽聽聽 聽聽聽聽聽聽聽聽聽聽聽 });
36.聽聽聽 聽聽聽聽聽聽聽 }
37.聽聽聽 聽聽聽 });
38.聽聽聽 });

checkURL,鈥 es la URL del snippet PHP de arriba. La acci贸n real sucede cuando se invoca el m茅todo createURL() que pasa el m茅todo a URL hasta que se achica y una funci贸n 鈥渙nComplete鈥 que toma lugar cuando la URL se recibe.

Fuente: David Walsh


Enviar a Del.icio.us Enviar a Meneame Enviar a Digg Enviar a Fresqui Enviar a Enchilame

Comentarios (2)

  1. jj dice:

    Con este c贸digo no creas tu propio reductor de URLs. Lo que haces es utilizar tinyURL. 脡so es usar un reductor, no crear el tuyo.

  2. Ivor dice:

    Por favor pongan ejemplos de como se ver谩-!! Gracias

Deja tu opinión

© 2007 - 2008 elWebmaster.com | Powered by Wordpress | Diseño CSS y XHTML válido. | Algunos íconos basados en FamFamFam Mini
Iniciar sesi贸n