Twitter API provides a platform to stream real-time public tweets. This feature is helpful to know about current trends, and what people are talking about. In terms of business, using real-time tweets, you can analyze the public sentiment about your products or services.

In this tutorial, we will study streaming real-time tweets using PHP. I’ll build a script that when run on the browser will print tweets along with some basic details like Author Name, Screen Name, and Tweet link.

To get started with Twitter streaming, you first need to create your developer account and get your Consumer Keys. Twitter API requires you to send an access token in the Authorization header in each request. With your API key and secret, we generate the app access token. This access token will be sent during interaction with the Twitter APIs.

Get Twitter API Credentials

  • Sign Up for a developer account.
  • Create a Twitter App within a developer portal.
  • Add your Twitter App to a Project.
  • Under Consumer Keys, you’ll get your API key and secret.

Building Rules for Filtered Stream

By default, if you fetch real-time tweets, it comes with a lot of noise which you don’t want. You may be looking for tweets that are beneficial to you and your business. You can do so by building rules to filtered a stream.

For example, let’s say you want to get tweets for specific hashtags (#WordPress) or mentions(@WordPress). Using the below rule, you can get the tweets matching the given rule.

#WordPress @WordPress lang:en -is:retweet

Here, I passed -is:reweet which fetched only original tweets(excluding retweets). Read more about building a rule on official documentation.

To add this rule to your stream you need to send a POST request to the Twitter API with an added payload. Once the rule is added, when you start streaming, it filters out the real-time tweets matching your rule.

One point that should be considered while building a rule is you should first delete the previous rules. Otherwise, rules are kept added which will limit your end results. It may happen your 2 rules are not related to each other so it doesn’t make sense to use them together for filtered streams.

Having said that, we’ll write a code that performs streaming following the steps below.

  • Generate App Access Token: This token is used for read-only purposes. We are also just reading real-time tweets.
  • Get rules: Fetch the rules which are already built.
  • Delete Rules: Delete these previous rules.
  • Set Rule: Add a new set of rules. It can be single or multiple.
  • Filtered Stream: Here, we’ll print real-time tweets.

Stream Tweets in Real-Time using PHP

Before writing a code, you have to install the felixdorn/twitter-stream-api package. Head over to the terminal and run the below command.

composer require redwebcreation/twitter-stream-api

Upon installation, include the package’s environment as follows.

<?php
require_once "vendor/autoload.php";

use FelixTwitterStreamRuleManager;
use FelixTwitterStreamRule;

Generate App Access Token

The App access token can be generated by sending a POST request to the oauth2/token endpoint of Twitter API. Let’s define a method for it.

define("API_KEY", "YOUR_API_KEY");
define("API_KEY_SECRET", "YOUR_API_KEY_SECRET");
 
function get_app_only_access_token() {
	$client = new GuzzleHttpClient([
    	'base_uri' => 'https://api.twitter.com',
	]);
 
	$response = $client->request('POST', '/oauth2/token', [
    	"headers" => [
        	"Authorization" => "Basic ". base64_encode(API_KEY.":".API_KEY_SECRET),
        	"Content-Type" => "application/x-www-form-urlencoded;charset=UTF-8"
    	],
    	"form_params" => [
        	"grant_type" => "client_credentials"
    	],
	]);
 
	$res = json_decode($response->getBody());
	return $res->access_token;
}

Get Rules

We have installed the package for Twitter streaming API. This package provides functions to get all rules defined for a stream. When you use it for the first time, you will get an empty result because you haven’t set any rules yet.

$connection = new FelixTwitterStreamTwitterConnection(get_app_only_access_token());

function get_rules($connection) {
	$rule = new RuleManager($connection);
	return $rule->all();
}
$current_rules = get_rules($connection);

The $connection variable holds an object of your connected Twitter App. This object must be passed to the package’s methods in order to get the expected output from Twitter API.

Delete Rules

As I said before, you should delete the previous rules before setting a new one. It gives you a better result for your latest rules.

function delete_rules($connection, $rules) {
	$rule = new RuleManager($connection);
	foreach ( $rules as $r ) {
    	$rule->delete($r->id);
	}
}
delete_rules($connection, $current_rules);

Set Rules

In this tutorial, I am filtering real-time tweets for a WordPress keyword. My plain rule for it is WordPress lang:en -is:retweet.

When setting rules for filtered streams, you should also set a tag for your rule. For instance, to the above rule, I’ll give a tag as ‘WordPress Stream’. The tag is user-defined and it’s up to you to decide on the text for it.

function set_rules($connection, $rules) {
	$build_rules = [];
	foreach ( $rules as $r ) {
    	$build_rules[] = new Rule($r['rule'], $r['tag']);
	}

	$rule = new RuleManager($connection);
	$rule->saveMany($build_rules);	 
}
$arr_rules = [
	[
    	"rule" => "WordPress lang:en -is:retweet",
    	"tag" => "WordPress Stream"
	],
];
set_rules($connection, $arr_rules);

You can set as many rules by passing multiple arrays providing values for rule and tag.

Filtered Stream

We’re done with all the required steps and now can start streaming real-time tweets. While filtering the stream, I’ll print basic details like Tweet, Author Name, Screen Name, and Tweet URL. Here is the code for it.

function filtered_stream() {
	try {
    	set_time_limit(0);
    	$connection = new FelixTwitterStreamTwitterConnection(get_app_only_access_token());
    	$stream = new FelixTwitterStreamStreamsFilteredStream();
    	$stream
        	->fields([
            	'tweet' => 'author_id'
        	])
        	->expansions('author_id')
        	->listen($connection, function (object $tweet) {
            	echo "<p>Tweet: ". $tweet->data->text . "</p>";
            	echo "<p>Screen Name: ". $tweet->includes->users[0]->username . "</p>";
            	echo "<p>Author Name: ". $tweet->includes->users[0]->name . "</p>";
            	echo "<p>Tweet URL: ". 'https://twitter.com/'.$tweet->includes->users[0]->username.'/status/'.$tweet->data->id . "</p>";
            	echo "<hr />";
        	});
	} catch(Exception $e) {
    	echo $e->getMessage();
	}
}
filtered_stream();

Our final code is as follows.

<?php
require_once "vendor/autoload.php";

use FelixTwitterStreamRuleManager;
use FelixTwitterStreamRule;

define("API_KEY", "YOUR_API_KEY");
define("API_KEY_SECRET", "YOUR_API_KEY_SECRET");
 
function get_app_only_access_token() {
	$client = new GuzzleHttpClient([
    	'base_uri' => 'https://api.twitter.com',
	]);
 
	$response = $client->request('POST', '/oauth2/token', [
    	"headers" => [
        	"Authorization" => "Basic ". base64_encode(API_KEY.":".API_KEY_SECRET),
        	"Content-Type" => "application/x-www-form-urlencoded;charset=UTF-8"
    	],
    	"form_params" => [
        	"grant_type" => "client_credentials"
    	],
	]);
 
	$res = json_decode($response->getBody());
	return $res->access_token;
}

$connection = new FelixTwitterStreamTwitterConnection(get_app_only_access_token());

function get_rules($connection) {
	$rule = new RuleManager($connection);
	return $rule->all();
}

function delete_rules($connection, $rules) {
	$rule = new RuleManager($connection);
	foreach ( $rules as $r ) {
    	$rule->delete($r->id);
	}
}

function set_rules($connection, $rules) {
	$build_rules = [];
	foreach ( $rules as $r ) {
    	$build_rules[] = new Rule($r['rule'], $r['tag']);
	}

	$rule = new RuleManager($connection);
	$rule->saveMany($build_rules);	 
}

function filtered_stream() {
	try {
    	set_time_limit(0);
    	$connection = new FelixTwitterStreamTwitterConnection(get_app_only_access_token());
    	$stream = new FelixTwitterStreamStreamsFilteredStream();
    	$stream
        	->fields([
            	'tweet' => 'author_id'
        	])
        	->expansions('author_id')
        	->listen($connection, function (object $tweet) {
            	echo "<p>Tweet: ". $tweet->data->text . "</p>";
            	echo "<p>Screen Name: ". $tweet->includes->users[0]->username . "</p>";
            	echo "<p>Author Name: ". $tweet->includes->users[0]->name . "</p>";
            	echo "<p>Tweet URL: ". 'https://twitter.com/'.$tweet->includes->users[0]->username.'/status/'.$tweet->data->id . "</p>";
            	echo "<hr />";
        	});
	} catch(Exception $e) {
    	echo $e->getMessage();
	}
}

$current_rules = get_rules($connection);

delete_rules($connection, $current_rules);

$arr_rules = [
	[
    	"rule" => "WordPress lang:en -is:retweet",
    	"tag" => "WordPress Stream"
	],
	// [
	// 	"rule" => "@laravelphp #Laravel lang:en -is:retweet",
	// 	"tag" => "Laravel Stream"
	// ]
];
set_rules($connection, $arr_rules);

filtered_stream();

I hope you understand how to stream real-time Tweets in PHP. You may also like to read our other articles related to the Twitter API.

The post Stream Tweets in Real-Time using PHP appeared first on Artisans Web.