Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search Tweets with since_id and max_id #3

Open
giselajesslim opened this issue Jun 17, 2015 · 7 comments
Open

Search Tweets with since_id and max_id #3

giselajesslim opened this issue Jun 17, 2015 · 7 comments

Comments

@giselajesslim
Copy link

Hi,

Where I can find api with since and max id for seach tweets?
Because I need it.
I also see your tweet.php you only support your defined entities.
Is there anyway I can take all return data from twitter with entities?
Thanks.

@ivanvermeyen
Copy link
Contributor

Hello,

I only included a few basic options in my class, but you could copy Twitter.php to your own project, namespace it and make sure it extends CodeZero\Twitter\TwitterBase.

Then edit the searchTweets method to fit your needs. The max_id and since_id options can be added to the following $data array:

https://github.com/codezero-be/twitter/blob/master/src/Twitter.php#L69-L72

All available options can be found here:

https://dev.twitter.com/rest/reference/get/search/tweets

You'll probably want to create your own ServiceProvider to bootstrap your custom Twitter class:

https://github.com/codezero-be/twitter/blob/master/src/TwitterServiceProvider.php

Finally, to get the raw results instead of the custom entities, you can set $returnEntities = false; and pass it as a last parameter to the searchTweets method.

https://github.com/codezero-be/twitter#set-options

If you need more advanced features, maybe take a look at this package that I found recently:

https://github.com/logical-and/php-oauth

Hope this helps :)

Ivan

@giselajesslim
Copy link
Author

I did all your instructions before ask. haha. I think there is another way.
thanks.

@ivanvermeyen
Copy link
Contributor

Did you get an exception? If you can tell me some specifics, perhaps I can help to find a solution. :)

@giselajesslim
Copy link
Author

Here is my code.
$take means that how many code already running.

$take = 1;
$since_id = 0;
$max_id = 0;
$tweets = $twitter->searchTweets($clean_query, $count, $cacheMinutes, $returnEntities, $take, $since_id, $max_id);

I modified yours.

public function searchTweets($query, $count = 10, $cacheMinutes = 30, $returnEntities = true, $take = 1, $sinceId = 0, $maxId = 0)
    {
        $count = $this->getVerifiedCount($count, 1, 100);
        $endpoint = '/search/tweets.json';
        $url = $this->urlHelper->joinSlugs([$this->baseUrl, $this->apiVersion, $endpoint]);
        if($take <= 1){
            $data = [
                'q' => $query,
                'count' => $count
            ];
        }
        elseif($take == 2){
            $data = [
                'q' => $query,
                'count' => $count,
                'since_id' => $sinceId
            ];
        }
        elseif($take >= 3){
            $data = [
                'q' => $query,
                'count' => $count,
                'since_id' => $sinceId,
                'max_id' => $maxId,
            ];
        }
        
        $headers = [
            'Authorization' => 'Bearer ' . $this->requestAppAccessToken()
        ];
        
        $response = $this->courier->get($url, $data, $headers, $cacheMinutes);
        if ($returnEntities)
        {
            $tweets = $response->toArray();
            $response = $this->createTweetEntities($tweets['statuses']);
        }
        return $response;
    }

This is my result for the first take for "GameOfThronesFood Thrones" query.

