PHP code example of david-canial / remote-config-tool

1. Go to this page and download the library: Download david-canial/remote-config-tool 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/ */

    

david-canial / remote-config-tool example snippets




avidCanial\RemoteConfigTool\RemoteConfig;

// アクセストークンを取得
$token = RemoteConfig::token();

// Remote Config(ETag 付き)を取得
$result = RemoteConfig::get();
$etag   = $result['etag'];
$config = $result['data'];

// 現在の ETag のみ取得
$etagOnly = RemoteConfig::etag();

// Remote Config の本体データのみ(ETag なし)
$dataOnly = RemoteConfig::config();

$etag = RemoteConfig::etag('your-firebase-project-id');

use DavidCanial\RemoteConfigTool\RemoteConfig;

// 現在の設定を取得
$result = RemoteConfig::get();
$etag   = $result['etag'];
$config = $result['data'];

// 例: 特定の parameter を変更したい場合
$parameters = $config['parameters'] ?? [];

$parameters['example_param'] = [
    'defaultValue' => ['value' => '123'],
];

// 古い ETag を指定して Firebase に送信
$updateResult = RemoteConfig::update(
    ['parameters' => $parameters],
    $etag
);

$newEtag = $updateResult['etag'] ?? null;

use DavidCanial\RemoteConfigTool\RemoteConfig;

// 新しい parameters を用意
$parameters = [
    'example_param' => [
        'defaultValue' => ['value' => 'force-update'],
    ],
];

$result = RemoteConfig::forceUpdate(['parameters' => $parameters]);
$newEtag = $result['etag'] ?? null;

use DavidCanial\RemoteConfigTool\RemoteConfigClient;
use GuzzleHttp\Client as HttpClient;

// 環境変数を利用してクライアントを生成
$client = new RemoteConfigClient();

// あるいは projectId と Guzzle クライアントを明示的に指定
//$http   = new HttpClient(['timeout' => 30]);
//$client = new RemoteConfigClient('your-firebase-project-id', $http);

// アクセストークンを取得
$token = $client->getToken();

// Remote Config を取得
$result = $client->get();
$etag   = $result['etag'];
$config = $result['data'];

use DavidCanial\RemoteConfigTool\RemoteConfigClient;

// 例: プロジェクトルートに .env がある場合
// .env
// FIREBASE_PROJECT_ID=your-firebase-project-id
// GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json

$client = RemoteConfigClient::createFromEnvFile(__DIR__ . '/..');

$result = $client->get();

use DavidCanial\RemoteConfigTool\RemoteConfig;
use GuzzleHttp\Exception\ClientException;

try {
    $result = RemoteConfig::get();
} catch (ClientException $e) {
    $response = $e->getResponse();
    $status   = $response->getStatusCode();
    $body     = (string) $response->getBody();

    // Firebase から返ってきたエラー内容をログに出すなどの処理
} catch (\Throwable $e) {
    // 環境変数の不足、ネットワークエラー、JSON パースエラーなど
}