Tech 387 Twitter Streaming
A PHP Symphony interface to the Twitter Streaming api extending Phirehose.
For more info see: Twitter Dev Page
Why do i need it?
If you are building a twitter app that consumes the realtime data you will encurage problems as connecting to the stream and how to handle connections.
What it does?
-
Connects to the twitter api
-
Consumes followers
-
Handles disconections
-
Handles reconectinos
-
Handles error and warning logging
How to use it?
Download via composer
composer require tech387-twitter/stream
Example with Symfony (sample in example project)
- TweetsCommand.php (called from the comand line by the console file)
- ServiceTweets.php (extends TwitterUser and implements two function for sotring tweets and geting realtime update user ids change)
- console (handles comand line input and calls TweetsCommand.php)
1. Create the console file
Tutorial how to create a console file.
#!/usr/bin/env php
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Symfony\Component\Console\Application;
use Demo\TweetsCommand;
$app = new Application();
$app->add(new TweetsCommand());
$app->run();
2. TweetsCommand.php
class TweetsCommand extends Command
{
protected function configure(){
$this->setName("Twitter:Consume");
}
protected function execute(InputInterface $input, OutputInterface $output){
$configurationEntity = new ConfigurationEntity(
'---',//OAuth Token
'---',//OAuth Secret
'---',//Twitter Consumer Key
'---'//Twitter Consume rSecret
);
$stream = new ServiceTweets($configurationEntity,ServiceTweets::class);
$output->writeln(
$stream->consume()
);
}
}
3. ServiceTweets
showTweets() - display the tweets and info that we get from twitter. We can suply here a functionality that writes tweets to the database.
getIds() - on each stream update this function gets called to detect changes in the array of users we follow.You can extend this to have a db conneciton for a live update of users to follow.
<?php
namespace Demo;
use Tech387\TwitterUser;
class ServiceTweets extends TwitterUser
{
protected function showTweets($tweets)
{
// TODO: Implement showTweets() method.
echo $tweets;
}
protected function getIds(): array
{
// TODO: Implement getIds() method.
return ['906684074793639936','19701628','39538010','934114596310454272'];
}
}
Support
If you have any additional questions feel free to email me arslan.hajdarevic@tech387.com