PHPackages                             ahmadrosid/anthropic-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. ahmadrosid/anthropic-php

ActiveLibrary[API Development](/categories/api)

ahmadrosid/anthropic-php
========================

Anthropic PHP Client

v1.0.1(7mo ago)931.1k↑17.4%1[2 issues](https://github.com/ahmadrosid/anthropic-php/issues)MITPHPPHP ^8.1.0

Since Mar 19Pushed 7mo ago2 watchersCompare

[ Source](https://github.com/ahmadrosid/anthropic-php)[ Packagist](https://packagist.org/packages/ahmadrosid/anthropic-php)[ RSS](/packages/ahmadrosid-anthropic-php/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (4)Versions (3)Used By (0)

Anthropic PHP
=============

[](#anthropic-php)

Anthropic PHP is library to interact with Anthropic API, this library is designed to be close to OpenAI PHP. The goal of this library is to have the same API to OpenAI PHP client, so you can switch from GPT model to Claude easily.

Installation
------------

[](#installation)

Make sure you are using php: ^8.1.0.

```
composer require ahmadrosid/anthropic-php
```

How to use?
-----------

[](#how-to-use)

Create anthropic client.

```
use Anthropic\Anthropic;

$client = Anthropic::factory()
    ->withHeaders([
        'anthropic-version' => '2023-06-01',  // Required!
        'content-type' => 'application/json',
        'x-api-key' => env('ANTHROPIC_API_KEY', '')
    ])
    ->make();
```

**Important:** The `anthropic-version` header is required. Without it, you'll get a 404 error.

Available Models
----------------

[](#available-models)

- `claude-sonnet-4-5-20250929` - Latest and most intelligent model (best for coding)
- `claude-3-opus-20240229` - Powerful model for complex tasks
- `claude-3-haiku-20240307` - Fast and cost-effective model

Chat with Claude
----------------

[](#chat-with-claude)

Send chat message.

```
$response = $client->chat()->create([
    'model' => 'claude-sonnet-4-5-20250929',
    'max_tokens' => 4096,
    'system' => 'You are a helpful assistant.',
    'messages' => [
        [
            'role' => 'user',
            'content' => 'Hello, how are you?'
        ]
    ]
]);

$content = $response->choices[0]->message->content;
echo $content;
```

Chat Streaming
--------------

[](#chat-streaming)

Process server sent event reply from chatbot.

```
$model = 'claude-3-opus-20240229';
$max_tokens = 4096;
$temperature = 0;
$systemMessage = 'Always reply with "Hello!"';
$messages = [
    [
        'role' => 'user',
        'content' => 'Hi there...'
    ]
];
$stream = $client->chat()->createStreamed([
    'model' => $model,
    'temperature' => $temperature,
    'max_tokens' => $max_tokens,
    'system' => $systemMessage,
    'messages' => $messages,
]);

foreach ($stream as $response) {
    $text = $response->choices[0]->delta->content;

    echo $text;
}
```

Testing
-------

[](#testing)

This package includes both unit tests and integration tests.

### Running Unit Tests

[](#running-unit-tests)

Unit tests use mocked HTTP clients and don't require an API key:

```
composer test
```

Or run only unit tests:

```
vendor/bin/phpunit --testsuite Unit
```

### Running Integration Tests

[](#running-integration-tests)

Integration tests make real API calls to Anthropic. To run them:

1. Copy `.env.example` to `.env`:

    ```
    cp .env.example .env
    ```
2. Add your Anthropic API key to `.env`:

    ```
    ANTHROPIC_API_KEY=your-api-key-here

    ```
3. Run integration tests:

    ```
    vendor/bin/phpunit --testsuite Integration
    ```

**Note:** Integration tests will consume API credits.

### Running All Tests

[](#running-all-tests)

```
vendor/bin/phpunit
```

LICENSE
-------

[](#license)

MIT

###  Health Score

42

—

FairBetter than 90% of packages

Maintenance58

Moderate activity, may be stable

Popularity35

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~561 days

Total

2

Last Release

229d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/7d81b22a23cd7d401263a7966ae4ee19222b7e65ea4baa68c0d63ee7a4ce1e25?d=identicon)[ahmadrosid](/maintainers/ahmadrosid)

---

Top Contributors

[![ahmadrosid](https://avatars.githubusercontent.com/u/29406408?v=4)](https://github.com/ahmadrosid "ahmadrosid (7 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/ahmadrosid-anthropic-php/health.svg)

```
[![Health](https://phpackages.com/badges/ahmadrosid-anthropic-php/health.svg)](https://phpackages.com/packages/ahmadrosid-anthropic-php)
```

###  Alternatives

[stripe/stripe-php

Stripe PHP Library

4.0k143.3M480](/packages/stripe-stripe-php)[twilio/sdk

A PHP wrapper for Twilio's API

1.6k92.9M272](/packages/twilio-sdk)[facebook/php-business-sdk

PHP SDK for Facebook Business

90821.9M34](/packages/facebook-php-business-sdk)[meilisearch/meilisearch-php

PHP wrapper for the Meilisearch API

74513.7M114](/packages/meilisearch-meilisearch-php)[google/gax

Google API Core for PHP

265103.1M454](/packages/google-gax)[google/common-protos

Google API Common Protos for PHP

173103.7M50](/packages/google-common-protos)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
