PHP code example of platformsh / client

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

    

platformsh / client example snippets


use Platformsh\Client\Connection\Connector;
use Platformsh\Client\PlatformClient;

// Set up configuration.
$connector = new Connector([
    'api_url' => 'https://api.platform.sh',
    'accounts' => 'https://api.platform.sh/',
    'centralized_permissions_enabled' => true,
]);

// Initialize the client.
$client = new PlatformClient();

// Set the API token to use.
//
// N.B. you must keep your API token(s) safe!
$client->getConnector()->setApiToken('test', 'exchange');

// Get a project.
$project = $client->getProject('my_project_id');
if ($project) {
    // Get the default (production) environment.
    $environment = $project->getEnvironment($project->default_branch);

    // Create a new environment.
    $result = $environment->runOperation('branch', body: ['name' => 'sprint-1', 'title' => 'Sprint 1']);

    // Wait for the operation to complete.
    $activities = $result->getActivities();
    while (count($activities) > 0) {
        foreach ($activities as $key => $activity) {
            if ($activity->isComplete() || $activity->state === \Platformsh\Client\Model\Activity::STATE_CANCELLED) {
                unset($activities[$key]);
            } else {
                echo "Waiting for the activity: {$activity->getDescription()}\n";
                $activity->wait(function () { echo '.'; });
                echo "\n";
            }
        }
    }

    // Get the new branch.
    $sprint1 = $project->getEnvironment('sprint-1');
}

use \Platformsh\Client\Model\Subscription\SubscriptionOptions;

$subscription = $client->createSubscription(SubscriptionOptions::fromArray([
    'project_region' => 'uk-1.platform.sh',
    'project_title' => 'My project',
    'plan' => 'development',
    'default_branch' => 'main',
]));

echo "Created subscription $subscription->id, waiting for it to activate...\n";

$subscription->wait();

$project = $subscription->getProject();

echo "The project is now active: $project->id\n";
echo "Git URI: " . $project->getGitUrl() . "\n";