Skip to main contentSkip to navigation
[email protected]
Client AreaSupport
Hosting Mammoth
HostingMammothYour Data, Our Responsibility
Home
Solutions
Hosting Services
Store
Pricing
About
Blog
API
Contact

Stay Ahead of the Curve

Get the latest insights on cybersecurity, AI innovations, and enterprise data solutions delivered to your inbox.

Hosting Mammoth
HostingMammothEnterprise Solutions

Enterprise-grade data solutions. Hosting, recovery, cybersecurity, and AI-powered services for businesses worldwide.

[email protected]
Sun - Fri, 9:00am - 5:00pm

Services

  • Cloud Hosting
  • Data Recovery
  • Cybersecurity
  • Legal Support
  • MSP Services
  • Web Development
  • AI Services
  • Free Server Migration

Hosting

  • VPS Hosting (NVMe SSD)
  • VDS Hosting (NVMe)
  • Storage VPS (High SSD)
  • GPU Servers
  • Managed Services
  • Cloud Firewall
  • Load Balancer
  • One-Click Apps
  • n8n Hosting
  • Object Storage
  • FAQ

Company

  • Store
  • Pricing
  • About Us
  • Locations
  • Blog
  • Testimonials
  • Contact
  • Affiliate Program
  • White-Label
  • Terms of Service
  • Privacy Policy
  • Browser Cookies
  • SLA

Support

  • Client Area
  • Submit Ticket
  • Knowledge Base
  • Server Status
  • API Documentation

© 2026 Hosting Mammoth. All rights reserved.

Knowledge Base
Getting StartedAccount ManagementVPS HostingGPU ServersStorage VPSCloud FirewallLoad BalancerServer ManagementBilling & PaymentsSupport & TicketsAffiliate ProgramReseller ProgramMarketplace & Appsn8n HostingManaged ServicesServer MigrationAPI & DevelopersSecurityTroubleshootingGlossaryInstall Guides
  1. Home
  2. /
  3. Support
  4. /
  5. API & Developers
  6. /
  7. Api Code Examples Php
GUIDEAPI & Developers

API Code Examples — PHP

3 min read

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

php
<?php

define('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

php
$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

php
$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

php
$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

php
$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

php
$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

php
$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

php
$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

php
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
<?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.

Was this article helpful?

← Back to API & DevelopersBrowse all categories →

Still have questions?

Contact Support →Submit a Ticket