PHP code example of redwebcreation / twitter-stream-api

1. Go to this page and download the library: Download redwebcreation/twitter-stream-api library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

redwebcreation / twitter-stream-api example snippets


$connection = new \Felix\TwitterStream\TwitterConnection(
    bearerToken: '...' # the one you got from the Developer Portal
);

$stream = new \Felix\TwitterStream\Streams\VolumeStream();
// or
$stream = new \Felix\TwitterStream\Streams\FilteredStream();

    $stream->withTweetLimit(100_000);
    

$stream->listen($connection, function (object $tweet) {
    echo $tweet->data->text . PHP_EOL;
});

use Felix\TwitterStream\Rule\RuleManager;

$rule = new RuleManager($connection);

$rule->save(
	# tweets must contain the word cat and have at least one image
	"cat has:images",
	"images of cats"
);

$rule->all();

[
	0 => Felix\TwitterStream\Rule\Rule{
		+value: "cat has:images",
		+tag: "images of cats",
		+id: "4567654567654567654"
	}
]

$rule->delete('4567654567654567654');

use Felix\TwitterStream\Rule\Rule;

$rule->saveMany([  
   new Rule("cats has:images", "cat pictures"),  
   new Rule("dogs has:images", "dog pictures"),  
   new Rule("horses has:images", "horse picture"),  
]);

$rule->delete([
	'[RULE ID]',
	'[RULE ID]',
	'[RULE ID]',
]);

# returns a list of errors
$errors = $rule->validate('cats ha:images');

$rule->save('...', '...', dryRun: true);

$rule->saveMany([...], dryRun: true);

$rule->new('listening to music')
    ->raw('#nowplaying')
    ->isNotRetweet()
    ->lang('en')
    ->save();

$stream
    ->fields([
        'tweet' => 'author_id'
         // or,
         // 'tweet' => ['author_id', '...']
    ])
    ->listen(...);

$stream
    ->fields(['tweet' => 'author_id'])
    ->expansions('author_id')
    ->listen(...);