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
Espera, ¿el objeto OAuth está en PHP? ¿Está en alguna librerÃa de Google? ¿De donde sale?
Yon, en este caso OAuth es de PECL, pero hay otras librerÃas sin necesidad de PECL
Saludos
Esto va a serme realmente util para mostrar datos en mi blog.
¡Muchas gracias!