1. Go to this page and download the library: Download anthropic-ai/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/ */
$client = new Anthropic\Client(
requestOptions: Anthropic\RequestOptions::with(streamingTransporter: $myStreamingClient),
);
use Anthropic\Core\Helpers\StructuredOutputModel;
class Article extends StructuredOutputModel
{
public string $title;
public string $summary;
/** @var string[] */
public array $tags;
}
$message = $client->messages->create(
maxTokens: 1024,
messages: [['role' => 'user', 'content' => 'Write an article about PHP']],
model: 'claude-sonnet-4-5-20250929',
outputConfig: ['format' => Article::class],
);
$article = $message->content[0]->parsed; // Article instance
echo $article->title;
use Anthropic\Client;
$client = new Client(
apiKey: getenv('ANTHROPIC_API_KEY') ?: 'my-anthropic-api-key'
);
$page = $client->beta->messages->batches->list(limit: 20);
var_dump($page);
// fetch items from the current page
foreach ($page->getItems() as $item) {
var_dump($item->id);
}
// make additional network requests to fetch items from all pages, including and after the current page
foreach ($page->pagingEachItem() as $item) {
var_dump($item->id);
}
use Anthropic\Core\Exceptions\APIConnectionException;
use Anthropic\Core\Exceptions\RateLimitException;
use Anthropic\Core\Exceptions\APIStatusException;
try {
$message = $client->messages->create(
maxTokens: 1024,
messages: [['role' => 'user', 'content' => 'Hello, Claude']],
model: 'claude-sonnet-4-5-20250929',
);
} 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 Anthropic\Client;
// Configure the default for all requests:
$client = new Client(requestOptions: ['maxRetries' => 0]);
// Or, configure per-request:
$result = $client->messages->create(
maxTokens: 1024,
messages: [['role' => 'user', 'content' => 'Hello, Claude']],
model: 'claude-sonnet-4-5-20250929',
requestOptions: ['maxRetries' => 5],
);
use Anthropic\Core\FileParam;
// Pass a string with filename and content type:
$contents = file_get_contents('/path/to/file');
// Pass a string with filename and content type:
$fileMetadata = $client->beta->files->upload(
file: FileParam::fromString($contents, filename: '/path/to/file', contentType: '…'),
);
// Pass in only a string (where applicable)
$fileMetadata = $client->beta->files->upload(file: '…');
// Pass an open resource:
$fd = fopen('/path/to/file', 'r');
try {
$fileMetadata = $client->beta->files->upload(
file: FileParam::fromResource($fd, filename: '/path/to/file', contentType: '…'),
);
} finally {
fclose($fd);
}