array (size=30)
  0 => 
    array (size=11)
      'user_id' => int 609708038
      'screen_name' => string '' (length=0)
      'user_name' => string 'Katchiiez' (length=9)
      'campaign_id' => string '12345678902' (length=11)
      'tweet_id' => float 6.1125446381943E+17
      'tweet_text' => string 'RT @LordSnow: Pizza of Thrones #GameOfThronesFood http://t.co/qQwGZJ6wMQ' (length=72)
      'tweet_date' => string '2015-06-18 02:29:55' (length=19)
      'favorited_count' => int 0
      'retweet_count' => int 896
      'url' => string '' (length=0)
      'hashtags' => 
        array (size=1)
          0 => 
            array (size=1)
              'hashtag' => string 'GameOfThronesFood' (length=17)
  1 => 
    array (size=11)
      'user_id' => int 340262579
      'screen_name' => string '' (length=0)
      'user_name' => string 'S.♏' (length=5)
      'campaign_id' => string '12345678902' (length=11)
      'tweet_id' => float 6.1124689850314E+17
      'tweet_text' => string 'RT @LordSnow: Pizza of Thrones #GameOfThronesFood http://t.co/qQwGZJ6wMQ' (length=72)
      'tweet_date' => string '2015-06-18 01:59:51' (length=19)
      'favorited_count' => int 0
      'retweet_count' => int 896
      'url' => string '' (length=0)
      'hashtags' => 
        array (size=1)
          0 => 
            array (size=1)
              'hashtag' => string 'GameOfThronesFood' (length=17)
  2 => 
    array (size=11)
      'user_id' => int 1903848818
      'screen_name' => string '' (length=0)
      'user_name' => string '† Lαdy S †' (length=15)
      'campaign_id' => string '12345678902' (length=11)
      'tweet_id' => float 6.1123912786292E+17
      'tweet_text' => string 'RT @LordSnow: Pizza of Thrones #GameOfThronesFood http://t.co/qQwGZJ6wMQ' (length=72)
      'tweet_date' => string '2015-06-18 01:28:59' (length=19)
      'favorited_count' => int 0
      'retweet_count' => int 896
      'url' => string '' (length=0)
      'hashtags' => 
        array (size=1)
          0 => 
            array (size=1)
              'hashtag' => string 'GameOfThronesFood' (length=17)
  3 => 
    array (size=11)
      'user_id' => int 946573717
      'screen_name' => string '' (length=0)
      'user_name' => string 'FeuNek' (length=6)
      'campaign_id' => string '12345678902' (length=11)
      'tweet_id' => float 6.1112564454667E+17
      'tweet_text' => string 'RT @LordSnow: Pizza of Thrones #GameOfThronesFood http://t.co/qQwGZJ6wMQ' (length=72)
      'tweet_date' => string '2015-06-17 17:58:02' (length=19)
      'favorited_count' => int 0
      'retweet_count' => int 896
      'url' => string '' (length=0)
      'hashtags' => 
        array (size=1)
          0 => 
            array (size=1)
              'hashtag' => string 'GameOfThronesFood' (length=17)
  4 => 
    array (size=11)
      'user_id' => int 616259265
      'screen_name' => string '' (length=0)
      'user_name' => string 'Beckham❤' (length=10)
      'campaign_id' => string '12345678902' (length=11)
      'tweet_id' => float 6.1111538403328E+17
      'tweet_text' => string 'RT @LordSnow: Pizza of Thrones #GameOfThronesFood http://t.co/qQwGZJ6wMQ' (length=72)
      'tweet_date' => string '2015-06-17 17:17:16' (length=19)
      'favorited_count' => int 0
      'retweet_count' => int 896
      'url' => string '' (length=0)
      'hashtags' => 
        array (size=1)
          0 => 
            array (size=1)
              'hashtag' => string 'GameOfThronesFood' (length=17)
  5 => 
    array (size=11)
      'user_id' => float 2384671057
      'screen_name' => string '' (length=0)
      'user_name' => string 'Azael heaven ' (length=13)
      'campaign_id' => string '12345678902' (length=11)
      'tweet_id' => float 6.1101714984746E+17
      'tweet_text' => string 'RT @LordSnow: Pizza of Thrones #GameOfThronesFood http://t.co/qQwGZJ6wMQ' (length=72)
      'tweet_date' => string '2015-06-17 10:46:55' (length=19)
      'favorited_count' => int 0
      'retweet_count' => int 896
      'url' => string '' (length=0)
      'hashtags' => 
        array (size=1)
          0 => 
            array (size=1)
              'hashtag' => string 'GameOfThronesFood' (length=17)
  6 => 
    array (size=11)
      'user_id' => int 187452087
      'screen_name' => string '' (length=0)
      'user_name' => string 'Eve' (length=3)
      'campaign_id' => string '12345678902' (length=11)
      'tweet_id' => float 6.1098357462849E+17
      'tweet_text' => string 'RT @LordSnow: Pizza of Thrones #GameOfThronesFood http://t.co/qQwGZJ6wMQ' (length=72)
      'tweet_date' => string '2015-06-17 08:33:30' (length=19)
      'favorited_count' => int 0
      'retweet_count' => int 896
      'url' => string '' (length=0)
      'hashtags' => 
        array (size=1)
          0 => 
            array (size=1)
              'hashtag' => string 'GameOfThronesFood' (length=17)
  7 => 
    array (size=11)
      'user_id' => int 187166554
      'screen_name' => string '' (length=0)
      'user_name' => string 'Aakassh R Makhija' (length=17)
      'campaign_id' => string '12345678902' (length=11)
      'tweet_id' => float 6.1097312860956E+17
      'tweet_text' => string 'RT @LordSnow: Pizza of Thrones #GameOfThronesFood http://t.co/qQwGZJ6wMQ' (length=72)
      'tweet_date' => string '2015-06-17 07:51:59' (length=19)
      'favorited_count' => int 0
      'retweet_count' => int 896
      'url' => string '' (length=0)
      'hashtags' => 
        array (size=1)
          0 => 
            array (size=1)
              'hashtag' => string 'GameOfThronesFood' (length=17)
  8 => 
    array (size=11)
      'user_id' => int 720034193
      'screen_name' => string '' (length=0)
      'user_name' => string 'Eduardo Sanchez' (length=15)
      'campaign_id' => string '12345678902' (length=11)
      'tweet_id' => float 6.1095243117901E+17
      'tweet_text' => string 'RT @LordSnow: Pizza of Thrones #GameOfThronesFood http://t.co/qQwGZJ6wMQ' (length=72)
      'tweet_date' => string '2015-06-17 06:29:45' (length=19)
      'favorited_count' => int 0
      'retweet_count' => int 896
      'url' => string '' (length=0)
      'hashtags' => 
        array (size=1)
          0 => 
            array (size=1)
              'hashtag' => string 'GameOfThronesFood' (length=17)
  9 => 
    array (size=11)
      'user_id' => float 2571577238
      'screen_name' => string '' (length=0)
      'user_name' => string '☾ Yami' (length=8)
      'campaign_id' => string '12345678902' (length=11)
      'tweet_id' => float 6.1092481685038E+17
      'tweet_text' => string 'RT @LordSnow: Pizza of Thrones #GameOfThronesFood http://t.co/qQwGZJ6wMQ' (length=72)
      'tweet_date' => string '2015-06-17 04:40:01' (length=19)
      'favorited_count' => int 0
      'retweet_count' => int 896
      'url' => string '' (length=0)
      'hashtags' => 
        array (size=1)
          0 => 
            array (size=1)
              'hashtag' => string 'GameOfThronesFood' (length=17)

