Install via Composer:
composer require glorand/drip-php
Configure API Key:
Add your Drip API key to .env:
DRIP_API_KEY=your_api_key_here
Or set it programmatically:
$drip = new \Glorand\Drip\Client('your_api_key_here');
First Use Case: Fetch a subscriber by email:
$subscriber = $drip->subscribers()->getByEmail('user@example.com');
src directory outlines the API structure (e.g., Subscribers, Campaigns, Tags).tests folder demonstrates real-world usage patterns.Subscriber Management:
$subscriber = $drip->subscribers()->createOrUpdate([
'email' => 'user@example.com',
'tags' => ['vip', 'newsletter'],
'custom_fields' => ['plan' => 'premium']
]);
$drip->subscribers()->bulkCreate([
['email' => 'user1@example.com', 'tags' => ['tag1']],
['email' => 'user2@example.com', 'tags' => ['tag2']],
]);
Campaigns:
$drip->campaigns()->trigger('welcome_email', ['user@example.com']);
$campaigns = $drip->campaigns()->all();
Tags and Segments:
$drip->subscribers()->addTags('user@example.com', ['new_tag']);
$drip->subscribers()->removeTags('user@example.com', ['old_tag']);
$subscribers = $drip->subscribers()->getByTag('vip');
Webhooks:
$drip->webhooks()->subscribe('https://your-app.com/drip/webhook', [
'subscriber.created',
'subscriber.updated'
]);
Laravel Service Provider: Bind the client to the container for easy dependency injection:
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->singleton(\Glorand\Drip\Client::class, function ($app) {
return new \Glorand\Drip\Client(config('services.drip.key'));
});
}
Configure in config/services.php:
'drip' => [
'key' => env('DRIP_API_KEY'),
],
Queued Jobs: Offload Drip API calls to queues (e.g., Laravel Queues) to avoid timeouts:
// app/Jobs/SyncDripSubscriber.php
public function handle()
{
$drip = app(\Glorand\Drip\Client::class);
$drip->subscribers()->createOrUpdate($this->subscriberData);
}
Event Listeners: Sync Drip subscribers on user registration/login:
// app/Listeners/SyncDripSubscriber.php
public function handle(Registered $event)
{
$drip = app(\Glorand\Drip\Client::class);
$drip->subscribers()->createOrUpdate([
'email' => $event->user->email,
'custom_fields' => ['user_id' => $event->user->id],
]);
}
Deprecated API:
/subscribers/search) may require pagination handling manually.Rate Limiting:
$subscribers = Cache::remember("drip_subscribers_tag_{$tag}", now()->addHours(1), function () use ($drip, $tag) {
return $drip->subscribers()->getByTag($tag);
});
Error Handling:
\Glorand\Drip\Exceptions\DripException for API errors. Catch and log them:
try {
$drip->subscribers()->createOrUpdate($data);
} catch (\Glorand\Drip\Exceptions\DripException $e) {
Log::error("Drip API error: " . $e->getMessage());
// Retry or notify admin
}
Custom Fields:
Webhook Verification:
// In your webhook endpoint
$signature = $_SERVER['HTTP_X_DRIP_SIGNATURE'];
$payload = file_get_contents('php://input');
if (!hash_equals($signature, hash_hmac('sha256', $payload, config('services.drip.webhook_secret')))) {
abort(403, 'Invalid signature');
}
Enable Debug Mode:
$drip = new \Glorand\Drip\Client('your_api_key', [
'debug' => true, // Logs all API requests/responses
]);
Check logs for raw API calls/responses.
Mocking for Tests: Use Laravel’s HTTP client to mock Drip responses:
// tests/Feature/DripTest.php
public function test_subscriber_creation()
{
$mockResponse = ['email' => 'user@example.com', 'id' => 123];
Http::fake([
'api.getdrip.com/*' => Http::response($mockResponse, 200),
]);
$subscriber = $drip->subscribers()->createOrUpdate(['email' => 'user@example.com']);
$this->assertEquals(123, $subscriber['id']);
}
Custom Endpoints:
The Client class extends GuzzleHttp\Client, so you can add custom endpoints:
$drip->get('custom/endpoint', ['query' => ['param' => 'value']]);
Middleware: Add request/response middleware for logging, retries, or transformations:
$drip->getEmitter()->addSubscriber(new class implements SubscriberInterface {
public function handle(Message $message, callable $next) {
// Pre-process request
$response = $next($message);
// Post-process response
return $response;
}
});
Laravel Facades: Create a facade for cleaner syntax:
// app/Facades/Drip.php
class Drip extends Facade {
protected static function getFacadeAccessor() { return 'drip'; }
}
Then use:
Drip::subscribers()->getByEmail('user@example.com');
Batch Processing: For large datasets, implement chunked processing:
$subscribers = $drip->subscribers()->all(['limit' => 100]);
foreach (array_chunk($subscribers, 50) as $chunk) {
$drip->subscribers()->bulkUpdate($chunk, ['tags' => ['new_tag']]);
}
How can I help you explore Laravel packages today?