PHPackages                             kaiheila/api - 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. kaiheila/api

ActiveLibrary[API Development](/categories/api)

kaiheila/api
============

the api of kaiheila

25744↓100%9PHP

Since Mar 31Pushed 1y ago3 watchersCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

Bot
===

[](#bot)

本代码是[kaiheila](http://developer.kaiheila.cn/doc)机器人的php示例sdk， 用户可以直接使用该代码或者参照该代码来构建自己的机器人。

本代码既可以当成一个composer包，来供系统调用。也可以当成一个独立的机器人来运行。在本代码中有一个重要的概念叫session。当websocket/webhook在线时，我们认为它和服务器保持了一个session,然后我们可以通过session来处理数据了。

代码说明
----

[](#代码说明)

该包依赖了swoole，请确定你安装了[swoole扩展](https://wiki.swoole.com/#/environment?id=%e5%ae%89%e8%a3%85swoole)。

### docker

[](#docker)

如果你觉得安装php相关环境比较麻烦，我们也提供了docker来方便你的使用[php-bot](https://hub.docker.com/r/kaiheila/php-bot):

```
docker pull kaiheila/php-bot
```

### 代码使用

[](#代码使用)

```
//$session = new WebsocketSession();
$session = new WebhookSession();

// 注册接收frame事件回调，当session收到了正确的frame数据时，就会调用此方法
$session->on($session::EVENT_RECEIVE_FRAME, function($frame){

});

// 事件名默认为 channel_type + _ + type组成， 如下代表侦听群聊的文字消息
$session->on("GROUP_1", function($frame){

});

// 事件名支持通配符匹配，如下代表侦听群聊的所有消息
$session->on("GROUP*", function($frame){

});

// 事件默认是采用了swoole的协程进行异步回调，如果需要同步回调，如下所示
$session->on("GROUP*", function(){}, ['async' => false]);

// 通过webhook/websocket收到消息后，把数据传给session处理即可，session就会自动按上面注册的事件进行处理。
$session->receiveData($data);
```

在回调中，我们通常会跟据服务端返回的消息，来做一些动作，我们统一封装了ApiClient:

```
$client = new \kaiheila\api\helpers\ApiHelper($path);
// post json示例
$client->setBody(["foo" => "bar"])->send(\kaiheila\api\helpers\ApiHelper::POST);
// get示例
$client->setQuery(["foo" => "bar"])->send(\kaiheila\api\helpers\ApiHelper::GET);

```

kaiheila/api 作为composer集成至其它服务内
-------------------------------

[](#kaiheilaapi-作为composer集成至其它服务内)

```
composer require  kaiheila/api dev-main

```

参数上文或example, 直接使用`session->receiveData($data)`来处理数据即可。

**注意：** 在php-fpm中，正常是不支持async-io的，可能会报`async-io must be used in PHP CLI mode`的错误，因此在php-fpm模式下时，需要同步处理事件，示例如下：

```
$session->on($eventName, function(){},  ['async' => false]);
```

独立机器人
-----

[](#独立机器人)

本代码也可以作为一个独立的机器人来运行。

1. git clone :kaiheila/php-bot.git
2. 进入代码目录`cd php-bot`，运行`composer update`
3. 打开[开发者中心](https://developer.kaiheila.cn/bot), 创建机器人，并更改为webhook/websocket模式。
4. 更改配置，将开发者中心的配置填入config.php文件中。

```
cp examples/config.php.sample examples/config.php
# 按照参数说明，修改config.php的配置, vim examples/config.php
# 运行webhook机器人
php examples/webhook.php
# 运行websocket机器人
```

4. 在开发者后台，把机器人的地址填入后台。

在做好上述配置后，你也可以试试我们做的一个小机器人【强尼机器人】, 注意它是webhook模式，需要配置ip及端口。

```
php examples/cyberpunk.php
```

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance35

Infrequent updates — may be unmaintained

Popularity29

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity15

Early-stage or recently created project

 Bus Factor1

Top contributor holds 71.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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/0dee5677f7f0c4bf4e666c9aec16068a4ab6362c7f25574566463e11d78e93c4?d=identicon)[kaiheila](/maintainers/kaiheila)

---

Top Contributors

[![buptlsp](https://avatars.githubusercontent.com/u/3291512?v=4)](https://github.com/buptlsp "buptlsp (10 commits)")[![etby](https://avatars.githubusercontent.com/u/4692794?v=4)](https://github.com/etby "etby (1 commits)")[![fi6](https://avatars.githubusercontent.com/u/45503345?v=4)](https://github.com/fi6 "fi6 (1 commits)")[![Mundanity-fc](https://avatars.githubusercontent.com/u/17294992?v=4)](https://github.com/Mundanity-fc "Mundanity-fc (1 commits)")[![q1an1x](https://avatars.githubusercontent.com/u/19363684?v=4)](https://github.com/q1an1x "q1an1x (1 commits)")

---

Tags

botkaiheilaphpsdk

### Embed Badge

![Health badge](/badges/kaiheila-api/health.svg)

```
[![Health](https://phpackages.com/badges/kaiheila-api/health.svg)](https://phpackages.com/packages/kaiheila-api)
```

###  Alternatives

[stripe/stripe-php

Stripe PHP Library

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

A PHP wrapper for Twilio's API

1.6k92.9M270](/packages/twilio-sdk)[knplabs/github-api

GitHub API v3 client

2.2k15.8M187](/packages/knplabs-github-api)[facebook/php-business-sdk

PHP SDK for Facebook Business

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

PHP wrapper for the Meilisearch API

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

Google API Core for PHP

263103.1M452](/packages/google-gax)

PHPackages © 2026

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