desarrolla2/twitter-client
Independent PHP Twitter client for fetching a user’s tweets via RSS. Supports simple usage or optional caching through desarrolla2/cache and RSSClient. Install via Composer, set a screen name, and fetch recent tweets quickly.
Installation Add the package via Composer:
composer require desarrolla2/twitter-client
Ensure desarrolla2/rss-client (dependency) is also installed.
First Use Case Fetch tweets from a user without caching (for testing or low-frequency use):
use Desarrolla2\TwitterClient\TwitterClient;
$client = new TwitterClient();
$client->setScreenName('laravelphp'); // Replace with target username
$tweets = $client->fetch(5); // Fetch 5 most recent tweets
Where to Look First
TwitterClient class: Core functionality for fetching tweets.setScreenName(): Configure the target Twitter handle.fetch($limit): Retrieve tweets (returns raw data; see Implementation Patterns for parsing).RSSCacheClient: For caching (requires desarrolla2/cache package).Fetching Tweets
$client = new TwitterClient();
$client->setScreenName('laravelnews');
$tweets = $client->fetch(10); // Array of tweet objects (see structure below).
fetch() method returns an array of tweet objects with properties like:
$tweet->id, $tweet->text, $tweet->created_at, $tweet->user
Example loop:
foreach ($tweets as $tweet) {
echo $tweet->user->screen_name . ': ' . $tweet->text . "\n";
}
Caching Integration
use Desarrolla2\RSSClient\RSSCacheClient;
use Desarrolla2\Cache\Adapters\RedisAdapter;
$cache = new RSSCacheClient(new RedisAdapter());
$client = new TwitterClient($cache);
$client->setScreenName('laravel');
$tweets = $client->fetch(5); // Uses cache if available.
RSSCacheClient (default: 300 seconds).
$cache->setTTL(1800); // Cache for 30 minutes.
Error Handling
try {
$tweets = $client->fetch(10);
} catch (\Exception $e) {
Log::error("Twitter fetch failed: " . $e->getMessage());
// Fallback: Return cached data or empty array.
}
Laravel Integration
AppServiceProvider:
public function register() {
$this->app->singleton(TwitterClient::class, function ($app) {
$cache = new RSSCacheClient(new \Desarrolla2\Cache\Adapters\FileAdapter());
return new TwitterClient($cache);
});
}
// app/Facades/Twitter.php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class Twitter extends Facade { protected static function getFacadeAccessor() { return 'twitter'; } }
Register in config/app.php:
'twitter' => \Desarrolla2\TwitterClient\TwitterClient::class,
Batch Processing
$client->setScreenName('laravel');
$allTweets = [];
$limit = 100;
$offset = 0;
do {
$tweets = $client->fetch($limit, $offset);
$allTweets = array_merge($allTweets, $tweets);
$offset += $limit;
} while (!empty($tweets));
No API Key Authentication
https://twitter.com/{screen_name}.rss).abraham/twitteroauth.Caching Dependencies
RSSCacheClient) requires desarrolla2/cache.composer require desarrolla2/twitter-client desarrolla2/cache
Data Format Quirks
fetch() are not standardized JSON/arrays but custom objects.print_r(get_object_vars($tweets[0]));
Performance
fetch() makes an HTTP request to Twitter’s RSS feed.
desarrolla2/cache performance. Test with your adapter (Redis > File).Deprecation Risk
Enable Verbose Logging
$client->setDebug(true);
https://twitter.com/{screen_name}.rss).Validate Screen Names
setScreenName() uses a valid Twitter handle (no spaces, correct case).if (!preg_match('/^[a-z0-9_]+$/i', $screenName)) {
throw new \InvalidArgumentException("Invalid Twitter screen name.");
}
Cache Invalidation
$cache->clear();
Customize Tweet Parsing
TwitterClient class to transform raw tweet objects:
class CustomTwitterClient extends TwitterClient {
public function fetch($limit, $offset = 0) {
$tweets = parent::fetch($limit, $offset);
return array_map(function ($tweet) {
return [
'id' => $tweet->id,
'text' => $this->cleanText($tweet->text),
'url' => "https://twitter.com/{$tweet->user->screen_name}/status/{$tweet->id}"
];
}, $tweets);
}
private function cleanText($text) {
// Remove URLs, mentions, etc.
return preg_replace('/https?:\/\/[^\s]+/', '', $text);
}
}
Add Rate Limiting
class RateLimitedTwitterClient {
private $client;
private $lastFetch = 0;
private $interval = 60; // 1 minute
public function __construct(TwitterClient $client) {
$this->client = $client;
}
public function fetch($limit) {
if (time() - $this->lastFetch < $this->interval) {
throw new \RuntimeException("Rate limit exceeded. Try again later.");
}
$this->lastFetch = time();
return $this->client->fetch($limit);
}
}
Support Multiple Accounts
class TwitterClientFactory {
public static function create($screenName, $cache = null) {
$client = new TwitterClient($cache);
$client->setScreenName($screenName);
return $client;
}
}
Usage:
$client1 = TwitterClientFactory::create('laravel');
$client2 = TwitterClientFactory::create('php');
How can I help you explore Laravel packages today?