positus / positus-api-php-client
Positus API 客户端库
0.1.0
2021-07-13 19:08 UTC
Requires
- php: ^7.3|^8.0
- guzzlehttp/guzzle: ^7.0.1
README
- PHP 7.3+
安装
使用 Composer 安装
composer require positus/positus-api-php-client
如果您使用 Laravel,我们提供了一个 包装器。
实例化
use Positus\Client; $client = new Client();
认证
如果您没有认证令牌,请点击此处 生成一个。
$client = new Client(); $client->setToken('you-api-token');
发送消息
要能够发送消息,第一步是指定原始 ID 号
$number = $client->number('your-number-id');
然后发送消息并等待响应。
如果您想测试沙盒,可以在调用 number 方法时传递第二个参数作为 true
。
$number = $client->number('sandbox-number-id', true);
如果您想使用自己的数据发送消息,可以使用
$response = $number->sendData([ 'to' => '+5511999999999', 'type' => 'text', 'text' => [ 'body' => 'Hi!' ] ]);
要检查每种类型消息可以发送的所有数据,请查看 WhatsApp Business 文档。
如果您愿意,我们为每种类型的消息提供了准备好的方法。
文本
$response = $number->sendText('+5511999999999', 'Your message');
模板
$response = $number->sendTemplate('+5511999999999', 'namespace', 'name', 'languageCode', [ "type" => "body", "parameters" => [ [ "type" => "text", "text" => "Param 1" ], ... ] ]);
请查看 WhatsApp Business 文档 中有关模板的相关文档。
联系人
$response = $number->sendContacts('+5511999999999', [ [ 'name' => [ "formatted_name" => "John Doe" ], 'phones' => [ 'phone' => '+5511888888888', 'type' => 'CELL' ] ] ]);
请查看 WhatsApp Business 文档 中有关联系人的相关文档。
位置
$response = $number->sendLocation('+5511999999999', '-23.553885', '-46.662819', 'Robbu - Atendimento digital inteligente', 'Av. Angélica, 2530 - Bela Vista, São Paulo - SP, 01228-200');
图片
$response = $number->sendImage('+5511999999999', 'https://example.com/image.jpg', 'Random Image');
文档
$response = $number->sendDocument('+5511999999999', 'https://example.com/document.pdf', 'Random Document');
视频
$response = $number->sendVideo('+5511999999999', 'https://example.com/video.mp4', 'Random Video');
音频
$response = $number->sendAudio('+5511999999999', 'https://example.com/audio.mp3');
接收媒体
要下载媒体,请使用以下方法
$response = $number->getMedia('media-id');
响应
在每次调用之后,您可以使用以下方法检查一切是否正常并接收数据
if ($response->success()) { echo 'Message with Id ' . $response->json()->messages[0]->id . ' sent successfully'; }
如果一切正常,您将收到如下回答
{ "messages": [ { "id": "gBEGVUOWQWWQAgnFOaNl67sTDIE" } ], "message": "The message was successfully sent" }
如果出现问题,您将收到详细错误信息的消息
{ "errors": [ { "code": 1008, "title": "Required parameter is missing", "details": "Parameter 'body' is mandatory for type 'text'" } ], "message": "Unfortunately we were not able to send your message" }
请查看 api 可能返回的所有可能错误 WhatsApp Business 文档。
如果您有任何问题或发现任何问题,请随时创建 pull request 或在 Positus Studio 中打开 支持工单。
您可以检查是否失败
if ($response->error()) { echo 'Something went wrong'; }
如果您只需要状态代码
$response->status();
您可以从 api 中获取作为字符串的回答
$response->body();
或者如果您愿意,以 JSON 格式
$response->json();
或者如果您愿意,以对象格式
$response->object();
您可以获取一个头信息
$response->header('Content-Type');
或者您可以获取所有头信息
$response->headers();