I am working on the twitter hashtag search API project using twitter Oauth by Abraham Williams.
I am getting the hashtag tweets but i have one problem.
For example, there are only 2 tweets that have been entered into the hashtag, but my function gets latest 100 tweets and keep updating the database(i have managed duplicate items).
How to use since_id and max_id for eliminating the way of getting remaining tweets. My code
//Hashtag Specification
$search = "#KejriwalFirSe";
$notweets = 100;
//Hastag Connection Starts
$search = str_replace("#", "%23", $search);
$tweets = $connection->get("https://api.twitter.com/1.1/search/tweets.json? q=".$search."&count=".$notweets.);
//Parsing the twitter Data
$string1 =json_encode($tweets);
$string = json_decode($string1, true);
echo count($tweets);
foreach($string['statuses'] as $tweet)
{
$text = trim($tweet['text']);
$id =$tweet['id'];
$user_name=$tweet['user']['screen_name'];
$profile_image=$tweet['user']['profile_image_url'];
$user_id=$tweet['user']['id'];
//TimeZone for india
$date = new DateTime($tweet['created_at']);
$date->setTimezone(new DateTimeZone('Asia/Kolkata'));
$formatted_date = $date->format('h:i, M d');
echo "<br>";
echo "<img src='".$profile_image. "'/>".' '. $id .'<br/> '.$user_name.'<br/> ' . $text.' <br/>'.$formatted_date ."</br><br/>";
//Conditional Statements for inserting and updating
$select_query=$con->query("SELECT Twitter_status FROM Twitter_status WHERE Tweet_ID = '$user_id'") or die (mysqli_error());
$result = $select_query->num_rows;
if(!$result){
//Inserting the data in to the table
$ins_sql="INSERT INTO Twitter_status (Tweet_ID,Twitter_User,Twitter_status,Twitter_Profile,TotalTweets_User) VALUES ('$user_id','$user_name','$text','$profile_image',1)";
$con->query($ins_sql);
}
else
{
$con->query("UPDATE Twitter_status SET Twitter_status='updated12',TotalTweets_User=TotalTweets_User+1 WHERE Tweet_ID='$user_id'");
}
}
After lot of attempts i solved the error,
i passed an array which traverses through tweets and store status id of last tweet in a last_id (some variable to store).
I stored last_id in a session variable in order to pass that variable value after refreshing and pass that value to since_id.
When load it for first time , it stores Null value and then it stores session id
if (!isset($_SESSION['last_id'])) {
$_SESSION['last_id'] = NULL;
echo "NULL";
} else {
echo $_SESSION['last_id'];
$last_id=$_SESSION['last_id'];
$tweets = $connection->get("https://api.twitter.com/1.1/search/tweets.json?q=".$search."&count=".$notweets."&since_id=".$last_id);
}