1. Go to this page and download the library: Download dapr/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/ */
// retrieve a single secret
$client->getSecret(storeName: 'kubernetes', key: 'test');
// retrieve all secrets
$client->getBulkSecret(storeName: 'kubernetes');
#[\Dapr\State\Attributes\StateStore('statestore', \Dapr\consistency\StrongFirstWrite::class)]
class SomeState extends \Dapr\State\TransactionalState {
public string $value;
public function ok(): void {
$this->value = 'ok';
}
}
$app = Dapr\App::create();
$app->get('/do-work', function(SomeState $state) {
$state->begin();
$state->value = 'not-ok';
$state->ok();
$state->commit();
return $state;
});
$app->start();
/**
* Actor that keeps a count
*/
#[\Dapr\Actors\Attributes\DaprType('ExampleActor')]
interface ICounter {
/**
* Increment a counter
*/
public function increment(int $amount): void;
}
class CountState extends \Dapr\Actors\ActorState {
public int $count = 0;
}
#[\Dapr\Actors\Attributes\DaprType('Counter')]
class Counter extends \Dapr\Actors\Actor implements ICounter {
/**
* Initialize the class
*/
public function __construct(string $id, private CountState $state) {
parent::__construct($id);
}
/**
* Increment the count by 1
*/
public function increment(int $amount): void {
$this->state->count += $amount;
}
}
// register the actor with the runtime
$app = \Dapr\App::create(configure: fn(\DI\ContainerBuilder $builder) => $builder->addDefinitions([
'dapr.actors' => [Counter::class]
]));
$app->start();
use Dapr\Actors\ActorProxy;
$app = \Dapr\App::create();
$app->get('/increment/{actorId}[/{amount:\d+}]', function(string $actorId, ActorProxy $actorProxy, int $amount = 1) {
$counter = $actorProxy->get(ICounter::class, $actorId);
$counter->increment($amount);
$counter->create_reminder('increment', new \Dapr\Actors\Reminder('increment', new DateInterval('PT10M'), data: 10 ));
});
$app->start();