phpspam-preventionurl-shortenergoo.gl

PHP - How to check where shortened urls are leading to (goo.gl)


Recently our forum is getting flooded by a spammer who always registers with a new identity and sending spam with a link to his website.

The website is already on our blacklist, however, now he is using Google URL shortener to spam (goo.gl/xxxx) and I wanted to ask, if Google is providing an API to resolve the shortened url (in PHP), so we can check if it leads to his website.

Is there already a PHP snipped we can use or a documentation how to prevent spamming using Google shortened links? Else I have to ban goo.gl as well.

UPDATE: Only spammers would downvote this question .!.


Solution

  • Google does provide an API that allows you to expand URLs, to use it with PHP, simply

    function unshorten_url($url) {
        $ch = curl_init('https://www.googleapis.com/urlshortener/v1/url?shortUrl='.$url);
        curl_setopt_array($ch, array(
            CURLOPT_FOLLOWLOCATION => TRUE,
            CURLOPT_RETURNTRANSFER => TRUE,
            CURLOPT_SSL_VERIFYHOST => FALSE,
            CURLOPT_SSL_VERIFYPEER => FALSE, 
        ));
    
        $json = curl_exec($ch);
        $array = json_decode($json, true);
        return $array['longUrl'];
    }
    
    echo unshorten_url('http://goo.gl/XXX');