These PHP examples demonstrate how to interact with the Data Mammoth API using PHP's built-in cURL functions. Each example is ready for integration into your PHP applications.
Setup
Configuration
<?phpdefine('API_BASE', 'https://api.datamammoth.com/v1'); define('API_KEY', getenv('DM_API_KEY'));
function apiRequest(string $method, string $endpoint, array $data = null): array { $url = API_BASE . '/' . ltrim($endpoint, '/'); $ch = curl_init();
curl_setopt_array($ch, [ CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => [ 'Authorization: Bearer ' . API_KEY, 'Content-Type: application/json', ], ]);
switch (strtoupper($method)) { case 'POST': curl_setopt($ch, CURLOPT_POST, true); if ($data) { curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); } break; case 'PATCH': case 'PUT': case 'DELETE': curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method)); if ($data) { curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); } break; }
$response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch);
return [ 'status' => $httpCode, 'body' => json_decode($response, true), ]; }
Server Management
List All Servers
$result = apiRequest('GET', '/servers');
if ($result['status'] === 200) { foreach ($result['body']['data'] as $server) { echo "{$server['name']} ({$server['ip']}) - {$server['status']}\n"; } }
Get Server Details
$serverId = 'srv_abc123'; $result = apiRequest('GET', "/servers/{$serverId}");
if ($result['status'] === 200) { $server = $result['body']['data']; echo "Name: {$server['name']}\n"; echo "IP: {$server['ip']}\n"; echo "Status: {$server['status']}\n"; echo "Plan: {$server['plan']}\n"; echo "OS: {$server['os']}\n"; }
Create a Server
$result = apiRequest('POST', '/servers', [ 'name' => 'web-production', 'plan' => 'vps-standard', 'region' => 'us-east', 'os' => 'ubuntu-24.04', 'hostname' => 'web01.example.com', 'tags' => ['production', 'web'], ]);
if ($result['status'] === 201) { $server = $result['body']['data']; echo "Server created: {$server['id']} ({$server['ip']})\n"; } else { echo "Error: {$result['body']['error']['message']}\n"; }
Restart a Server
$serverId = 'srv_abc123'; $result = apiRequest('POST', "/servers/{$serverId}/actions", [ 'action' => 'restart', ]);
if ($result['status'] === 200) { echo "Server restart initiated\n"; } else { echo "Error: {$result['body']['error']['message']}\n"; }
Billing
List Unpaid Invoices
$result = apiRequest('GET', '/billing/invoices?status=unpaid');
if ($result['status'] === 200) { foreach ($result['body']['data'] as $invoice) { printf( "Invoice %s: $%.2f (due %s)\n", $invoice['number'], $invoice['total'], $invoice['due_date'] ); } }
Check Account Balance
$result = apiRequest('GET', '/billing/balance');
if ($result['status'] === 200) { $balance = $result['body']['data']; printf("Account balance: $%.2f %s\n", $balance['balance'], $balance['currency']); }
Support Tickets
Create a Ticket
$result = apiRequest('POST', '/support/tickets', [ 'subject' => 'High CPU usage on web server', 'department' => 'technical', 'priority' => 'high', 'server_id' => 'srv_abc123', 'message' => 'CPU usage has been above 90% for the past 3 hours.', ]);
if ($result['status'] === 201) { echo "Ticket created: {$result['body']['data']['id']}\n"; }
Utility Functions
Paginated Fetching
function fetchAll(string $endpoint, array $params = []): array { $allItems = []; $page = 1;do { $params['page'] = $page; $params['per_page'] = 50; $queryString = http_build_query($params);
$result = apiRequest('GET', "{$endpoint}?{$queryString}"); $allItems = array_merge($allItems, $result['body']['data']);
$hasNext = $result['body']['meta']['has_next'] ?? false; $page++; } while ($hasNext);
return $allItems; }
// Usage $allServers = fetchAll('/servers', ['status' => 'running']); echo "Total running servers: " . count($allServers) . "\n";
Webhook Handler
<?php$webhookSecret = 'your_webhook_secret'; $payload = file_get_contents('php://input'); $signature = $_SERVER['HTTP_X_WEBHOOK_SIGNATURE'] ?? '';
// Verify signature $expected = 'sha256=' . hash_hmac('sha256', $payload, $webhookSecret);
if (!hash_equals($expected, $signature)) { http_response_code(401); echo json_encode(['error' => 'Invalid signature']); exit; }
// Process the event $event = json_decode($payload, true);
switch ($event['event']) { case 'server.status_changed': $serverName = $event['data']['name']; $newStatus = $event['data']['new_status']; error_log("Server {$serverName} changed to {$newStatus}"); break;
case 'billing.invoice_created': $invoiceNumber = $event['data']['number']; error_log("New invoice created: {$invoiceNumber}"); break;
case 'billing.payment_failed': error_log("Payment failed - immediate attention required"); break; }
http_response_code(200); echo json_encode(['status' => 'ok']);
What to Do Next
- API Code Examples — cURL — Command-line examples.
- API Code Examples — Python — Python examples.
- API Overview — Full API documentation.
- API Error Codes & Handling — Error reference.