For example if I try to get tweets with since_id : 6.1123912786292E+17 (3rd data in array)

$take = 1;
$since_id = 6.1123912786292E+17;
$max_id = 0;
$tweets = $twitter->searchTweets($clean_query, $count, $cacheMinutes, $returnEntities, $take, $since_id, $max_id);

The result should be

array (size=30)
  0 => 
    array (size=11)
      'user_id' => int 609708038
      'screen_name' => string '' (length=0)
      'user_name' => string 'Katchiiez' (length=9)
      'campaign_id' => string '12345678902' (length=11)
      'tweet_id' => float 6.1125446381943E+17
      'tweet_text' => string 'RT @LordSnow: Pizza of Thrones #GameOfThronesFood http://t.co/qQwGZJ6wMQ' (length=72)
      'tweet_date' => string '2015-06-18 02:29:55' (length=19)
      'favorited_count' => int 0
      'retweet_count' => int 896
      'url' => string '' (length=0)
      'hashtags' => 
        array (size=1)
          0 => 
            array (size=1)
              'hashtag' => string 'GameOfThronesFood' (length=17)
  1 => 
    array (size=11)
      'user_id' => int 340262579
      'screen_name' => string '' (length=0)
      'user_name' => string 'S.♏' (length=5)
      'campaign_id' => string '12345678902' (length=11)
      'tweet_id' => float 6.1124689850314E+17
      'tweet_text' => string 'RT @LordSnow: Pizza of Thrones #GameOfThronesFood http://t.co/qQwGZJ6wMQ' (length=72)
      'tweet_date' => string '2015-06-18 01:59:51' (length=19)
      'favorited_count' => int 0
      'retweet_count' => int 896
      'url' => string '' (length=0)
      'hashtags' => 
        array (size=1)
          0 => 
            array (size=1)
              'hashtag' => string 'GameOfThronesFood' (length=17)
  2 => 
    array (size=11)
      'user_id' => int 1903848818
      'screen_name' => string '' (length=0)
      'user_name' => string '† Lαdy S †' (length=15)
      'campaign_id' => string '12345678902' (length=11)
      'tweet_id' => float 6.1123912786292E+17
      'tweet_text' => string 'RT @LordSnow: Pizza of Thrones #GameOfThronesFood http://t.co/qQwGZJ6wMQ' (length=72)
      'tweet_date' => string '2015-06-18 01:28:59' (length=19)
      'favorited_count' => int 0
      'retweet_count' => int 896
      'url' => string '' (length=0)
      'hashtags' => 
        array (size=1)
          0 => 
            array (size=1)
              'hashtag' => string 'GameOfThronesFood' (length=17)

But after I tried with since_id, the result is the same as without since_id.

@giselajesslim
Copy link
Author

Hi, I got this.

https://twittercommunity.com/t/get-search-tweets-sinceid-and-maxid-parameters-issue/10752/4

This is a twitter issue.
I dont know if twitter has fixed it.
Is this why your api doesnt work?
I got the same result as them.

@ivanvermeyen
Copy link
Contributor

It returns all 10 tweets, instead of only the first 3?

Try to put the count parameter last in the $data array. It's a wild guess after reading this:

http://www.bennadel.com/blog/1477-does-order-of-twitter-api-parameters-make-a-difference.htm

@ivanvermeyen
Copy link
Contributor

Or another possibility...

Maybe count has preference over since_id?

https://forums.digitalpoint.com/threads/is-this-a-problem-in-the-twitter-search-api-code.2648205/

Could you try to remove the count and see if it works then?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants