Acortar URLs mediante goo.gl y PHP
La API de Google para acortar URLs permite acortar URLs, recuperar info del link original y las URLs acortadas de un usuario, aunque para usarlo se necesita crear una clave y dar de alta proyecto.
El resto es fácil, acceso mediante CURL y listo:
define('GOOGLE_API_KEY', '[insert your key here]');
define('GOOGLE_ENDPOINT', 'https://www.googleapis.com/urlshortener/v1');
function shortenUrl($longUrl)
{
// initialize the cURL connection
$ch = curl_init(
sprintf('%s/url?key=%s', GOOGLE_ENDPOINT, GOOGLE_API_KEY)
);
// tell cURL to return the data rather than outputting it
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// create the data to be encoded into JSON
$requestData = array(
'longUrl' => $longUrl
);
// change the request type to POST
curl_setopt($ch, CURLOPT_POST, true);
// set the form content type for JSON data
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
// set the post body to encoded JSON data
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($requestData));
// perform the request
$result = curl_exec($ch);
curl_close($ch);
// decode and return the JSON response
return json_decode($result, true);
}
$response = shortenUrl('http://phpriot.com');
echo sprintf(
'%s was shortened to %s',
$response['longUrl'],
$response['id']
);
Shortening URLs for goo.gl with Google’s URL Shortener API
Vía / PHPDeveloper.org
Google no para de sorprendernos y en esta ocasión nos ofrece un API para incluir búsquedas normales, de videos, en blogs, en mapas y en noticias, en nuestro propio blog (bueno, o sitio web en general). Lo más destacable es que se trata de búsquedas realizadas con AJAX, por lo que la actualización se realiza de una forma muy dinámica.
Ha salido a la luz la nueva versión de PHP, en esta ocasión se trata de la versión 5.2.3, siendo recomendable su instalación.
Simple curiosidad, ¿por qué no permites poner comentarios en posts antiguos?
Porque se llenan de spam 🙁