Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Goo.gl URL Shortener Stopped Working (php/curl)

Tags:

php

curl

goo.gl

For some reason my script stopped working today. When I look in the API control panel says I still have 100% left of usage. Any ideas? Did they change the auth way?

function url_small($url)
    {
        //This is the URL you want to shorten
        $longUrl = $url;
        $apiKey = '#####HIDDEN######';
        //Get API key from : http://code.google.com/apis/console/

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

        $curlObj = curl_init();

        curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/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);

        //change the response json string to object
        $json = json_decode($response);
        curl_close($curlObj);

        return $json->id;
    }

Response

stdClass Object
(
    [error] => stdClass Object
        (
            [errors] => Array
                (
                    [0] => stdClass Object
                        (
                            [domain] => usageLimits
                            [reason] => dailyLimitExceededUnreg
                            [message] => Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.
                            [extendedHelp] => https://code.google.com/apis/console
                        )

                )

            [code] => 403
            [message] => Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.
        )

)
like image 392
RichardW11 Avatar asked Mar 23 '15 15:03

RichardW11


1 Answers

My below answer is no longer valid. Google now makes you use Firebase for URL Shortening. Easy to setup.

https://firebase.google.com/docs/dynamic-links/rest


So it turns out this old function that is displayed in multiple websites now needs the api key to be displayed in the URL section too for google to register the request to your account.

curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url');

switched to this

curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url?key='.$apiKey);
like image 144
RichardW11 Avatar answered Nov 08 '22 04:11

RichardW11