dyninc/dyn-php
PHP SDK for Dyn APIs. Manage DNS with Traffic Management (sessions, zones, records, redirects, dynamic DNS) and send email via Message Management. Supports PHP 7.4+, works with Composer, and includes examples and PHPUnit tests.
Installation Add the package via Composer:
composer require dyninc/dyn-php
Verify the package loads in config/app.php under providers.
Configuration Publish the config file:
php artisan vendor:publish --provider="Dyninc\DynPhp\DynPhpServiceProvider" --tag="config"
Update .env with your DynAPI credentials:
DYN_API_USERNAME=your_username
DYN_API_PASSWORD=your_password
DYN_DEBUG=false # Enable for verbose logging
First Use Case: DNS Record Management
Fetch a DNS record (e.g., example.com):
use Dyninc\DynPhp\Client;
$client = new Client();
$record = $client->getDnsRecord('example.com', 'A');
dd($record);
New in 0.11.0: Delete DNS records or entire nodes (including child records):
// Delete a single record
$client->deleteDnsRecord('example.com', 'A', '192.0.2.1');
// Delete a node and ALL its child records (new capability)
$client->deleteDnsRecord('example.com', 'A', null, true);
DNS Management
$client->createDnsRecord('example.com', 'A', '192.0.2.1', 3600);
// Delete a single record
$client->deleteDnsRecord('example.com', 'A', '192.0.2.1');
// Delete a node and ALL child records (new in 0.11.0)
$client->deleteDnsRecord('example.com', 'A', null, true);
updateDnsRecords() with an array of record data for batch operations.Traffic Director (Load Balancing)
$client->createPool('web_pool', ['192.0.2.1:80', '192.0.2.2:80']);
$client->updateTrafficRule('web_pool', 'example.com', 'A', 'roundrobin');
Monitoring & Alerts
$alerts = $client->getAlerts();
foreach ($alerts as $alert) {
Log::warning($alert->message);
}
Laravel Artisan Commands
Create a custom command for DNS management (e.g., DeleteDnsRecords):
php artisan make:command DeleteDnsRecords
Use the SDK in the handle() method:
$client->deleteDnsRecord($domain, $type, $address, $deleteChildren);
Service Providers Bind the client to the container for dependency injection:
$this->app->singleton(Client::class, function ($app) {
return new Client(config('dyn.api_username'), config('dyn.api_password'));
});
Event Listeners Trigger actions on Dyn API events (e.g., record deletions):
event(new DnsRecordDeleted($record));
Scheduling Cleanup Tasks Schedule a command to delete stale DNS records or nodes:
// app/Console/Kernel.php
$schedule->command('delete:stale-dns')->daily();
Deprecated API
deleteDnsRecord method now supports node deletion (including child nodes).curl or Guzzle for unsupported endpoints if needed.Rate Limiting
$cacheKey = "dyn_records_{$domain}";
return Cache::remember($cacheKey, now()->addMinutes(5), function () use ($client, $domain) {
return $client->getDnsRecords($domain);
});
Authentication Failures
.env credentials are correct. Test with:
try {
$client->getAccountInfo();
} catch (\Exception $e) {
Log::error("Dyn Auth Failed: " . $e->getMessage());
}
Node Deletion Behavior (New in 0.11.0)
// Deletes ONLY the specified record (no children)
$client->deleteDnsRecord('example.com', 'A', '192.0.2.1');
// Deletes the node AND ALL child records (use sparingly)
$client->deleteDnsRecord('example.com', 'A', null, true);
API Versioning
delete node endpoint may behave differently across DynAPI versions. Verify behavior in the DynAPI documentation.Enable Verbose Logging
Set DYN_DEBUG=true in .env to log raw API responses.
Handle Exceptions Wrap SDK calls in try-catch blocks:
try {
$client->deleteDnsRecord('example.com', 'A', '192.0.2.1', true);
} catch (DynException $e) {
report($e);
Log::error("Failed to delete DNS node. Domain: example.com, Type: A", [
'exception' => $e,
'stack' => $e->getTraceAsString(),
]);
}
Custom Endpoints
Extend the Client class to support non-standard API calls:
class CustomClient extends Client {
public function customEndpoint($path, $data = []) {
return $this->request('POST', $path, $data);
}
}
Webhook Integration
Use Laravel’s queue:work to process Dyn webhook payloads asynchronously:
Route::post('/dyn-webhook', function (Request $request) {
dispatch(new ProcessDynWebhook($request->input()));
});
Testing
Mock the Client in PHPUnit:
$mock = Mockery::mock(Client::class);
$mock->shouldReceive('deleteDnsRecord')
->with('example.com', 'A', null, true)
->andReturn(true);
$this->app->instance(Client::class, $mock);
Soft Deletion Pattern Implement a soft-delete strategy to avoid accidental node deletions:
$client->updateDnsRecord('example.com', 'A', '192.0.2.1', [
'disabled' => true,
'ttl' => 300,
]);
Transaction-like Operations For critical operations, implement a rollback mechanism:
try {
$client->deleteDnsRecord('example.com', 'A', null, true);
// Backup records before deletion
Cache::put("backup_{$domain}", $client->getDnsRecords($domain), now()->addHours(1));
} catch (\Exception $e) {
// Restore from backup if needed
}
How can I help you explore Laravel packages today?