PHP code example of aluvia-connect / aluvia-php-sdk

1. Go to this page and download the library: Download aluvia-connect/aluvia-php-sdk 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/ */

    

aluvia-connect / aluvia-php-sdk example snippets





use Aluvia\Aluvia;

$sdk = new Aluvia('your-api-token');

// Get your first connectivity credential
$credential = $sdk->first();

if ($credential) {
    echo "Connection URL: " . $credential->toUrl() . "\n";
    echo "Username: " . $credential->getUsername() . "\n";
    echo "Password: " . $credential->getPassword() . "\n";
} else {
    echo "No credentials available. Create one first.\n";
}


$sdk = new Aluvia('your-api-token');

// Create a single credential
$credentials = $sdk->create(1);
$credential = $credentials[0];
echo "Created: " . $credential->toUrl() . "\n";

// Create multiple
$list = $sdk->create(5);
echo "Created " . count($list) . " credentials\n";


$sdk = new Aluvia('your-api-token');

$credential = $sdk->find('user123');

if ($credential) {
    // Modify settings
    $credential->setUseSticky(true);

    // Save changes
    $credential->save();

    echo "Credential updated.\n";
} else {
    echo "Credential not found.\n";
}


$sdk = new Aluvia('your-api-token');
$credential = $sdk->first();

if ($credential) {
    // Current period usage
    $usage = $credential->getUsage();
    echo "Used: " . $usage['dataUsed'] . " GB\n";

    // Custom date range
    $weekly = $sdk->getUsage($credential->getUsername(), [
        'usageStart' => time() - (7 * 86400),
        'usageEnd'   => time(),
    ]);

    echo "Weekly usage: " . $weekly['dataUsed'] . " GB\n";
}


$credential = $sdk->first();

if ($credential) {
    echo "HTTP: " . $credential->toUrl('http') . "\n";
    echo "HTTPS: " . $credential->toUrl('https') . "\n";
    print_r($credential->toArray());
}


$credential = $sdk->first();

if ($credential) {
    $ch = curl_init();

    curl_setopt_array($ch, [
        CURLOPT_URL => 'https://ipconfig.io/json',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_PROXY => $credential->getHost() . ':' . $credential->getHttpPort(),
        CURLOPT_PROXYUSERPWD => $credential->getUsername() . ':' . $credential->getPassword(),
        CURLOPT_PROXYTYPE => CURLPROXY_HTTP,
    ]);

    $response = curl_exec($ch);
    curl_close($ch);

    echo "Response: $response\n";
}


$credential = $sdk->find('user123');

if ($credential) {
    $credential->delete();
    echo "Credential deleted.\n";
}

$sdk->delete('anotherUser');


use Aluvia\Exceptions\AuthenticationException;
use Aluvia\Exceptions\ValidationException;
use Aluvia\Exceptions\NetworkException;
use Aluvia\Exceptions\ApiException;
use Aluvia\Exceptions\RateLimitException;

try {
    $credential = $sdk->first();
} catch (AuthenticationException $e) {
    echo "Auth error: " . $e->getMessage();
} catch (RateLimitException $e) {
    echo "Rate limited. Retry in " . $e->getRetryAfter() . "s";
} catch (NetworkException $e) {
    echo "Network issue: " . $e->getMessage();
} catch (ApiException $e) {
    echo "API error: " . $e->getMessage();
} catch (ValidationException $e) {
    echo "Invalid request: " . $e->getMessage();
}