Question

Now I load the tweets from twitter by using a username like this: (keys and tokens are filled in my code)

<?php
session_start();
require_once("twitteroauth/twitteroauth.php"); // PATH TO TWITTEROUTH LIBRARY

if(isset($_GET['twitteruser']) && !empty($_GET['twitteruser'])) {
    $twitteruser = $_GET['twitteruser']; // USER
}
$notweets = 60; // NUMBER OF TWEETS

// OAUTH SETTINGS APPLICATION (https://dev.twitter.com/apps/)
$consumerkey = "";
$consumersecret = "";
$accesstoken = "";
$accesstokensecret = "";

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

$tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$notweets);

$data =  json_encode($tweets);

?>

As you can see I load the tweets with a username and set the number of tweets.
Can this easily be changed to load tweets with a search term?

I've search on google but strangely I can't find much.

I've found this in the twitter API but that doesn't seem to work ..

https://api.twitter.com/1.1/search/tweets.json?q=%23freebandnames&since_id=24012619984051000&max_id=250126199840518145&result_type=mixed&count=4

source: https://dev.twitter.com/docs/api/1.1/get/search/tweets
(I changed the link to that link)

But I get this output with a var_dump:

string(319) "{"statuses":[],"search_metadata":{"completed_in":0.004,"max_id":2.50126199841e+17,"max_id_str":"250126199840518145","query":"%23freebandnames","refresh_url":"?since_id=250126199840518145&q=%23freebandnames&result_type=mixed&include_entities=1","count":4,"since_id":2.40126199841e+16,"since_id_str":"24012619984051000"}}".....

Was it helpful?

Solution

The Twitter search API only returns tweets from the past week or so. See more info here.

The Search API is not complete index of all Tweets, but instead an index of recent Tweets. At the moment that index includes between 6-9 days of Tweets.

Note that the search feature on Twitter's website does query historical tweets, it is just the API that does not support them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top