dsgs8208 2013-12-26 10:52
浏览 66
已采纳

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

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+(.*)
/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

  • 写回答

1条回答 默认 最新

  • duan0417 2013-12-26 11:31
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