简体   繁体   English

掌握最新的Twitter结果

[英]Grabbing latest Twitter results

Hi I made a simple script that inserts the most recent id of a twitter post into a textfile, which then compares it too the last 100 tweets so it only retreieves the latest ones. 嗨,我做了一个简单的脚本,该脚本将Twitter帖子的最新ID插入文本文件,然后将其与最后100条Tweet进行比较,以便仅检索最新的Tweet。

I have noticed that sometimes when i run this script it does not break the loop and just displays all of the old tweets again. 我注意到有时运行该脚本时,它不会中断循环,而仅再次显示所有旧的推文。

Is their a better approach to such a problem? 他们是解决这种问题的更好方法吗?

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

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


$json= $connection->get("https://api.twitter.com/1.1/search/tweets.json?q=%23Drake&count=100");

$newcount=0;    
$flag;

$id = array();
$since_filename = 'twitter/since_filename.txt';
$count_filename = 'twitter/count_filename.txt';


$since_id = file_get_contents($since_filename);

foreach($json->statuses as $tweet)
{
  $text = $tweet->text; 
  $test = $tweet->id_str;
  $id[] =  $test;


  if($test==$since_id)
  {
    break;
  }
  else
    {
        echo "false";
    }

echo $text;

  $newcount++;
}

$current = $id[0];
// Write the contents back to the file
file_put_contents($since_filename, $current);

$existingcount = file_get_contents($count_filename);
file_put_contents($count_filename, ($newcount + $existingcount));

Twitter's search API allows you to pass a 'since_id' Twitter的搜索API允许您传递“ since_id”

since_id - Returns results with an ID greater than (that is, more recent than) the specified ID. since_id-返回ID大于(即比指定ID更新)ID的结果。 There are limits to the number of Tweets which can be accessed through the API. 可通过API访问的推文数量有限制。 If the limit of Tweets has occured since the since_id, the since_id will be forced to the oldest ID available. 如果自since_id以来已发生Tweets限制,则since_id将被强制为可用的最早ID。

$json= $connection->get("https://api.twitter.com/1.1/search/tweets.json?q=%23Drake&since_id=$since_id&count=100");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM