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
Asà es, la clase de carro de compra te puede salvar en algún apuro donde se necesite implementar una tienda online sin necesidad de utilizar alguna de las avanzadas tiendas online disponibles en la web.
Saludos!
Hola Julian, interesante el blog que tienes 🙂
Saludos
TodavÃa no tuve que hacer ningún carrito desde que salió esta clase, pero me guardo este tutorial para cuando llegue el momento 🙂