PHP code example of ftw-soft / rundeck-api-client

1. Go to this page and download the library: Download ftw-soft/rundeck-api-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/ */

    

ftw-soft / rundeck-api-client example snippets



FtwSoft\Rundeck\Authentication\PasswordAuthentication;
use FtwSoft\Rundeck\Authentication\TokenAuthentication;
use FtwSoft\Rundeck\Client;
use GuzzleHttp\Client as HttpClient;
use Http\Factory\Guzzle\RequestFactory;
use Http\Factory\Guzzle\StreamFactory;

$httpClient = new HttpClient();

// --- Setup authentication ---
# Password authentication
$authentication = new PasswordAuthentication(
    'https://rundeck.local',
    'username',
    'password',
    $httpClient,
    new RequestFactory(),
    new StreamFactory()
);

# OR Token based authentication
$authentication = new TokenAuthentication('secret-token');

// --- Initialize client ---
$client = new Client(
    'https://rundeck.local',
    $authentication,
    $httpClient,
    new RequestFactory(),
    new StreamFactory(),
    36 // optional API version
);

// Make a request
$response = $client->request('GET', 'projects');
var_dump($response->getBody()->getContents());


FtwSoft\Rundeck\Resource\Tokens as TokensResource;
use FtwSoft\Rundeck\Client;
use FtwSoft\Rundeck\Entity\TokenEntity;

/** @var Client $client */
$tokensResource = new TokensResource($client);

/** @var TokenEntity[] $tokens */
$tokens = $tokensResource->get();

foreach ($tokens as $token) {
    echo '====================================';
    echo 'id: ', $token->getId(), PHP_EOL;
    echo 'user:', $token->getUser(), PHP_EOL;
    echo 'token: ', $token->getToken(), PHP_EOL;
    echo 'creator: ', $token->getCreator(), PHP_EOL;
    echo 'expire at: ', $token->getExpiration()->format(\DATE_ATOM), PHP_EOL;
    echo 'roles: ', implode(', ', $token->getRoles()), PHP_EOL;
    echo 'is expired: ', $token->isExpired() ? 'yes' : 'no', PHP_EOL;
}