- How do I install dg/twitter-php in a Laravel project?
- Run `composer require dg/twitter-php` to install. Register the service provider in `config/app.php` under `providers`, then bind the client in a service provider or use Laravel’s facade pattern for cleaner syntax. Ensure PHP 8.2+ and Laravel 10+ compatibility.
- Does this package support OAuth 2.0 for X API v2 endpoints like DMs or followers?
- No, this package primarily uses OAuth 1.0a. For OAuth 2.0 endpoints (e.g., DMs, followers), pair it with `socialiteproviders/twitter` or implement a hybrid auth flow. Check Twitter’s API docs to confirm which endpoints require OAuth 2.0.
- What Laravel version does dg/twitter-php support?
- The package requires PHP 8.2+ and is optimized for Laravel 10+. If using an older Laravel version, ensure compatibility with Guzzle HTTP client (v7+) and PHP 8.2 features like named arguments.
- How do I handle rate limits in production with X API v2?
- Use Laravel Cache or Redis to track rate limits and implement exponential backoff. The package doesn’t include built-in rate-limiting, so wrap Guzzle requests with middleware like `guzzlehttp/guzzle`'s `RetryMiddleware` or create a custom Laravel middleware.
- Can I use this package for real-time tweet notifications or streaming?
- No, this package doesn’t support real-time streaming. For live updates, use Twitter’s Filtered Stream API with WebSockets (e.g., Laravel Echo + Pusher) or a separate service like `abraham/twitteroauth` for v1.1 streaming.
- How do I migrate from dg/twitter-php v1.1 to this v2 package?
- Update your Composer dependency, refactor method calls (e.g., `sendTweet()` replaces `statuses/update`), and adjust OAuth flows. Test all endpoints in Twitter’s v2 sandbox first, as v2 introduces breaking changes like OAuth 2.0 for some features.
- Does this package include helpers for storing tweets or user data in Laravel?
- No, the package returns raw API responses. Manually persist data to Eloquent models or use packages like Spatie’s Media Library for tweet attachments. Example: `Tweet::create($response->data)` for storing tweets.
- What are the alternatives to dg/twitter-php for Laravel?
- Consider `abraham/twitteroauth` (v1.1, deprecated), `thephpleague/oauth2-twitter` (OAuth 2.0), or `twitter-api-php` for v2. For Laravel-specific needs, combine this package with `socialiteproviders/twitter` for OAuth 2.0 or `laravel-socialite` for unified auth.
- How do I test this package in Laravel without hitting real API limits?
- Use Mockery to mock Guzzle HTTP client calls in unit tests. For integration tests, use Twitter’s v2 sandbox environment (apply for access via Twitter Developer Portal) to avoid rate limits and test endpoints safely.
- Can I use this package in a microservice architecture with Laravel?
- Yes, containerize the package as a standalone service using Docker. Expose API endpoints via Laravel’s HTTP routes or a microservice framework like Lumen. Decouple social logic by publishing events (e.g., `TweetSent`) for other services to consume.