spontena/pb-php
Composer 安装命令:
composer require spontena/pb-php
包简介
Pandorabots API client for PHP
关键字:
README 文档
README
Pandorabots API client for PHP.
See the official Pandorabots API documentation: https://www.pandorabots.com/docs/api-endpoints/
Requirements
- PHP 8.1 or newer
- ext-json
- Guzzle 7.5+
Installation
composer require spontena/pb-php
Quickstart
use Spontena\PbPhp\PBClient; use Spontena\PbPhp\FileKind; use Spontena\PbPhp\Exception\ApiException; $pb = new PBClient( host: 'https://api.pandorabots.com', appId: getenv('PB_APP_ID'), userKey: getenv('PB_USER_KEY'), ); try { $pb->create('mybot'); $pb->upload(__DIR__ . '/aiml/greetings.aiml', 'mybot'); $pb->compile('mybot'); $reply = $pb->talk('hello', 'mybot'); foreach ($reply->responses as $line) { echo $line, "\n"; } } catch (ApiException $e) { fprintf(STDERR, "Pandorabots API error %d: %s\n", $e->getStatusCode(), $e->getResponseBody()); }
A working end-to-end script lives in examples/quickstart.php. Run it after setting PB_APP_ID and PB_USER_KEY:
PB_APP_ID=xxx PB_USER_KEY=yyy php examples/quickstart.php
API reference
| Method | HTTP | Endpoint |
|---|---|---|
getBotsList() |
GET |
/bot/{appId} |
create($botname) |
PUT |
/bot/{appId}/{botname} |
delete($botname) |
DELETE |
/bot/{appId}/{botname} |
getBotFiles($botname) |
GET |
/bot/{appId}/{botname} |
getBotFile(FileKind $kind, $botname, ?$name) |
GET |
/bot/{appId}/{botname}/{kind}[/{name}] (since v2.1.0) |
upload($path, $botname) |
PUT |
/bot/{appId}/{botname}/{kind}[/{name}] |
deleteBotFile($name, FileKind $kind, $botname) |
DELETE |
/bot/{appId}/{botname}/{kind}[/{name}] |
compile($botname) |
GET |
/bot/{appId}/{botname}/verify |
talk($input, $botname, ...) |
POST |
/talk/{appId}/{botname} |
debug($input, $botname, ...) |
POST |
/talk/{appId}/{botname} (with trace etc.) |
atalk($input, ...) |
POST |
/talk?botkey=... (botkey auth, bot identified by key) |
Successful responses are returned as stdClass (the decoded JSON body). getBotsList() returns a list<\stdClass> because the API responds with a top-level JSON array. getBotFile() returns the raw response body as a string (no JSON decoding) — useful for downloading AIML/set/map content. HTTP 4xx/5xx responses raise Spontena\PbPhp\Exception\ApiException; bad inputs raise InvalidArgumentException or InvalidFileException.
upload() infers the file kind from the extension:
| Extension | FileKind |
URL form |
|---|---|---|
.aiml |
File |
/bot/.../file/{name} |
.set |
Set |
/bot/.../set/{name} |
.map |
Map |
/bot/.../map/{name} |
.substitution |
Substitution |
/bot/.../substitution/{name} |
.pdefaults |
Pdefaults |
/bot/.../pdefaults |
.properties |
Properties |
/bot/.../properties |
Migration from v1
v2 is a breaking-change release. Notable differences:
- Namespace is now
Spontena\PbPhp(wasspontena\pbphp); autoloading switched from PSR-0 to PSR-4. - Constructor accepts named arguments and an optional
botKeyforatalk(). - HTTP errors throw
ApiExceptioninstead of returning{ status: "error", ... }. deleteBotFile()now takes aFileKindenum instead of a string.botnameand filenames are URL-encoded.debug()'s brokenclient_nameparameter (the long-standing$clientnametypo) is fixed.- Requires PHP 8.1+ and Guzzle 7.5+.
Testing
Unit tests (no API access)
composer install composer test # PHPUnit unit suite (mocked HTTP, runs by default) composer analyse # PHPStan (level 6)
CI runs the unit suite on PHP 8.1 / 8.2 / 8.3 / 8.4 — see .github/workflows/ci.yml.
Integration tests (real Pandorabots API)
The tests/Integration/ suite hits the live API to validate that endpoint URLs, request formats, and response shapes still match. It is not run by default. Provide credentials via env vars and invoke the integration suite explicitly:
PB_APP_ID=xxx PB_USER_KEY=yyy vendor/bin/phpunit --testsuite integration
Test bots are created with names like pbphpci<hint><random> (lowercase alphanumeric only — Pandorabots rejects hyphens) and removed in tearDown() even if a test fails.
To exercise atalk(), additionally set PB_BOT_KEY for an existing, compiled bot whose bot key has been issued in the Pandorabots dashboard:
PB_BOT_KEY=zzz \ PB_APP_ID=xxx PB_USER_KEY=yyy \ vendor/bin/phpunit --testsuite integration
The bot is identified by the bot key alone — no botname is needed in the request path.
| Env var | Purpose |
|---|---|
PB_APP_ID |
Pandorabots app id (required) |
PB_USER_KEY |
Pandorabots user key (required) |
PB_HOST |
API host, defaults to https://api.pandorabots.com |
PB_BOT_KEY |
Bot key for atalk() integration test (optional) |
License
MIT — see LICENSE.txt.
spontena/pb-php 适用场景与选型建议
spontena/pb-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 310 次下载、GitHub Stars 达 6, 最近一次更新时间为 2014 年 11 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「pandorabots」 「aiml」 「spontena」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 spontena/pb-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 spontena/pb-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 spontena/pb-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
An AIML chatbot and admin portal written for the Laravel framework
Alfabank REST API integration
Converts AIML to CSV and CSV to AIML.
Zero-dependency raw PHP DNS resolver, domain-ownership verification, and intoDNS/MxToolbox-style diagnostics. Queries authoritative nameservers directly over sockets — never trusts the recursive cache for ownership checks.
An AIML tag to perform a google search in the Lemur Engine Chatbot
CLI tool to sync local AIML projects with Pandorabots
统计信息
- 总下载量: 310
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 25
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-11-13