Quickstart - Setup
Configure your local development environment to get started developing with Temporal.
Create your project directory
First, create and navigate into your project directory:
mkdir helloworld-temporalphp && cd helloworld-temporalphp
Install the Temporal PHP SDK
Install the PHP SDK using Composer:
If you don't already have Composer installed, you can install it using Homebrew on macOS: Or, follow these instructions on the Composer website for Linux or Windows.
composer require temporal/sdk
Download RoadRunner
See the RoadRunner README file for installation instructions.
When prompted, enter yes to generate a default .rr.yaml
file.
You’ll update it later to configure the Temporal plugin.
Next, you'll configure a local Temporal Service for development.
See the RoadRunner README file for installation instructions.
Using Composer, you can install it with the following command:
composer require spiral/roadrunner-cli ./vendor/bin/rr get-binary
Install Temporal CLI and start the development server
The fastest way to get a development version of the Temporal Service running on your local machine is to use Temporal CLI.
Choose your operating system to install Temporal CLI:
- macOS
- Windows
- Linux
Install the Temporal CLI using Homebrew:
brew install temporal
Download the Temporal CLI archive for your architecture:
Extract it and add temporal.exe
to your PATH.
Download the Temporal CLI for your architecture:
Extract the archive and move the temporal
binary into your PATH, for example:
sudo mv temporal /usr/local/bin
Create a RoadRunner configuration file
To configure RoadRunner Temporal plugin create or open the .rr.yaml
file in your project directory and make sure it contains the following:
Replace the default contents of your .rr.yaml
file with the following configuration, which is required to run the Temporal Worker:
This example shows a minimal .rr.yaml
configuration specifically for running a Temporal Worker.
version: "3"
rpc:
listen: tcp://127.0.0.1:6001
server:
command: "php -d display_errors=stderr src/worker.php"
temporal:
address: ${TEMPORAL_HOST:-localhost}:${TEMPORAL_PORT:-7233}
activities:
num_workers: 4
logs:
level: info
mode: development
Start the development server
Once you've installed Temporal CLI and added it to your PATH, open a new Terminal window and run the following command.
This command starts a local Temporal Service. It starts the Web UI, creates the default Namespace, and uses an in-memory database.
The Temporal Service will be available on localhost:7233. The Temporal Web UI will be available at http://localhost:8233.
Leave the local Temporal Service running as you work through tutorials and other projects. You can stop the Temporal Service at any time by pressing CTRL+C.
Once you have everything installed, you're ready to build apps with Temporal on your local machine.
After installing, open a new Terminal window and start the development server:
temporal server start-dev
Change the Web UI port
The Temporal Web UI may be on a different port in some examples or tutorials. To change the port for the Web UI, use the --ui-port
option when starting the server:
temporal server start-dev --ui-port 8080
The Temporal Web UI will now be available at http://localhost:8080.
Run Hello World: Test Your Installation
Now let's verify your setup is working by creating and running a complete Temporal application with both a Workflow and Activity.
This test will confirm that:
- Your PHP SDK installation is working
- Your local Temporal Service is running
- You can successfully create and execute Workflows and Activities
- The communication between components is functioning correctly
1. Create the Activity Interface
Create an Activity Interface file (GreetingActivityInterface.php) in an Activities subdirectory:
<?php
declare(strict_types=1);
namespace App\Activities;
use Temporal\Activity\ActivityInterface;
use Temporal\Activity\ActivityMethod;
#[ActivityInterface]
interface GreetingActivityInterface
{
#[ActivityMethod]
public function greet(string $name): string;
}
2. Create the Activity Implementation
An Activity is a normal function or method that executes a single, well-defined action (either short or long running), which often involve interacting with the outside world, such as sending emails, making network requests, writing to a database, or calling an API, which are prone to failure. If an Activity fails, Temporal automatically retries it based on your configuration.
Create an Activity implementation file (GreetingActivity.php) in the Activities subdirectory:
<?php
declare(strict_types=1);
namespace App\Activities;
class GreetingActivity implements GreetingActivityInterface
{
public function greet(string $name): string
{
return "Hello {$name}";
}
}
3. Create the Workflow Interface
Create a Workflow Interface file (SayHelloWorkflowInterface.php) in a Workflows subdirectory:
<?php
declare(strict_types=1);
namespace App\Workflows;
use Temporal\Workflow\WorkflowInterface;
use Temporal\Workflow\WorkflowMethod;
#[WorkflowInterface]
interface SayHelloWorkflowInterface
{
#[WorkflowMethod]
public function sayHello(string $name): \Generator;
}
4. Create the Workflow Implementation
Workflows orchestrate Activities and contain the application logic. Temporal Workflows are resilient. They can run—and keeping running—for years, even if the underlying infrastructure fails. If the application itself crashes, Temporal will automatically recreate its pre-failure state so it can continue right where it left off.
Create a Workflow implementation file (SayHelloWorkflow.php) in the Workflows subdirectory:
<?php
declare(strict_types=1);
namespace App\Workflows;
use App\Activities\GreetingActivityInterface;
use Temporal\Workflow;
use Temporal\Activity\ActivityOptions;
use Carbon\CarbonInterval;
class SayHelloWorkflow implements SayHelloWorkflowInterface
{
public function sayHello(string $name): \Generator
{
$activity = Workflow::newActivityStub(
GreetingActivityInterface::class,
ActivityOptions::new()
->withScheduleToCloseTimeout(CarbonInterval::seconds(10))
);
return yield $activity->greet($name);
}
}
5. Create and Run the Worker
With your Activity and Workflow defined, you need a Worker to execute them. A Worker polls a Task Queue, that you configure it to poll, looking for work to do. Once the Worker dequeues the a Workflow or Activity task from the Task Queue, it then executes that task.
Workers are a crucial part of your Temporal application as they're what actually execute the tasks defined in your Workflows and Activities. For more information on Workers, see Understanding Temporal and a deep dive into Workers.
Create a Worker file (worker.php):
<?php
declare(strict_types=1);
use Temporal\WorkerFactory;
require __DIR__ . '/vendor/autoload.php';
$factory = WorkerFactory::create();
$worker = $factory->newWorker();
$worker->registerWorkflowTypes(\App\Workflows\SayHelloWorkflow::class);
$worker->registerActivityImplementations(new \App\Activities\GreetingActivity());
$factory->run();
Run the Worker:
php worker.php
6. Execute the Workflow
Now that your Worker is running, it's time to start a Workflow Execution.
Create a separate file called starter.php:
<?php
declare(strict_types=1);
use Temporal\Client\WorkflowClient;
use Temporal\Client\WorkflowOptions;
require __DIR__ . '/vendor/autoload.php';
$workflowClient = WorkflowClient::create('localhost:7233');
$workflow = $workflowClient->newWorkflowStub(
\App\Workflows\SayHelloWorkflowInterface::class,
WorkflowOptions::new()
->withWorkflowId('say-hello-workflow-id')
->withTaskQueue('my-task-queue')
);
$result = $workflow->sayHello('Temporal');
echo "Workflow result: " . $result . PHP_EOL;
Then run:
php starter.php
Verify Success
If everything is working correctly, you should see:
- Worker processing the Workflow and Activity
- Output:
Workflow result: Hello Temporal
- Workflow Execution details in the Temporal Web UI
Next: Run your first Temporal Application
Learn how to create a basic Workflow and run it with the Temporal PHP SDK