【问题标题】:Extracting URL from tweets and getting numbers of tweets containing that url从推文中提取 URL 并获取包含该 URL 的推文数量
【发布时间】:2014-01-14 00:50:00
【问题描述】:

在这里,我从推文中获取 url,将该 url 转换为长 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;
    }

问题:

  1. 如果full_url 又是短网址,则获取实际的长网址
  2. 如果 url 是 twitter 照片的链接,例如 http://twitter.com/ADSPLAYINDIA/status/415847973210181632/photo/1,则跳过进一步获取推文计数

我添加了continue,但它仍然没有跳过它

【问题讨论】:

    标签: php url twitter


    【解决方案1】:

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

    @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    

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

    如果有帮助请告诉我

    谢谢

    【讨论】:

    • 谢谢,但请您详细说明一下。这些都没有造成任何差异
    猜你喜欢
    • 2019-08-17
    • 1970-01-01
    • 2012-07-28
    • 2020-09-07
    • 1970-01-01
    • 2020-01-09
    • 2020-08-12
    • 2021-07-25
    • 2017-04-12
    相关资源
    最近更新 更多