PHP code example of gs2 / gs2-php-sdk

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

    

gs2 / gs2-php-sdk example snippets


use Gs2\Account\Gs2AccountRestClient;
use Gs2\Core\Model\BasicGs2Credential;
use Gs2\Core\Model\Region;
use Gs2\Core\Net\Gs2RestSession;

$session = new Gs2RestSession(
    new BasicGs2Credential(
        "your client id",
        "your client secret"
    ),
    Region::AP_NORTHEAST_1
);

$session->open();

$client = new Gs2AccountRestClient(
    $session
);

use Gs2\Account\Model\ScriptSetting;
use Gs2\Account\Request\CreateNamespaceRequest;
use Gs2\Core\Exception\Gs2Exception;
use PHPUnit\Framework\Assert;

try {
    $result = $client->createNamespace(
        (new CreateNamespaceRequest())
            ->withName('namespace-0001')
            ->withAuthenticationScript(
                (new ScriptSetting())
                    ->withTriggerScriptId('script-0001')
            )
    );
    
    Assert::assertNotNull($result->getItem());
    Assert::assertEquals('namespace-0001', $result->getItem()->getName());
    Assert::assertEquals('script-0001', $result->getItem()->getAuthenticationScript()->getTriggerScriptId());
} catch (Gs2Exception $e) {
    Assert::fail($e->getMessage());
}

use Gs2\Account\Model\ScriptSetting;
use Gs2\Account\Request\CreateNamespaceRequest;
use Gs2\Account\Result\CreateNamespaceResult;
use Gs2\Core\Exception\Gs2Exception;
use PHPUnit\Framework\Assert;

// 非同期処理をハンドリングするための Promise が返る
$promise = $client->createNamespaceAsync(
    (new CreateNamespaceRequest())
        ->withName('namespace-0001')
        ->withAuthenticationScript(
            (new ScriptSetting())
                ->withTriggerScriptId('script-0001')
        )
)->then(
    function (CreateNamespaceResult $result) {
        // コールバック形式でハンドリングしたい場合は成功時のハンドラーをここに記述
        Assert::assertNotNull($result->getItem());
        Assert::assertEquals('namespace-0001', $result->getItem()->getName());
        Assert::assertEquals('script-0001', $result->getItem()->getAuthenticationScript()->getTriggerScriptId());
    },
    function (Gs2Exception $e) {
        // コールバック形式でハンドリングしたい場合は失敗時のハンドラーをここに記述
        Assert::fail($e->getMessage());
    }
);

try {
    // Promise を wait することで処理が実行される。戻り値には成功時の結果が返り、失敗時には例外が発生する。
    $result = $promise->wait();
    Assert::assertNotNull($result->getItem());
    Assert::assertEquals('namespace-0001', $result->getItem()->getName());
    Assert::assertEquals('script-0001', $result->getItem()->getAuthenticationScript()->getTriggerScriptId());
} catch (Gs2Exception $e) {
    Assert::fail($e->getMessage());
}