user3121782
user3121782

Reputation: 159

Extracting URL from tweets and getting numbers of tweets containing that url

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.

if(preg_match($reg_exUrl, $tweet, $url)) {
                    preg_match_all($reg_exUrl, $tweet, $urls);
                    foreach ($urls[0] as $url) {
                    echo "Tiny url :  {$url}<br>";\
                    $full = MyURLDecode($url);
                    echo "Full url : $full<br>";
                    if (strpos($full, '//t.co') === true)                   
                        continue;
                    if (strpos($full, '//twitter.com') === true)                    
                        continue;
                    else if (strpos($full, '//bit.ly') === true)                    
                        $full = MyURLDecode($full);
                    $url_count = get_twitter_url_count($full);
                    echo "Url count: $url_count";               
                    //echo "Numbers of tweets containing this link : ", $code['count']
                    echo "<br>";
                    }
                } else {
                    echo "Mismatch<br>";
                }           

function MyURLDecode($url) 

    {

        $ch = @curl_init($url);

        @curl_setopt($ch, CURLOPT_HEADER, TRUE);

        @curl_setopt($ch, CURLOPT_NOBODY, TRUE);

        @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);

        @curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

        $url_resp = @curl_exec($ch);

        preg_match('/Location:\s+(.*)\n/i', $url_resp, $i);

        if (!isset($i[1]))

        {

        return $url;

        }

        return $i[1];

    }

    function get_twitter_url_count($url) {
        $encoded_url = urlencode($url);
        $content = @file_get_contents('http://urls.api.twitter.com/1/urls/count.json?url=' . $encoded_url);
        return $content ? json_decode($content)->count : 0;
    }

problem:

  1. If full_url is again short url then get actual long url
  2. If url is link to twitter photo like http://twitter.com/ADSPLAYINDIA/status/415847973210181632/photo/1 then skip further getting tweet count

I added continue but still it does not skip it

Upvotes: 0

Views: 166

Answers (1)

Abhijith Babu
Abhijith Babu

Reputation: 86

For the first problem try setting follow location to true in your MyURLDecode function

@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

For your second problem,i think strpos will never return true.Check out this link to a comment on php.net http://www.php.net/manual/en/function.strpos.php#107240

Please let me know if it helped

Thanks

Upvotes: 1

Related Questions