WordPress: usare le API di Google per abbreviare gli URL

WordPress: usare le API di Google per abbreviare gli URL

Google ci permette di utilizzare le sue API per abbreviare gli URL. Possiamo utilizzare questa funzionalità, che al momento permette un milione di richieste al giorno, per abbreviare gli URL dei nostri post di WordPress.

Possiamo definire la seguente classe che andremo ad inserire nel file functions.php o in un file esterno da richiamare:


class GoogleURLShortener {

    /**
     * URL delle API
     *
     * @var string URL API
     */
    private $_url = 'https://www.googleapis.com/urlshortener/v1/url';

    /**
     * Chiave delle API
     * Vedi: https://code.google.com/apis/console/
     *
     * @var string chiave API
     */
    private $_apikey = '';

    /**
     * Imposta la chiave delle API
     *
     * @param string $key chiave API
     * @return void
     */
    public function __construct( $key)
    {
        $this->_apikey = $key;
    }

    public function shortenURL( $longurl)
    {     
       // crea l'URL della richiesta
       $url = $this->_url . '?key=' . $this->_apikey;

       return $this->_getResponse( $url, '{"longUrl": "' . $longurl . '"}');
    }

    public function expandURL( $shorturl)
    {
       // crea l'URL della richiesta
       $url = $this->_url . '?shortUrl=' . $shorturl;

       return $this->_getResponse( $url );

    }

    public function getStats( $shorturl)
    {
       // crea l'URL della richiesta
       $url = $this->_url . '?shortUrl=' . $shorturl . '&projection=FULL';

       return $this->_getResponse( $url );

    }

    /**
     * Apre l'URL e restituisce l'output in JSON
     *
     * @param string $url
     * @return risposta JSON
     */
    private function _getResponse( $url, $data = false) {

       
        $ch = curl_init( $url);

        
        curl_setopt( $ch, CURLOPT_HEADER, 0);
        curl_setopt( $ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; Windows NT 5.1) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31');
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);

        
        if($data !== false)
        {
            curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-Type: application/json'));
            curl_setopt($ch,CURLOPT_POST,count($data));
            curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
        }

        ob_start();
        $output = curl_exec($ch);
        ob_end_clean();

        curl_close ($ch);

        return $output;

    }

}

La classe funziona in questo modo:


$google_shortener = new GoogleURLShortener('la tua chiave API');
$long_url = 'https://gabrieleromanato.com/2013/04/javascript-impostare-la-scadenza-dei-cookie-in-ore/';
$json_response = $google->shortenURL($long_url);

Il metodo GoogleURLShortener::shorten() restituisce una stringa JSON:


{
 "kind": "urlshortener#url",
 "id": "http://goo.gl/rFFVH",
 "longUrl": "https://gabrieleromanato.com/2013/04/javascript-impostare-la-scadenza-dei-cookie-in-ore/"
}

L'URL abbreviato è contenuto nella proprietà ID. Quindi nei nostri post possiamo scrivere:


<?php
$google_shortener = new GoogleURLShortener('la tua chiave API');
$long_url = get_permalink();
$json_response = $google->shortenURL($long_url);
$response = json_decode($json_response, true);
$short_url = $response['id'];
?>

<a href="<?php echo $short_url; ?>">Short link</a>

Torna su