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.2(1mo ago)936.4k↓45.8%1[2 issues](https://github.com/ahmadrosid/anthropic-php/issues)MITPHPPHP ^8.1.0

Since Mar 19Pushed 1mo 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 2d ago

READMEChangelog (3)Dependencies (8)Versions (5)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)

### Latest Models

[](#latest-models)

ModelAPI IDDescriptionClaude Opus 4.7`claude-opus-4-7`Most capable model for complex reasoning and agentic codingClaude Sonnet 4.6`claude-sonnet-4-6`Best combination of speed and intelligenceClaude Haiku 4.5`claude-haiku-4-5-20251001`Fastest model with near-frontier intelligence### Previous Generation (still available)

[](#previous-generation-still-available)

ModelAPI IDClaude Opus 4.6`claude-opus-4-6`Claude Sonnet 4.5`claude-sonnet-4-5-20250929`Claude Opus 4.5`claude-opus-4-5-20251101`Claude Opus 4.1`claude-opus-4-1-20250805`### Using Model Constants

[](#using-model-constants)

You can use the `Model` class for convenient access to model identifiers:

```
use Anthropic\Model;

$response = $client->chat()->create([
    'model' => Model::CLAUDE_OPUS_4_7,
    'max_tokens' => 4096,
    'messages' => [
        ['role' => 'user', 'content' => 'Hello!']
    ]
]);
```

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

[](#chat-with-claude)

Send chat message.

```
$response = $client->chat()->create([
    'model' => 'claude-opus-4-7',
    '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-sonnet-4-6';
$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

50

—

FairBetter than 95% of packages

Maintenance88

Actively maintained with recent releases

Popularity35

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 88.9% 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 ~398 days

Total

3

Last Release

40d ago

### Community

Maintainers

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

---

Top Contributors

[![ahmadrosid](https://avatars.githubusercontent.com/u/29406408?v=4)](https://github.com/ahmadrosid "ahmadrosid (8 commits)")[![devin-ai-integration[bot]](https://avatars.githubusercontent.com/in/811515?v=4)](https://github.com/devin-ai-integration[bot] "devin-ai-integration[bot] (1 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

[exsyst/swagger

A php library to manipulate Swagger specifications

35916.4M7](/packages/exsyst-swagger)[hubspot/api-client

Hubspot API client

24016.2M20](/packages/hubspot-api-client)[pocketmine/bedrock-protocol

An implementation of the Minecraft: Bedrock Edition protocol in PHP

172445.0k15](/packages/pocketmine-bedrock-protocol)[botman/driver-telegram

Telegram driver for BotMan

93459.5k6](/packages/botman-driver-telegram)

PHPackages © 2026

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