Create Function Google Shorterner

copy this function into your code

function get_googl($url){
    $apikey = 'yourApiKeyFromGoogle';
    $asset = array('longUrl' => $url);
    $object = json_encode($asset);
    $google = 'https://www.googleapis.com/urlshortener/v1/url?key='.$apikey;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $google);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $object);
    $data = curl_exec($ch);
    curl_close($ch);
    $response = json_decode($data);
    $resulturl = $response->id;
    return $resulturl;
}

then using this function

<?php

echo get_googl('http://www.example.com/long-url-blabla-john-doe');
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s