How to generate short url using PHP and Google API

    <form method="post" action="">
        <input type="text" name="url_val">
        <input type="submit" name="submit_url">

PHP Code

    $apiKey = 'Your API Key';

    $data = array('longUrl' => $long_Url, 'key' => $apiKey);
    $jsonData = json_encode($data);

    $curlObj = curl_init();

    curl_setopt($curlObj, CURLOPT_URL, '');
    curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curlObj, CURLOPT_HEADER, 0);
    curl_setopt($curlObj, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
    curl_setopt($curlObj, CURLOPT_POST, 1);
    curl_setopt($curlObj, CURLOPT_POSTFIELDS, $jsonData);

    $response = curl_exec($curlObj);
    $json = json_decode($response);


    echo 'Your Short URL is: '.$json->id;

Leave a Reply

Your email address will not be published. Required fields are marked *