PHPackages                             guanhui07/open-ai - 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. guanhui07/open-ai

ActiveLibrary[API Development](/categories/api)

guanhui07/open-ai
=================

OpenAI API Client in PHP

1.0.2(1y ago)119MITPHPPHP &gt;=7.1

Since Aug 23Pushed 1y ago1 watchersCompare

[ Source](https://github.com/guanhui07/open-ai)[ Packagist](https://packagist.org/packages/guanhui07/open-ai)[ Docs](https://github.com/swoole-inc/open-ai)[ GitHub Sponsors](https://github.com/orhanerday)[ RSS](/packages/guanhui07-open-ai/feed)WikiDiscussions main Synced 2d ago

READMEChangelog (2)DependenciesVersions (6)Used By (0)

OpenAI API Client in PHP
========================

[](#openai-api-client-in-php)

为何 `fork` 此库而不是向原项目发起 `PR` ，主要是是作者的响应时间太长，一个非常简单的 `PR` 经常 `1-2` 周的时间没有任何回复。 我们需要快速验证迭代，后续会抽时间提交给 `orhanerday/open-ai`

改进
--

[](#改进)

对 `orhanerday/open-ai` 库做了一些改进，包括如下内容：

1. 支持了获取 `curl` 底层错误码，在出现网络底层的问题后可以根据错误信息排查问题
2. 支持设置 `HTTP` 版本，使用方法 `$openai->setHttpVersion(2)` ，通过设置 `HTTP2` 协议可以绕过 Nginx 的 `proxy cache`
3. 改进了 `setBaseUrl()` 方法，原库是硬编码写死了 `api.openai.com`，发送请求前通过字符串替换设置 `BaseUrl`，不是很优雅
4. 改进了 `chat stream` 的实现，原库直接使用了 `WRITE_FUNCTION`，应用层需要分割 `Chunks`并手工解析 `Event-Stream` 消息
5. 兼容php7

使用
--

[](#使用)

```
composer require guanhui07/open-ai
```

实例
--

[](#实例)

```
define('BASE_PATH', dirname(__DIR__));
require BASE_PATH . '/vendor/autoload.php';

use SwooleAi\OpenAi\OpenAi;

$open_ai = new OpenAi('test');
$open_ai->setBaseURL('https://api.openai.com/');
$messages[] = ["role" => "system", "content" => "You are a helpful assistant."];
$messages[] = ["role" => "user", "content" => "Who won the world series in 2020?"];
$messages[] = ["role" => "assistant", "content" => "The Los Angeles Dodgers won the World Series in 2020."];
$messages[] = ["role" => "user", "content" => "Where was it played?"];
$complete = $open_ai->chat([
    'model' => 'gpt-3.5-turbo',
    'messages' => $messages
], function ($curl_info, $data) use (&$txt) {
    if ($data !== '[DONE]') {
        $json = json_decode($data, true);
        $txt .= $json['choices'][0]['delta']['content'];
    }
});
var_dump($complete);
if ($complete) {
    var_dump($txt);
} else {
    var_dump($open_ai->getError(), $open_ai->getErrno() === CURLE_COULDNT_CONNECT);
}
```

代理
--

[](#代理)

在中国无法直接访问 `OpenAI` 服务器，可以设置代理。

```
$open_ai = new OpenAi('test');
$open_ai->setProxy('socks5h://127.0.0.1:1080');
```

- `socks5h://`：`socks5` 代理并且在对端进行 `DNS` 解析（推荐使用）
- `socks5://`：`socks5` 代理并且在本地进行 `DNS` 解析
- `http://`：`HTTP` 代理

我的其他包：
------

[](#我的其他包)

 借鉴Laravel实现的 PHP Framework ，FPM模式、websocket使用的workerman、支持容器、PHP8特性attributes实现了路由注解、中间件注解、Laravel Orm等特性

 Swoole模式下 Redis连接池

 facade、门面 fpm模式下可使用

 基于swoole实现的crontab秒级定时任务

 基于 illuminate/database 做的连接池用于适配Swoole的协程环境

 高性能PHP Framework ，Cli模式，基于Swoole实现，常驻内存，协程框架，支持容器、切面、PHP8特性attributes实现了路由注解、中间件注解、支持Laravel Orm等特性

 chatgpt sdk

 geohash附近的人

###  Health Score

27

—

LowBetter than 47% of packages

Maintenance39

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community18

Small or concentrated contributor base

Maturity39

Early-stage or recently created project

 Bus Factor1

Top contributor holds 61.4% 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 ~2 days

Total

3

Last Release

677d ago

### Community

Maintainers

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

---

Top Contributors

[![orhanerday](https://avatars.githubusercontent.com/u/22305274?v=4)](https://github.com/orhanerday "orhanerday (135 commits)")[![guanhui07](https://avatars.githubusercontent.com/u/5820457?v=4)](https://github.com/guanhui07 "guanhui07 (26 commits)")[![matyhtf](https://avatars.githubusercontent.com/u/2017766?v=4)](https://github.com/matyhtf "matyhtf (10 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (10 commits)")[![orhan-cmd](https://avatars.githubusercontent.com/u/98099231?v=4)](https://github.com/orhan-cmd "orhan-cmd (8 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (5 commits)")[![bashar94](https://avatars.githubusercontent.com/u/13801112?v=4)](https://github.com/bashar94 "bashar94 (5 commits)")[![adetch](https://avatars.githubusercontent.com/u/1463387?v=4)](https://github.com/adetch "adetch (5 commits)")[![cotrufoa](https://avatars.githubusercontent.com/u/121743171?v=4)](https://github.com/cotrufoa "cotrufoa (4 commits)")[![kuafuRace](https://avatars.githubusercontent.com/u/13517412?v=4)](https://github.com/kuafuRace "kuafuRace (3 commits)")[![slaFFik](https://avatars.githubusercontent.com/u/282234?v=4)](https://github.com/slaFFik "slaFFik (1 commits)")[![ali-wells](https://avatars.githubusercontent.com/u/19995855?v=4)](https://github.com/ali-wells "ali-wells (1 commits)")[![assert6](https://avatars.githubusercontent.com/u/28561563?v=4)](https://github.com/assert6 "assert6 (1 commits)")[![dsampaolo](https://avatars.githubusercontent.com/u/1384803?v=4)](https://github.com/dsampaolo "dsampaolo (1 commits)")[![johanvanhelden](https://avatars.githubusercontent.com/u/19389981?v=4)](https://github.com/johanvanhelden "johanvanhelden (1 commits)")[![mahadsprouttech](https://avatars.githubusercontent.com/u/56071270?v=4)](https://github.com/mahadsprouttech "mahadsprouttech (1 commits)")[![marcosegato](https://avatars.githubusercontent.com/u/6240628?v=4)](https://github.com/marcosegato "marcosegato (1 commits)")[![Muchwat](https://avatars.githubusercontent.com/u/13133862?v=4)](https://github.com/Muchwat "Muchwat (1 commits)")[![mydnic](https://avatars.githubusercontent.com/u/2733767?v=4)](https://github.com/mydnic "mydnic (1 commits)")

---

Tags

chatgptchatgpt-apiopenaiswooleOpen AI

### Embed Badge

![Health badge](/badges/guanhui07-open-ai/health.svg)

```
[![Health](https://phpackages.com/badges/guanhui07-open-ai/health.svg)](https://phpackages.com/packages/guanhui07-open-ai)
```

###  Alternatives

[orhanerday/open-ai

OpenAI GPT-3 Api Client in PHP

2.4k4.8M41](/packages/orhanerday-open-ai)[daodao97/apidog

A swagger library for Hyperf.

14940.9k1](/packages/daodao97-apidog)[hyperf/swagger

A swagger library for Hyperf.

20371.1k7](/packages/hyperf-swagger)[swoole-inc/open-ai

OpenAI API Client in PHP

101.1k](/packages/swoole-inc-open-ai)[hyperf/graphql

A GraphQL component for hyperf.

151.5k](/packages/hyperf-graphql)

PHPackages © 2026

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