picr/php-autopilothq
PHP library for interacting with the AutopilotHQ API. Provides an AutopilotManager to manage contacts (get/save/delete/subscribe/unsubscribe, update email), lists (create/find/add/remove/check members), triggers/journeys, and REST hooks.
Installation:
composer require picr/php-autopilothq
Add API key to your .env:
AUTPILOTHQ_API_KEY=your_api_key_here
First Use Case: Initialize the manager and fetch a contact:
use Picr\AutopilotHQ\AutopilotManager;
$manager = new AutopilotManager(config('services.autopilothq.key'));
$contact = $manager->getContact('user@example.com');
Where to Look First:
AutopilotManager (all API interactions).AutopilotContact (data manipulation).Contact Management Workflow:
// Create/update a contact
$contact = new AutopilotContact();
$contact->fill([
'firstName' => 'John',
'email' => 'john@example.com',
'customField' => 'value'
]);
$manager->saveContact($contact);
// Bulk operations
$contacts = collect([...])->map(function ($data) {
$contact = new AutopilotContact();
$contact->fill($data);
return $contact;
});
$manager->saveContacts($contacts->all());
List and Journey Integration:
// Add contact to a list/journey
$manager->addContactToList('Marketing Newsletter', 'john@example.com');
$manager->addContactToJourney('Welcome Series', 'john@example.com');
// Check list membership
if ($manager->checkContactInList('Newsletter', 'john@example.com')) {
// Handle logic
}
Webhook Setup:
// Register a webhook for contact updates
$manager->addRestHook('contact_updated', 'https://your-app.com/webhooks/autopilothq');
Laravel Integration:
// Service Provider Binding
$this->app->singleton(AutopilotManager::class, function ($app) {
return new AutopilotManager(config('services.autopilothq.key'));
});
// Facade (optional)
facade(AutopilotManager::class, 'Autopilot');
Event-Driven Workflows:
// Laravel Event Listener for webhooks
public function handle(IncomingWebhook $event) {
if ($event->data['event'] === 'contact_updated') {
$contact = $manager->getContact($event->data['email']);
// Trigger business logic
}
}
$contacts = Cache::remember("autopilothq_contacts_{$list}", now()->addHours(1), function () use ($manager, $list) {
return $manager->getAllContactsInList($list);
});
try {
$manager->saveContact($contact);
} catch (\Exception $e) {
Log::error("AutopilotHQ Error: " . $e->getMessage());
// Retry or notify admin
}
foreach ($contacts as $contact) {
SyncContactJob::dispatch($contact)->onQueue('autopilothq');
}
API Key Management:
$manager = new AutopilotManager(config('services.autopilothq.key'));
Deprecated API:
dd($manager->getContact(...)); to inspect responses.Custom Fields:
setFieldValue() instead of magic methods for custom fields:
$contact->setFieldValue('custom_field_name', 'value');
Rate Limiting:
retry helper:
$response = retry(3, function () use ($manager) {
return $manager->getAllContactsInList('list_name');
}, 100);
Webhook Delays:
$manager = new AutopilotManager($apiKey, [
'debug' => true,
]);
$manager->setLogger(function ($message) {
Log::debug($message);
});
AutopilotContact data with toArray().curl or file_get_contents. Replace with Laravel’s Http client for consistency:
// Override the HTTP client in AutopilotManager (if extensible)
$manager->setHttpClient(app('http'));
Http client config:
'timeout' => 60,
Custom HTTP Client:
AutopilotManager to use Guzzle or Laravel’s Http client:
class LaravelAutopilotManager extends AutopilotManager {
public function __construct($apiKey) {
parent::__construct($apiKey);
$this->setHttpClient(app('http'));
}
}
Eloquent Integration:
Contact model that syncs with AutopilotHQ:
class Contact extends Model {
public function syncToAutopilot() {
$manager = app(AutopilotManager::class);
$autopilotContact = new AutopilotContact($this->toArray());
$manager->saveContact($autopilotContact);
}
}
Event Dispatching:
$manager->afterSaveContact(function ($contact) {
event(new ContactSynced($contact));
});
Testing:
AutopilotManager in tests:
$mockManager = Mockery::mock(AutopilotManager::class);
$mockManager->shouldReceive('getContact')->andReturn($fakeContact);
$this->app->instance(AutopilotManager::class, $mockManager);
How can I help you explore Laravel packages today?