Getting urls from tweets, converting short url to original and getting numbers of tweets containing that url


#1

Here I am getting url from tweets, converting that url to long url.

And then getting count value for numbers of tweets containing that url.

$reg_exUrl = "/(?<![\>https?:\/\/|href=\"'])(?<http>(https?:[\/][\/]|www\.)([a-z]|[A-Z]|[0-9]|[\/.]|[~])*)/";
// The Text you want to filter for urls
$text = "Make this Xmas super powerful with #Krrish3 on http://t.co/PHOdAqnzkT !\ ";
// Check if there is a url in the text

    if(preg_match($reg_exUrl, $text, $url)) {
        preg_match_all($reg_exUrl, $text, $urls);
        foreach ($urls[0] as $url) {
        echo "{$url}<br>";
        $full = MyURLDecode($url);
        echo "full is: $full<br>";
        $feedUrl = "http://urls.api.twitter.com/1/urls/count.json?url=$full";
        $json = file_get_contents($feedUrl);
        $code = json_decode($json,true);
        var_dump($code);
        echo "1";
        echo "Numbers of tweets containing this link : ", $code['count'];
        echo "2";
    }
    } else {
    echo $text;
    }

Issues:

1. Decoding some twitter tiny urls again give biturl(i.e. again tiny url)
2. Getting number of tweets(count value) containig that url. Above code give it, but for most of url it show 0 though they have count value

Any suggestion for improvement?


#2

Hi @karimkhan_it,

Challenge #2 (zero count) is because you are using a retired api. Version 1 of the REST API has been retired ( https://dev.twitter.com/docs/api/1) and version 1.1 requires OAuth authentication. Additionally there is no direct method for getting the count of a URL from a tweet unless it’s via the official tweet button or you create an app to connect to the streaming API and do your own filtering. Ref this discussion: https://dev.twitter.com/discussions/5653

Adam Green has a lot of info about working with the streaming APIs: http://140dev.com/twitter-api-programming-tutorials/aggregating-tweets-search-api-vs-streaming-api/