dsaff82024 2019-03-02 04:08
浏览 140

Twitter API删除类似的推文

I need to develop a script to get brand mentions from Twitter. So I want to search tweet which contain my brand mention or keywords. It's work fine but I need to remove similar tweets. I tried this code but doesn't work as I would like:

session_start();
require_once("twitteroauth/twitteroauth.php"); //Path to twitteroauth library

$twitteruser = "xxx";
$notweets = 30;
$consumerkey = "xxxx";
$consumersecret = "xxx";
$accesstoken = "xxx";
$accesstokensecret = "xxx";



//use the php similar_text function to remove similar tweets

function removeSimilar ($array, $minSimilarity = 60) {
    $result = [];

    foreach ($array as $outerValue) {
        $append = true;
        foreach ($result as $key => $innerValue) {
            $similarity = null;
            similar_text($innerValue, $outerValue, $similarity);
            if ($similarity >= $minSimilarity) {
                if (strlen($outerValue) > strlen($innerValue)) {
                    // always keep the longer one
                    $result[$key] = $outerValue;
                }
                $append = false;
                break;
            }
        }

        if ($append) {
            $result[] = $outerValue;
        }
    }

    return $result;
}


function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) {
  $connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
  return $connection;
}

$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);

$tweets = $connection->get("https://api.twitter.com/1.1/search/tweets.json?q=london+exclude:retweets+exclude:replies&since=2019-03-01&count=100");

$encode = json_encode($tweets);
$decode = json_decode($encode,true);

$decode = removeSimilar($decode);

foreach($decode['statuses'] as $tweets) {
    echo '<p>' . $tweets['text'] . '</p>';
    }

Can someone help me? I know the issue is on the variable $decode but i honestly I don't know how to resolve it.

Thanks so much!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 高德地图点聚合中Marker的位置无法实时更新
    • ¥15 DIFY API Endpoint 问题。
    • ¥20 sub地址DHCP问题
    • ¥15 delta降尺度计算的一些细节,有偿
    • ¥15 Arduino红外遥控代码有问题
    • ¥15 数值计算离散正交多项式
    • ¥30 数值计算均差系数编程
    • ¥15 redis-full-check比较 两个集群的数据出错
    • ¥15 Matlab编程问题
    • ¥15 训练的多模态特征融合模型准确度很低怎么办