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

混江龙づ霸主 提交于 2019-12-22 05:04:28

问题


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.
        )

)

回答1:


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);


来源:https://stackoverflow.com/questions/29214063/goo-gl-url-shortener-stopped-working-php-curl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!