ahmadrosid/anthropic-php 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ahmadrosid/anthropic-php

Composer 安装命令:

composer require ahmadrosid/anthropic-php

包简介

Anthropic PHP Client

README 文档

README

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

Make sure you are using php: ^8.1.0.

composer require ahmadrosid/anthropic-php

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

Latest Models

Model API ID Description
Claude Opus 4.7 claude-opus-4-7 Most capable model for complex reasoning and agentic coding
Claude Sonnet 4.6 claude-sonnet-4-6 Best combination of speed and intelligence
Claude Haiku 4.5 claude-haiku-4-5-20251001 Fastest model with near-frontier intelligence

Previous Generation (still available)

Model API ID
Claude 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

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

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

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

This package includes both unit tests and integration 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

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

vendor/bin/phpunit

LICENSE

MIT

ahmadrosid/anthropic-php 适用场景与选型建议

ahmadrosid/anthropic-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 35.96k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2024 年 03 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 ahmadrosid/anthropic-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ahmadrosid/anthropic-php 我们能提供哪些服务?
定制开发 / 二次开发

基于 ahmadrosid/anthropic-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 35.96k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 9
  • 点击次数: 20
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 9
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-03-19