1. Go to this page and download the library: Download browserbase/stagehand 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/ */
browserbase / stagehand example snippets
tagehand\Client;
use Stagehand\Sessions\Action;
// Load environment variables from .env file if it exists
if (file_exists(__DIR__ . '/../.env')) {
$dotenv = parse_ini_file(__DIR__ . '/../.env');
foreach ($dotenv as $key => $value) {
if (!getenv($key)) {
putenv("$key=$value");
}
}
}
// Initialize the Stagehand client with API keys from environment variables
$client = new Client(
browserbaseAPIKey: getenv('BROWSERBASE_API_KEY') ?: throw new Exception('BROWSERBASE_API_KEY environment variable is possible actions
$observeResponse = $client->sessions->observe(
$sessionID,
instruction: 'find the link to view comments for the top post',
);
$actions = $observeResponse->data->result;
echo "Found " . count($actions) . " possible actions\n";
if (count($actions) === 0) {
echo "No actions found\n";
exit(0);
}
// Use the first action
$action = $actions[0];
echo "Acting on: {$action->description}\n";
// Pass the action to Act
$actResponse = $client->sessions->act(
$sessionID,
input: Action::with(
description: $action->description,
selector: $action->selector,
method: $action->method,
arguments: $action->arguments,
),
);
echo "Act completed: {$actResponse->data->result->message}\n";
// Extract data from the page
// We're now on the comments page, so extract the top comment text
$extractResponse = $client->sessions->extract(
$sessionID,
instruction: 'extract the text of the top comment on this page',
schema: [
'type' => 'object',
'properties' => [
'commentText' => [
'type' => 'string',
'description' => 'The text content of the top comment',
],
'author' => [
'type' => 'string',
'description' => 'The username of the comment author',
],
],
'
use Stagehand\Client;
$client = new Client(
browserbaseAPIKey: getenv('BROWSERBASE_API_KEY') ?: 'My Browserbase API Key',
modelAPIKey: getenv('MODEL_API_KEY') ?: 'My Model API Key',
);
$stream = $client->sessions->actStream(
'00000000-your-session-id-000000000000',
input: 'click the first link on the page',
);
foreach ($stream as $response) {
var_dump($response);
}
$client = new Stagehand\Client(
requestOptions: Stagehand\RequestOptions::with(streamingTransporter: $myStreamingClient),
);
use Stagehand\Core\Exceptions\APIConnectionException;
use Stagehand\Core\Exceptions\RateLimitException;
use Stagehand\Core\Exceptions\APIStatusException;
try {
$response = $client->sessions->start(modelName: 'openai/gpt-5.4-mini');
} catch (APIConnectionException $e) {
echo "The server could not be reached", PHP_EOL;
var_dump($e->getPrevious());
} catch (RateLimitException $e) {
echo "A 429 status code was received; we should back off a bit.", PHP_EOL;
} catch (APIStatusException $e) {
echo "Another non-200-range status code was received", PHP_EOL;
echo $e->getMessage();
}
use Stagehand\Client;
// Configure the default for all requests:
$client = new Client(requestOptions: ['maxRetries' => 0]);
// Or, configure per-request:
$result = $client->sessions->start(
modelName: 'openai/gpt-5.4-mini', requestOptions: ['maxRetries' => 5]
);
bash
# Set your credentials
export BROWSERBASE_API_KEY="your-browserbase-api-key"
export MODEL_API_KEY="your-openai-api-key"
# Install dependencies and run
composer install
php examples/basic.php
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.