正在检索与Twitter帐户进行交互的用户列表


Retrieving a list of users who interacted with a Twitter account?

我在这个项目上工作了很长一段时间。让我先解释一下目标:我们有四个推特账户(与代码无关),我们办公室里有一个人花了几个小时仔细研究每个账户的互动。他记下了任何1的句柄(或API中的屏幕名称)。提到我们的帐户2。最喜欢我们的一条推特。转发我们的一条推文。我正在处理4个不同的文件(每个推特账户一个),所以现在基本上只需要专注于一个。

不必要的信息:我们正在查看最后一条#ff是什么时候发的,因为在收集了所有的句柄后,我们办公室的人发了一条推特回来,说了一些类似#ff的话,感谢@foo@bar与我们的互动。因此,一旦它在最近的推文列表中找到一条包含#ff的推文,它就会得到该推文的日期。简单地说,我们正在收集这个日期之后的所有互动(在某种程度上是每周)。

<?php
require_once('./top.php');
// oAuth library
$twitter = new TwitterAPIExchange($valpo_settings);
// get recents
$string_recents = json_decode($twitter->setGetfield($getfield.$valpo_id)->buildOauth($url_recents, 'GET')->performRequest(), $assoc = true);
// get mentions
$string_mentions = json_decode($twitter->setGetfield($getfield.$valpo_id)->buildOauth($url_mentions, 'GET')->performRequest(), $assoc = true);
// get retweets
$string_retweets = json_decode($twitter->setGetfield($getfield.$valpo_id)->buildOauth($url_retweets, 'GET')->performRequest(), $assoc = true);
// error (recents)
if($string_recents["errors"][0]["message"] != "") {echo "<h1>error (recents)</h1><p>".$string_recents[errors][0]["message"]."</p>";exit();}
// error (mentions)
if($string_mentions["errors"][0]["message"] != "") {echo "<h1>error (mentions)</h1><p>".$string_mentions[errors][0]["message"]."</p>";exit();}
// error (retweets)
if($string_retweets["errors"][0]["message"] != "") {echo "<h1>error (mentions)</h1><p>".$string_retweets[errors][0]["message"]."</p>";exit();}
// filter recents
foreach($string_recents as $items_recents) {
    // echo "<pre>";
    // print_r($items_recents);
    // echo "</pre>";
    // filter hashtags
    foreach($items_recents['entities']['hashtags'] as $items_hashtags) {
        $hashtag_text = strtolower($items_hashtags['text']);
        if ($hashtag_text == $hashtag && $vi != 1) {
            $date = date('z', strtotime($items_recents['created_at']));
            $time = date('H-i-s', strtotime($items_recents['created_at']));
            $vi = 1; // prevents finding the date of previous tweets
        }
    }
}
// filter mentions
foreach($string_mentions as $items_mentions) {
    if (date('z', strtotime($items_mentions['created_at'])) == $date && date('H-i-s', strtotime($items_mentions['created_at'])) >= $time || date('z', strtotime($items_mentions['created_at'])) > $date) {
        $names[] .= $items_mentions['user']['screen_name'];
        $vxm++;
    }
}
// filter retweets
foreach($string_retweets as $items_retweets) {
    echo "<pre>";
    print_r($items_retweets);
    echo "</pre>";
    foreach ($items_retweets['entities']['user_mentions'] as $items_retweets_user) {
        if (date('z', strtotime($items_retweets['created_at'])) == $date && date('H-i-s', strtotime($items_retweets['created_at'])) >= $time || date('z', strtotime($items_retweets['created_at'])) > $date) {
            $names[] .= $items_retweets_user['screen_name'];
            $vxr++;
        }
    }
}
// eliminates duplicate handles
$unames = array_unique($names);
// creates directory if needed
if (!is_dir("./".date('Y-m-d'))) {
    mkdir("./".date('Y-m-d'), 0777, true);
}
$file = fopen("./".date('Y-m-d')."/".date('H-i-s')."-valpo.txt", "w");
foreach ($unames as $value) {
    fwrite($file, '@'.$value.' ');
    $unames_size += strlen($value) + 2; // + 2 accomadates for @ and a space
}
fclose($file);
$unames_size = ceil($unames_size / 140);
echo "
            <div class='row'>
                <div class='col x3'></div>
                <div class='col x6'>
                    <span class='title'>ValpoLife</span>
                    <span class='info'>
                        <p><i class='fa fa-comments'></i> ".$vxm." &nbsp; <i class='fa fa-star'></i> ".$vxm." &nbsp; <i class='fa fa-retweet'></i> ".$vxr." &nbsp; <i class='fa fa-user'></i> ".count($unames)."</p>
                        <p><i class='fa fa-slack'></i> ".$hashtag." &nbsp; <i class='fa fa-calendar-o'></i> ".($curdate - $date)." days ago</p>
                        <p>Appoximately ".$unames_size." tweet(s) needed</p>
                    </span>
                    <a href='./index.php' class='button sec animated' target='blank'><i class='fa fa-chevron-left'></i></a><a href='./".date('Y-m-d')."/".date('H-i-s')."-valpo.txt' class='button pri animated' target='blank'><i class='fa fa-file-text'></i> View handles</a>
                </div>
            </div>
            <footer>
                &copy; Ideas In Motion Media. All Rights Reserved.
            </footer>
        </div>
    </body>
</html>
";
?>

以上是其中一个帐户文件的所有代码。正如你所看到的,它包括top.php,它只是有一些全局变量、html的头部和每个帐户的oAuth信息。现在,除了检索收藏夹和转发外,一切都正常。提及效果很好,最近提及也很好。我尝试了几种方法来获得收藏夹或转发,但似乎都无济于事。我已经删除了上面收藏夹的代码,因为我更关心获得转发。如果需要收藏夹代码,请告诉我,因为我很乐意分享它。谢谢你的帮助。

tl;dr

使用推特API,我成功地检索到了最近的推文和任何提到我们推特账户的推文。现在,我正在尝试检索喜欢或转发我们推文的人的姓名/账号。

可以获得转发推文的每个人的姓名。

https://dev.twitter.com/rest/reference/get/statuses/retweeters/ids

给它一个推特的ID,你会得到转发它的人的名单。

对于最喜欢的,你不会喜欢这个答案。没有REST API可以让你看到谁喜欢推特。

当您的推文被标记为收藏夹时,您可以使用流媒体API接收消息。

https://dev.twitter.com/streaming/overview/messages-types#Events_event