All Projects → redwebcreation → twitter-stream-api

redwebcreation / twitter-stream-api

Licence: MIT License
Consume the Twitter Stream API in real-time.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to twitter-stream-api

Archive-Tweets
Archive and Delete Liked and Posted Tweets
Stars: ✭ 28 (+47.37%)
Mutual labels:  twitter, twitter-api
Tweetledee
A PHP library that provides an incredibly easy way to access Twitter data as JSON or RSS feed by URL or standard CLI syntax.
Stars: ✭ 208 (+994.74%)
Mutual labels:  twitter, twitter-api
Twitter Api Php
The simplest PHP Wrapper for Twitter API v1.1 calls
Stars: ✭ 1,808 (+9415.79%)
Mutual labels:  twitter, twitter-api
Twurl
OAuth-enabled curl for the Twitter API
Stars: ✭ 1,648 (+8573.68%)
Mutual labels:  twitter, twitter-api
shut-up-bird
🐦 Put your tweets/likes in an EPUB and delete them like a boss
Stars: ✭ 22 (+15.79%)
Mutual labels:  twitter, twitter-api
Twitwork
Monitor twitter stream
Stars: ✭ 133 (+600%)
Mutual labels:  twitter, twitter-api
Twitter To Sqlite
Save data from Twitter to a SQLite database
Stars: ✭ 203 (+968.42%)
Mutual labels:  twitter, twitter-api
Tia
Your Advanced Twitter stalking tool
Stars: ✭ 98 (+415.79%)
Mutual labels:  twitter, twitter-api
Egg Mode
a twitter api crate for rust
Stars: ✭ 249 (+1210.53%)
Mutual labels:  twitter, twitter-api
Twitterdelete
💀 Delete your old, unpopular tweets.
Stars: ✭ 231 (+1115.79%)
Mutual labels:  twitter, twitter-api
Twitter Python Ads Sdk
A Twitter supported and maintained Ads API SDK for Python.
Stars: ✭ 114 (+500%)
Mutual labels:  twitter, twitter-api
labs-sample-code
Sample code for Twitter Developer Labs
Stars: ✭ 25 (+31.58%)
Mutual labels:  twitter, twitter-api
Awesome Twitter Tools
A curated list of awesome twitter tools
Stars: ✭ 113 (+494.74%)
Mutual labels:  twitter, twitter-api
Csscreatures
Make a creature by tweeting to @csscreatures
Stars: ✭ 144 (+657.89%)
Mutual labels:  twitter, twitter-api
Twitterbot
Several PHP scripts for making Twitter bots that retweet certain terms, or post from a data source (rss, database, markov body, picture folder).
Stars: ✭ 106 (+457.89%)
Mutual labels:  twitter, twitter-api
Twitter Bot
Node js twitter bot to send auto welcome message for your new followers
Stars: ✭ 184 (+868.42%)
Mutual labels:  twitter, twitter-api
21 Recipes
📕 An R/rtweet edition of Matthew A. Russell's Python Twitter Recipes Book
Stars: ✭ 69 (+263.16%)
Mutual labels:  twitter, twitter-api
Tweetview
This project is an example Android Twitter feed reader app from the Codehenge Android development tutorials.
Stars: ✭ 75 (+294.74%)
Mutual labels:  twitter, twitter-api
Harpy
A Twitter app built with Flutter
Stars: ✭ 211 (+1010.53%)
Mutual labels:  twitter, twitter-api
twittered
Twitter API client for Java developers
Stars: ✭ 170 (+794.74%)
Mutual labels:  twitter-api, twitter-api-stream

Twitter Stream API (v2)

Tests Formats Version Total Downloads

Consume the Twitter Stream API v2 in real-time.

This package is the spiritual successor of fennb/phirehose.

Getting started

Requires PHP 8.0+

You can install the package via composer:

composer require redwebcreation/twitter-stream-api

Usage

use RWC\TwitterStream\RuleBuilder;
use RWC\TwitterStream\Fieldset;
use RWC\TwitterStream\Sets;
use RWC\TwitterStream\TwitterStream;

$twitterStream = new TwitterStream(
    $bearerToken = '',
    $apiKey  = '',
    $apiSecretKey = '',
);

RuleBuilder::create('cats')
  ->not->retweets()
  ->hasLinks()
  ->save();

$sets = new Sets(
    new Fieldset('user.fields', 'created_at')
);

foreach ($twitterStream->filteredTweets($sets) as $tweet) {
    dump($tweet['data']['text']);
    
    if ($enoughTweets) {
        $twitterStream->stopListening();
    }
}

Concepts

Rules

Rules are made up of one, or many operators that are combined using boolean logic and parentheses to help define which Tweets will deliver to your stream. Rules are saved in the Twitter API and are persistent.

You need to create a TwitterStream before using anything related to rules. Alternatively, you can use Rule::useBearerToken() for full control over which token is used.

Listing all the rules

use RWC\TwitterStream\Rule;

Rule::all();

Adding a rule

use RWC\TwitterStream\Rule;

