mozex/anthropic-php
Composer 安装命令:
composer require mozex/anthropic-php
包简介
PHP client for the Anthropic API: messages, streaming, tool use, thinking, web search, code execution, batches, and more.
关键字:
README 文档
README
A community-maintained PHP SDK for the Anthropic API. Send messages, stream responses, call tools, use extended thinking, search the web, execute code, process batches, and more. Works with any PSR-18 HTTP client.
Read the full documentation at mozex.dev: searchable docs, version requirements, detailed changelog, and more.
Using Laravel? Check out Anthropic Laravel, which wraps this SDK with service container integration, config-based setup, and a facade.
Table of Contents
- Introduction
- Usage
- Reference
Support This Project
I maintain this package along with several other open-source PHP packages used by thousands of developers every day.
If my packages save you time or help your business, consider sponsoring my work on GitHub Sponsors. Your support lets me keep these packages updated, respond to issues quickly, and ship new features.
Business sponsors get logo placement in package READMEs. See sponsorship tiers →
Why This Package
Built-in test client. Swap Anthropic\Client with ClientFake in your tests, queue fake responses, and assert exactly which requests were sent. No HTTP mocking libraries needed, no test server to run. See the testing docs →
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'; });
Forward-compatible. Parameters pass through to the API as-is. When Anthropic ships a new feature (a new tool type, a new thinking mode, a new parameter), it works in your code the same day. You don't wait for an SDK release.
Typed, immutable responses. Every response is a readonly PHP object with typed properties. Access $response->usage->inputTokens, not $response['usage']['input_tokens']. Full IDE autocompletion, no guessing.
Rate limits on every response. Call $response->meta() on any response (including streams and batch results) to get your current request limits, token limits, and reset times.
Any HTTP client. Built on PSR-18, so it works with Guzzle, Symfony HTTP Client, Buzz, or whatever your project already uses. No vendor lock-in.
Installation
Requires PHP 8.2+ - see all version requirements
composer require mozex/anthropic-php
The included php-http/discovery plugin finds and installs a compatible PSR-18 HTTP client automatically. If you want to use a specific one (like Guzzle or Symfony), see Configuration.
Quick Start
Create a client, send a message, read the response:
$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?
Streaming
Print text as it arrives:
$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; } }
Tool Use
Give Claude tools to call, execute them in your code, send results back:
$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'], ], 'required' => ['location'], ], ], ], 'messages' => [ ['role' => 'user', 'content' => 'What is the weather in San Francisco?'], ], ]); $response->content[1]->name; // 'get_weather' $response->content[1]->input['location']; // 'San Francisco'
Extended Thinking
Let Claude reason through complex problems before answering:
$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.'
Configuration
For custom base URIs, timeouts, or HTTP clients, use the factory:
$client = Anthropic::factory() ->withApiKey('your-api-key') ->withBaseUri('anthropic.example.com/v1') ->withHttpClient(new \GuzzleHttp\Client(['timeout' => 120])) ->withHttpHeader('X-Custom-Header', 'value') ->make();
The full documentation covers every feature in detail: web search and code execution, document citations, token counting, batch processing, error handling, rate limits, and more.
Resources
Visit the documentation site for searchable docs auto-updated from this repository.
- AI Integration: Use this package with AI coding assistants via Context7 and Laravel Boost
- Requirements: PHP and dependency versions
- Changelog: Release history with linked pull requests and diffs
- Contributing: Development setup, code quality, and PR guidelines
- Questions & Issues: Bug reports, feature requests, and help
- Security: Report vulnerabilities directly via email
License
The MIT License (MIT). Please see License File for more information.
mozex/anthropic-php 适用场景与选型建议
mozex/anthropic-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 510.97k 次下载、GitHub Stars 达 47, 最近一次更新时间为 2024 年 05 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「api」 「client」 「sdk」 「streaming」 「ai」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mozex/anthropic-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mozex/anthropic-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mozex/anthropic-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PSR-7 compatible library for making CRUD API endpoints
Mock PSR-18 HTTP client
Asynchronous MQTT client built on React
Client for Google Directions API to add interpolated points to a route consisting of given coordinates.
Alfabank REST API integration
A Flickr wrapper to allow you to call the Flickr api with Guzzle as the backend.Goal is to have 100% Flickr api coverage rather than just upload/display photos (currently at 23%).
统计信息
- 总下载量: 510.97k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 49
- 点击次数: 30
- 依赖项目数: 11
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2024-05-01