dongpao5658 2014-10-21 23:07
浏览 70
已采纳

Twitter Hashtag搜索使用php和sql

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'");
}   

}

展开全部

  • 写回答

1条回答 默认 最新

  • dtwye28880 2014-10-23 22:05
    关注

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

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部