spatie/twitter-oembed
Fetch Twitter (X) tweet embed HTML via the public oEmbed API—no developer account needed. Lightweight utility for turning tweet URLs into embeddable HTML for blogs, markdown, editors, or emails, without relying on Twitter’s heavy JavaScript widget.
Tweet entity in Laravel).spatie/twitter-api) or the official Twitter API.Str::markdown or a library like htmlpurifier).config/app.php and publish its config (if needed) via php artisan vendor:publish.Http facade or Guzzle for custom retries/caching.spatie/laravel-markdown).composer.json constraints).guzzlehttp/guzzle version is compatible.composer require spatie/twitter-oembed..env or config/services.php).use Spatie\TwitterOEmbed\TwitterOEmbed;
class TweetEmbedder {
public function embed(string $tweetUrl): string {
$oEmbed = new TwitterOEmbed();
$tweet = $oEmbed->getEmbed($tweetUrl);
return cleanHtml($tweet->html); // Custom sanitization
}
}
TwitterOEmbed class to test failure scenarios.dd() or Log::debug() for troubleshooting.| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Twitter API downtime | Embeds break site-wide. | Serve cached HTML; notify users. |
| Rate limiting | Embeds fail intermittently. | Implement retries with exponential backoff. |
| Invalid tweet URLs | Broken embeds in content. | Validate URLs before fetching. |
| HTML injection (XSS) | Security vulnerability. | Sanitize output with htmlpurifier. |
| API response format changes | Package breaks. | Fork and extend the package. |
https://twitter.com/user/status/123).spatie/laravel-monitor).How can I help you explore Laravel packages today?