$rule = new Rule('cat has:image', 'cats with images');
$rule->save();

If no tag is provided, the fallback is the rule content itself.

For your convience, there is a "query builder" for rules available, learn more here.

Deleting a rule

Note: you can not delete a rule before adding it.

use RWC\TwitterStream\Rule;

$rule  = Rule::all()[0];
$rule->delete();

To reduce the number of requests made to Twitter's API, you may want to use bulk rules creation.

use RWC\TwitterStream\Rule;

// One request
Rule::addBulk(
    new Rule('one rule'),
    new Rule('another one')
);

// 2 requests instead of 1 + x rules to delete
Rule::deleteBulk(...Rule::all());

Sets

If you would like to receive additional fields beyond id and text, you will have to specify those fields in your request with sets.

Sets are also referred as expansions / additional fields in the Twitter documentation.

Sets are a group of Fieldset, Twitter exposes three as of now :

  • tweet.fields
  • user.fields
  • expansions
use RWC\TwitterStream\Fieldset;
use RWC\TwitterStream\Sets;

$sets = new Sets(
    new Fieldset('tweet.fields', 'created_at', '...'),
    new Fieldset('expansions', 'author_id', '...')
);

Then, pass it to filteredTweets()

$twitterStream->filteredTweets($sets);

Rule Builder

It's a powerful tool to build complex rules using an expressive syntax.

use RWC\TwitterStream\RuleBuilder;

$builder = RuleBuilder::create('#php')
    ->group(function (RuleBuilder $builder) {
        $builder->raw('tip')->or()->raw('🔥');
    })
    ->retweets()
    ->hasImages()
    ->not->hasLinks();

// Produces #php (tip OR 🔥) is:retweet has:images -has:links

You can negate an operator using the magic property not.

use RWC\TwitterStream\RuleBuilder;
RuleBuilder::create('#php')
  ->not->retweets()
  ->hasLinks();

// Produces: #php -is:retweet has:links

You can also group operators together :

use RWC\TwitterStream\RuleBuilder;
RuleBuilder::create('#laravel')
    ->group(function (RuleBuilder $builder) {
        $builder->raw('tip')->or()->raw('tips')->or()->raw('🔥');
    });

// Produces: #laravel (tip OR tips OR 🔥)

You can also directly save the rule :

use RWC\TwitterStream\RuleBuilder;

RuleBuilder::create('cats')
  ->hasImages()
  ->not->retweets()
  ->save('cats with images, not a retweet');

This sends a request to Twitter.

Available methods

  • from : Matches any Tweet from a specific user.
  • to : Matches any Tweet that is in reply to a particular user.
  • sample : Returns a random percent sample of Tweets that match a rule rather than the entire set of Tweets.
  • nullcast : Removes Tweets created for promotion only on ads.twitter.com. (Must always be negated)
  • replies : Deliver only explicit replies that match a rule.
  • retweets : Matches on Retweets that match the rest of the specified rule.
  • quote : Returns all Quote Tweets, also known as Tweets with comments.
  • verified : Deliver only Tweets whose authors are verified by Twitter.
  • retweetsOf : Matches Tweets that are Retweets of the specified user.
  • context : Matches Tweets with a specific domain id and/or domain id, entity id pair.
  • hasHashtags : Matches Tweets that contain at least one hashtag.
  • hasCashtags : Matches Tweets that contain a cashtag symbol.
  • hasLinks : This operator matches Tweets which contain links and media in the Tweet body.
  • hasMentions : Matches Tweets that mention another Twitter user.
  • hasMedia : Matches Tweets that contain a media object, such as a photo, GIF, or video, as determined by Twitter.
  • hasImages : Matches Tweets that contain a recognized URL to an image.
  • hasVideos : Matches Tweets that contain native Twitter videos, uploaded directly to Twitter.
  • hasGeographicDataAttached : Matches Tweets that have Tweet-specific geolocation data provided by the Twitter user.
  • locale : Matches Tweets that have been classified by Twitter as being of a particular language
  • url : Matches on any validly-formatted URL of a Tweet.
  • entity : Matches Tweets with a specific entity string value.
  • conversation : Matches Tweets with a specific entity string value.
  • bio : Matches a keyword or phrase within the Tweet publisher's bio.
  • bioName :Matches a keyword within the Tweet publisher's user bio name.
  • bioLocation : Matches Tweets that are published by users whose location contains the specified keyword or phrase.
  • place : Matches Tweets tagged with the specified location or Twitter place ID.
  • placeCountry : Matches Tweets where the country code associated with a tagged place/location matches the given ISO alpha-2 character code.
  • pointRadius : Matches against the place.geo.coordinates object of the Tweet when present, and in Twitter, against a place geo polygon, where the Place polygon is fully contained within the defined region.
  • boundingBox : Matches against the place.geo.coordinates object of the Tweet when present, and in Twitter, against a place geo polygon, where the place polygon is fully contained within the defined region.

Testing

composer test

Twitter Stream API was created by Félix Dorn under the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].