PHP

Dapr offers an SDK to help with the development of PHP applications. Using it, you can create PHP clients, servers, and virtual actors with Dapr.

Setting up

Prerequisites

Optional Prerequisites

Initialize your project

In a directory where you want to create your service, run composer init and answer the questions. Install with composer require dapr/php-sdk and any other dependencies you may wish to use.

Configure your service

Create a config.php, copying the contents below:

  1. <?php
  2. use Dapr\Actors\Generators\ProxyFactory;
  3. use Dapr\Middleware\Defaults\{Response\ApplicationJson,Tracing};
  4. use Psr\Log\LogLevel;
  5. use function DI\{env,get};
  6. return [
  7. // set the log level
  8. 'dapr.log.level' => LogLevel::WARNING,
  9. // Generate a new proxy on each request - recommended for development
  10. 'dapr.actors.proxy.generation' => ProxyFactory::GENERATED,
  11. // put any subscriptions here
  12. 'dapr.subscriptions' => [],
  13. // if this service will be hosting any actors, add them here
  14. 'dapr.actors' => [],
  15. // if this service will be hosting any actors, configure how long until dapr should consider an actor idle
  16. 'dapr.actors.idle_timeout' => null,
  17. // if this service will be hosting any actors, configure how often dapr will check for idle actors
  18. 'dapr.actors.scan_interval' => null,
  19. // if this service will be hosting any actors, configure how long dapr will wait for an actor to finish during drains
  20. 'dapr.actors.drain_timeout' => null,
  21. // if this service will be hosting any actors, configure if dapr should wait for an actor to finish
  22. 'dapr.actors.drain_enabled' => null,
  23. // you shouldn't have to change this, but the setting is here if you need to
  24. 'dapr.port' => env('DAPR_HTTP_PORT', '3500'),
  25. // add any custom serialization routines here
  26. 'dapr.serializers.custom' => [],
  27. // add any custom deserialization routines here
  28. 'dapr.deserializers.custom' => [],
  29. // the following has no effect, as it is the default middlewares and processed in order specified
  30. 'dapr.http.middleware.request' => [get(Tracing::class)],
  31. 'dapr.http.middleware.response' => [get(ApplicationJson::class), get(Tracing::class)],
  32. ];

Create your service

Create index.php and put the following contents:

  1. <?php
  2. require_once __DIR__.'/vendor/autoload.php';
  3. use Dapr\App;
  4. $app = App::create(configure: fn(\DI\ContainerBuilder $builder) => $builder->addDefinitions(__DIR__ . '/config.php'));
  5. $app->get('/hello/{name}', function(string $name) {
  6. return ['hello' => $name];
  7. });
  8. $app->start();

Try it out

Initialize dapr with dapr init and then start the project with dapr run -a dev -p 3000 -- php -S 0.0.0.0:3000.

You can now open a web browser and point it to http://localhost:3000/hello/world replacing world with your name, a pet’s name, or whatever you want.

Congratulations, you’ve created your first Dapr service! I’m excited to see what you’ll do with it!

More Information