1. Go to this page and download the library: Download mozex/anthropic-php 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/ */
mozex / anthropic-php example snippets
use Anthropic\Testing\ClientFake;
use Anthropic\Responses\Messages\CreateResponse;
$client = new ClientFake([
CreateResponse::fake([
'content' => [['type' => 'text', 'text' => 'Paris is the capital of France.']],
]),
]);
$response = $client->messages()->create([...]);
$client->assertSent(Messages::class, function (string $method, array $parameters): bool {
return $parameters['model'] === 'claude-sonnet-4-6';
});
$client = Anthropic::client('your-api-key');
$response = $client->messages()->create([
'model' => 'claude-sonnet-4-6',
'max_tokens' => 1024,
'messages' => [
['role' => 'user', 'content' => 'Hello!'],
],
]);
echo $response->content[0]->text; // Hello! How can I assist you today?
$stream = $client->messages()->createStreamed([
'model' => 'claude-sonnet-4-6',
'max_tokens' => 1024,
'messages' => [
['role' => 'user', 'content' => 'Tell me a short story.'],
],
]);
foreach ($stream as $response) {
if ($response->type === 'content_block_delta'
&& $response->delta->type === 'text_delta') {
echo $response->delta->text;
}
}
$response = $client->messages()->create([
'model' => 'claude-sonnet-4-6',
'max_tokens' => 1024,
'tools' => [
[
'name' => 'get_weather',
'description' => 'Get the current weather in a given location',
'input_schema' => [
'type' => 'object',
'properties' => [
'location' => ['type' => 'string'],
],
'
$response = $client->messages()->create([
'model' => 'claude-opus-4-6',
'max_tokens' => 16000,
'thinking' => ['type' => 'adaptive'],
'messages' => [
['role' => 'user', 'content' => 'What is the GCD of 1071 and 462?'],
],
]);
// Thinking block with Claude's reasoning process
$response->content[0]->thinking; // 'Using the Euclidean algorithm...'
// Final answer
$response->content[1]->text; // 'The GCD of 1071 and 462 is 21.'