waapi/waapi-php-sdk
Composer 安装命令:
composer require waapi/waapi-php-sdk
包简介
The official EAZE WhatsApp PHP SDK
README 文档
README
Introduction
PHP package to easily interact with waapi.app.
Installation
Composer
composer require waapi/waapi-php-sdk
Usage
Initial Setup
use WaAPI\WaAPISdk\WaAPISdk; $apiToken = 'xxxxxxxxxxxxxxxxxxxxxx'; $sdk = new WaAPISdk($apiToken);
Check if WhatsApp API is available
$isAvailable = $sdk->isApiAvailable();
Create a new instance
// Create a basic instance $instance = $sdk->createInstance(); $instanceId = $instance->id; // Create an instance with a name and webhook $instance = $sdk->createInstance( name: 'My Instance', webhookUrl: 'https://my.url.com/webhook/handler', webhookEvents: ['message', 'ready', 'qr'] );
Get an existing instance
$instanceId = 10; //you need to know your instance id at this point $instance = $sdk->getInstance($instanceId);
Update an existing instance
$instanceId = 10; //you need to know your instance id at this point //if a subscribed event occurs, this url will be requested with the event data //can also be null if you do not want to receive webhooks $webhookUrl = ''; $subscribedEvents = ['', '']; //can also be null or an empty array $sdk->updateInstance($instanceId, $webhookUrl, $subscribedEvents); // You can also update the instance name $sdk->updateInstance($instanceId, name: 'New Name'); //if you have an instance object, you can also use the following method $instance = $sdk->getInstance($instanceId); $instance->update($webhookUrl, $subscribedEvents);
Delete an existing instance
$instanceId = 10; //you need to know your instance id at this point $sdk->deleteInstance($instanceId); //if you have an instance object, you can also use the following method to delete this instance $instance = $sdk->getInstance($instanceId); $instance->delete();
Get the QR-Code
After creating a new instance, you need to connect your WhatsApp phone number with this instance. With the following code, you are able to receive the current QR-Code.
$instanceId = 10; //you need to know your instance id at this point $response = $sdk->getInstanceClientQrCode($instanceId); //if you have an instance object, you can also use the convenience method $instance = $sdk->getInstance($instanceId); $response = $instance->clientQrCode(); // The QR code as a base64 image string (data:image/png;base64,…). // Set this string in the src attribute of a <img src=””> html tag. $qrCode = $response->qrCode;
Get the instance status
An instance always has a status.
| Status | Description |
|---|---|
| booting | The node is starting the instance. The status will change to loading_screen soon. |
| loading_screen | The instance is starting WhatsApp Web. The status will change to qr or ready soon. |
| qr | The instance needs to be connected with a WhatsApp phone number. You can fetch the QR code now. |
| authenticated | The connection via the QR code was successful. The status will change to ready soon. |
| auth_failure | The connection via the QR code failed or WhatsApp Web is down. |
| ready | The instance is ready to use. You are able to execute actions and send messages. |
| disconnected | Disconnected from WhatsApp Web |
$instanceId = 10; //you need to know your instance id at this point $response = $sdk->getInstanceClientStatus($instanceId); //if you have an instance object, you can also use the convenience method $instance = $sdk->getInstance($instanceId); $response = $instance->clientStatus(); $instanceStatus = $response->instanceStatus;
Get information about an existing instance
$instanceId = 10; //you need to know your instance id at this point $response = $sdk->getInstanceClientInfo($instanceId); //if you have an instance object, you can also use the convenience method $instance = $sdk->getInstance($instanceId); $response = $instance->clientInfo(); //your public name of your WhatsApp profile (your name) $displayName = $response->displayName; //the connected WhatsApp phone number (your phone number) $phoneNumber = $response->formattedNumber; //profile image url of the connected WhatsApp phone number (your profile picture) $profileUrl = $response->profilePicUrl; //a unique identifier for your WhatsApp account / profile / phone number $whatsAppId = $response->contactId;
Execute actions
Once your instance is in ready state, you can execute WhatsApp actions:
$instanceId = 10; // Send a text message $result = $sdk->executeInstanceAction($instanceId, 'send-message', [ 'chatId' => '491234567890@c.us', 'message' => 'Hello from waapi!', ]); // Send media $result = $sdk->executeInstanceAction($instanceId, 'send-media', [ 'chatId' => '491234567890@c.us', 'mediaUrl' => 'https://example.com/image.png', 'mediaCaption' => 'Check this out!', ]); // Using the instance object $instance = $sdk->getInstance($instanceId); $result = $instance->executeClientAction('send-message', [ 'chatId' => '491234567890@c.us', 'message' => 'Hello!', ]);
Common actions: send-message, send-media, send-location, send-vcard, get-chats, get-contacts, get-contact-by-id, is-registered-user, get-number-id, send-seen, logout, reboot.
For a full list of available actions and their parameters, see the API documentation.
Webhook events
When configuring webhooks, use the WebhookEvent class for available event constants:
use WaAPI\WaAPISdk\Resources\WebhookEvent; $sdk->createInstance( name: 'My Instance', webhookUrl: 'https://my.url.com/webhook', webhookEvents: [ WebhookEvent::MESSAGE, WebhookEvent::READY, WebhookEvent::QR, WebhookEvent::DISCONNECTED, ] ); // Subscribe to all events $sdk->createInstance( name: 'My Instance', webhookUrl: 'https://my.url.com/webhook', webhookEvents: WebhookEvent::ALL );
| Event | Description |
|---|---|
message |
Fired when you receive a message |
message_create |
New message created (sent and received) |
message_edit |
A message was edited |
message_ack |
Message was read or received |
message_reaction |
Reaction sent, received, updated or removed |
message_revoke_everyone |
Message deleted for everyone |
message_revoke_me |
Message deleted for you only |
media_uploaded |
Media file uploaded successfully |
qr |
New QR code available |
authenticated |
Connection successfully established |
auth_failure |
Authentication failed |
ready |
Instance is ready to use |
disconnected |
Instance was disconnected |
loading_screen |
Instance is loading data |
change_state |
Connection status changed |
group_join |
Someone joined or was added to a group |
group_leave |
Someone left or was removed from a group |
group_update |
Group information was updated |
call |
Incoming call (cannot be handled by API) |
vote_update |
Poll vote was updated or removed |
License
The MIT License (MIT). Please see License File for more information.
waapi/waapi-php-sdk 适用场景与选型建议
waapi/waapi-php-sdk 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11.26k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2023 年 08 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「library」 「sdk」 「whatsapp」 「eaze」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 waapi/waapi-php-sdk 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 waapi/waapi-php-sdk 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 waapi/waapi-php-sdk 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PSR-7 compatible library for making CRUD API endpoints
Inbox pattern process implementation for your Laravel Applications
Core library that defines common interfaces used by the rest of the intahwebz..
Amqp classes
Provides command to manage a web library directory
CakePHP Bitly Plugin
统计信息
- 总下载量: 11.26k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 8
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-08-21