dongpao5658 2014-10-22 07: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-24 06: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);
    

    }

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

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器