PHP code example of sherinbloemendaal / phpipam-client

1. Go to this page and download the library: Download sherinbloemendaal/phpipam-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sherinbloemendaal / phpipam-client example snippets




declare(strict_types=1);

use SherinBloemendaal\PhpIPAMClient\PhpIPAMClient;
use SherinBloemendaal\PhpIPAMClient\Connection\Connection;

// Initialize client with SSL (recommended)
$client = new PhpIPAMClient(
    url: 'https://phpipam.example.com',
    appID: 'myApp',
    username: 'admin',
    password: 'secret',
    apiKey: '', // Not needed for SSL
    securityMethod: Connection::SECURITY_METHOD_SSL
);

// Or initialize connection statically
Connection::initializeConnection(
    url: 'https://phpipam.example.com',
    appID: 'myApp', 
    username: 'admin',
    password: 'secret'
);

use SherinBloemendaal\PhpIPAMClient\Controller\Section;

// Get all sections
$sections = Section::getAll();

// Get section by ID
$section = Section::getByID(1);

// Get section by name
$section = Section::getByName('Production');

// Create new section
$section = Section::post([
    'name' => 'Development',
    'description' => 'Development networks',
    'strictMode' => true
]);

// Update section
$section->setDescription('Updated description')
        ->setStrictMode(false)
        ->patch();

// Delete section
$section->delete();

use SherinBloemendaal\PhpIPAMClient\Controller\Subnet;
use SherinBloemendaal\PhpIPAMClient\Controller\Section;

// Get all subnets
$subnets = Subnet::getAll();

// Create subnet in a section
$section = Section::getByName('Production');
$subnet = Subnet::post([
    'subnet' => '192.168.1.0',
    'mask' => 24,
    'description' => 'Web servers',
    'sectionId' => $section, // Can use object or ID
]);

// Get subnet usage
$usage = $subnet->getUsage();

// Get addresses in subnet  
$addresses = $subnet->getAddresses();

// Find first free IP
$freeIP = $subnet->getFirstFree();

use SherinBloemendaal\PhpIPAMClient\Controller\Address;

// Search for IP address
$addresses = Address::getSearchByIP('192.168.1.100');

// Get address by IP and subnet
$address = Address::getByIPAndSubnet('192.168.1.100', $subnet);

// Create new address
$address = Address::postFirstFree($subnet, [
    'hostname' => 'web01.example.com',
    'description' => 'Web server',
    'owner' => 'IT Department'
]);

// Update address
$address->setHostname('web01-new.example.com')
        ->setDescription('Updated web server')
        ->patch();

$client = new PhpIPAMClient(
    url: 'https://phpipam.example.com',
    appID: 'myApp',
    username: 'admin', 
    password: 'secret',
    apiKey: '',
    securityMethod: Connection::SECURITY_METHOD_SSL
);

$client = new PhpIPAMClient(
    url: 'http://phpipam.example.com',
    appID: 'myApp',
    username: '',
    password: '',
    apiKey: 'your-api-key',
    securityMethod: Connection::SECURITY_METHOD_CRYPT
);

$client = new PhpIPAMClient(
    url: 'https://phpipam.example.com',
    appID: 'myApp',
    username: 'admin',
    password: 'secret', 
    apiKey: 'your-api-key',
    securityMethod: Connection::SECURITY_METHOD_BOTH
);

use SherinBloemendaal\PhpIPAMClient\Controller\{Section, Subnet, Address, VLAN};

// Fluent interface with method chaining
$section = Section::getByName('Production')
    ->setDescription('Production environment')
    ->patch();

// Object relationships - automatic ID conversion
$subnet = Subnet::post([
    'subnet' => '10.0.0.0',
    'mask' => 16,
    'sectionId' => $section, // Pass object, not ID
]);

// Work with related objects
$addresses = $subnet->getAddresses();
$firstFree = Address::postFirstFree($subnet, [
    'hostname' => 'server01.prod.local'
]);

// Raw API call
$response = $client->call('GET', 'sections', [1], []);
$data = $response->getData();

// Check response
if ($response->isSuccess()) {
    echo "Success: " . $response->getMessage();
} else {
    echo "Error: " . $response->getMessage();
}

use SherinBloemendaal\PhpIPAMClient\Exception\{PhpIPAMException, PhpIPAMRequestException};

try {
    $section = Section::getByID(999);
} catch (PhpIPAMRequestException $e) {
    // API request failed
    $response = $e->getResponse();
    echo "API Error: " . $response->getMessage();
    echo "HTTP Code: " . $response->getCode();
} catch (PhpIPAMException $e) {
    // Client library error  
    echo "Client Error: " . $e->getMessage();
}

use SherinBloemendaal\PhpIPAMClient\Connection\Connection;

// Initialize once, use everywhere
Connection::initializeConnection(
    url: 'https://phpipam.example.com',
    appID: 'myApp',
    username: 'admin',
    password: 'secret'
);

// Get connection instance
$connection = Connection::getInstance();

// Check token status
$token = $connection->getToken();
$expires = $connection->getTokenExpires();

// Get custom fields for addresses
$customFields = Address::getCustomFields();

// Create address with custom fields
$address = Address::postFirstFree($subnet, [
    'hostname' => 'server01',
    'custom_Environment' => 'Production',
    'custom_Owner' => 'IT Team'
]);

$response = $client->call('GET', 'sections');

$response->getCode();      // HTTP status code
$response->isSuccess();    // Success boolean
$response->getMessage();   // Response message
$response->getData();      // Response data
$response->getTime();      // Response time
$response->getBody();      // Full response body