- How do I install spatie/twitter-oembed in a Laravel project?
- Run `composer require spatie/twitter-oembed` in your project directory. The package integrates seamlessly with Laravel’s service container and requires no additional configuration unless you customize caching or HTTP clients.
- Does this package work with Laravel 10/11? What’s the supported PHP version?
- Yes, the package supports Laravel 10 and 11 and requires PHP 8.1 or higher. Check the [GitHub repository](https://github.com/spatie/twitter-oembed) for the latest compatibility updates, as Spatie regularly aligns with newer Laravel releases.
- Can I use this to embed tweets in a Laravel Blade template?
- Absolutely. Fetch the embed HTML via the package’s `getEmbedHtml()` method, then output it directly in your Blade view. For example: `{{ $tweetEmbed->getEmbedHtml() }}`. Ensure you sanitize the HTML to prevent XSS if embedding user-generated content.
- What happens if Twitter’s oEmbed API is down or rate-limited?
- The package doesn’t include built-in retry logic or fallback mechanisms. You’ll need to implement caching (e.g., Redis) with a short TTL (e.g., 5 minutes) and handle failures gracefully, such as displaying a cached version or a placeholder.
- Does this package support quoted tweets or replies?
- No, the oEmbed API (and thus this package) only returns basic tweet data like text, author, and timestamp. Quoted tweets or replies require Twitter’s full API, which needs authentication. For richer data, consider pairing this with `spatie/twitter-api`.
- How do I cache tweet embeds to reduce API calls?
- Use Laravel’s cache system (e.g., Redis or file cache) to store responses. The package doesn’t enforce caching, but you can wrap its methods in a cached closure or use Laravel’s `Cache::remember()` to store embed HTML for a set duration.
- Is this package suitable for newsletters or email templates?
- Yes, it’s ideal for static email content like newsletters. Fetch the embed HTML server-side and include it directly in your email template. However, test rendering in your email client to ensure compatibility, as some may strip or modify embedded HTML.
- Can I use this in a headless CMS like Strapi or directus?
- Yes, the package is framework-agnostic and works in any PHP environment. For a headless CMS, fetch embeds during content creation or updates, then store the HTML in your database to avoid repeated API calls.
- What’s the difference between this and Twitter’s official embed widget?
- This package generates static HTML via Twitter’s oEmbed API, avoiding JavaScript dependencies. The official widget is interactive (likes, replies) but requires Twitter’s script and may break if Twitter changes its embed code. Choose this for static content, the widget for dynamic interactions.
- How do I handle XSS risks from embedded tweet HTML?
- Always sanitize the embed HTML before rendering. Use Laravel’s `Str::markdown()` or a library like `htmlpurifier` to strip unsafe tags. Avoid embedding tweets from untrusted sources, as the oEmbed API doesn’t validate content.