从推文中提取网址并获取包含该网址的推文数量

时间:2013-12-26 10:52:55

标签: php url twitter

这里我从推文获取网址,将该网址转换为长网址。

然后获取包含该网址的推文数量的计数值。

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

问题:

  1. 如果full_url再次是短网址,则获取实际的长网址
  2. 如果网址链接到像http://twitter.com/ADSPLAYINDIA/status/415847973210181632/photo/1这样的Twitter照片,那么请跳过进一步的推文计数
  3. 我添加了continue,但仍然没有跳过它

1 个答案:

答案 0 :(得分:1)

对于第一个问题,请尝试在MyURLDecode函数中将跟随位置设置为true

@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

对于你的第二个问题,我认为strpos永远不会返回true。查看这个链接到php.net上的评论http://www.php.net/manual/en/function.strpos.php#107240

如果有帮助请告诉我

由于