PHP code example of anocweb / netsapiens-api-php

1. Go to this page and download the library: Download anocweb/netsapiens-api-php 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/ */

    

anocweb / netsapiens-api-php example snippets


use NetSapiens\NetSapiensV2;
use NetSapiens\ApiVersion;

$client = new NetSapiensV2([
    'base_url' => 'https://your-netsapiens-server.com/v2',
    'access_token' => 'your-access-token',
    'timeout' => 60,
    'verify' => true
], ApiVersion::V44_4);

use NetSapiens\NetSapiensV2;
use NetSapiens\Config;
use NetSapiens\ApiVersion;

// Method chaining
$config = Config::create()
    ->baseUrl('https://your-netsapiens-server.com/v2')
    ->accessToken('your-access-token')
    ->timeout(60)
    ->verify(true);

$client = new NetSapiensV2($config, ApiVersion::V44_4);

// Or traditional setters
$config = new Config();
$config->setBaseUrl('https://your-netsapiens-server.com/v2');
$config->setAccessToken('your-access-token');
$config->setTimeout(60);
$config->setVerify(true);

$client = new NetSapiensV2($config, ApiVersion::V44_4);

// Use specific API version (e.g., v44.4)
use NetSapiens\NetSapiensV2;
use NetSapiens\ApiVersion;

$client = new NetSapiensV2([
    'base_url' => 'https://your-netsapiens-server.com/v2',
    'access_token' => 'your-access-token'
], ApiVersion::V44_4);

// Get domains (organized by endpoint)
$domains = $client->domains->searchDomains();

// Get users in a domain
$users = $client->users->searchUsers('your-domain');

// Create a user
$user = $client->users->createUser('your-domain', [
    'user' => 'john.doe',
    'first_name' => 'John',
    'last_name' => 'Doe'
]);

// Authentication - Individual parameters (with validation)
$token = $client->authentication->getAccessTokenFromUserpass(
    'password',
    'client-id',
    'client-secret', 
    'your-username',
    'your-password'
);

// Authentication - Configuration array
$token = $client->authentication->getAccessTokenFromUserpassWithConfig([
    'grant_type' => 'password',
    'client_id' => 'client-id',
    'client_secret' => 'client-secret',
    'username' => 'your-username',
    'password' => 'your-password'
]);

use NetSapiens\NetSapiensV1;
use NetSapiens\Config;

// Array configuration
$client = new NetSapiensV1([
    'base_url' => 'https://your-netsapiens-server.com',
    'access_token' => 'your-access-token'
]);

// Or OOP configuration
$config = Config::create()
    ->baseUrl('https://your-netsapiens-server.com')
    ->accessToken('your-access-token');

$client = new NetSapiensV1($config);

// Get queue statistics
$stats = $client->getQueueStats();

// Get CDR agent stats
$cdrStats = $client->getCDRAgentStats();

// Access and modify configuration after client creation
$config = $client->getConfig();
$config->setTimeout(120);
$config->setVerify(true);

// Default version (v44.4)
$client = new NetSapiensV2($config);

// Specific version
$client = new NetSapiensV2($config, ApiVersion::V45_1);

// Type-safe with validation and IDE support
$response = $client->authentication->getAccessTokenFromUserpass(
    'password',           // grant_type
    'your-client-id',     // client_id  
    'your-client-secret', // client_secret
    '[email protected]',    // username
    'password123'         // password
);

// Readable method chaining with self-documenting parameters
$response = $client->authentication->getAccessTokenFromUserpass()
    ->grant_type('password')
    ->client_id('your-client-id')
    ->client_secret('your-client-secret')
    ->username('[email protected]')
    ->password('password123')
    ->request();

$token = $client->authentication->getAccessTokenFromUserpass(...);

// Object properties
echo $token->status_code;  // HTTP status code
echo $token->bodyData;     // Response body data
echo $token->success;      // Boolean success flag
echo $token->error;        // Error message (if any)

// Methods
if ($token->isSuccess()) {
    $data = $token->getData();
}

// Array access - maintains backward compatibility
echo $token['status'];     // HTTP status code
echo $token['data'];       // Response body data
echo $token['success'];    // Boolean success flag
echo $token['error'];      // Error message

// Traditional usage
if ($token['success']) {
    $accessToken = $token['data']['access_token'];
}
bash
composer