Download the PHP package redwebcreation/twitter-stream-api without Composer
On this page you can find all versions of the php package redwebcreation/twitter-stream-api. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download redwebcreation/twitter-stream-api
More information about redwebcreation/twitter-stream-api
Files in redwebcreation/twitter-stream-api
Package twitter-stream-api
Short Description Consume the Twitter Stream API in real-time.
License MIT
Homepage https://github.com/redwebcreation/phirehose
Informations about the package twitter-stream-api
Twitter Stream API (v2)
Consume the Twitter Stream API v2 in real-time.
This package is the spiritual successor of fennb/phirehose
. It also uses some of salsify/jsonstreamingparser
and maxakawizard/json-collection-parser
.
Getting started
You need an approved developer account. If you don't have one, apply here. Once you are in, create an "Application" in the Developer Portal and generate a new bearer token.
Requires PHP 8.1+
You can install the package via composer:
Then, create a connection:
Usage
Streams
- \Felix\TwitterStream\Streams\VolumeStream
- \Felix\TwitterStream\Streams\FilteredStream
Creating a stream
Configuring a stream
-
withTweetLimit(int)
- Limit the number of tweets a connection should process fields(string[])
- See Fieldsexpansions(...string)
- See Expansions
For advanced use
withBufferSize(int = 85)
- How many bytes should the parser store before trying to parse the JSON, on very high-volume streams, using a larger buffer size is recommended (2500, 10000, depending on the volume). Setting to a big value > 2000 on a low-volume stream would result in 0 tweets being processed until there are enough tweets in the buffer.
Interacting with a stream
stopListening()
- Stops listening to the stream.createdAt(): int
- The UNIX timestamp at which you started listeningtimeElapsedInSeconds(): int
- How much time passed since you started listeningtweetsReceived(): int
- How much the stream sent
For advanced use
response(): Psr\Http\Message\ResponseInterface
- The response sent by Twitter
Listening to a stream
Filtering the stream
This part only applies if you're interested in the filtered stream.
Building a rule
Note, If you change your rules while connected to the stream, Twitter will use the new rules immediately.
Save, read and delete rules
You can not update rules.
Let's create a rule:
You may now retrieve your newly saved rule:
Which returns an array of Felix\TwitterStream\Rule\Rule
:
Note, the
Felix\TwitterStream\Rule\Rule
is merely a Data Object, it does not contain any method.
To delete the rule pass its ID to the delete
method:
Batch Processing
To save many rules at once:
To delete these new rules,
Validating your rules
You can either use the validate()
method:
Or, the save
and saveMany
method both have a dryRun parameter:
Rule Builder
Every operator is available, here's an example:
You may also use and[Operator]
, or[Operator]
, for example orNotFrom('ID')
or andBioLocation('location')
.
Compiling this would produce the following:
Tips
- To directly add a string to the rule, use
raw(string)
- You may call
dump()
ordd()
to quickly debug your rule. and
is the default operator, you may omit it. For example,andIsNotRetweet()
is the same asisNotRetweet()
.
Fields
Fields allow for more customization regarding the payload returned per tweet. Let's see that in an example below:
Which could return:
Here's the list of all the available field types and their respective object model (last updated: Aug. 2022):
- Tweet
- User
- Media
- Poll
- Place
You can also check out Twitter’s documentation for more details.
Expansions
Expansions let you expand ids to their complete object, for example, if you request an extra author_id field, you may expand it using the author_id expansion:
Which could return:
The list of expansions is quite extensive and not all expansions work the same, please check out Twitter's documentation. on the subject.
Testing
Twitter Stream API was created by Félix Dorn under the MIT License.