hexxondiv/whatsapp

轻量级的PHP库,用于使用ultramsg.com API通过PHP发送WhatsApp消息

2.0.6 2022-03-31 10:25 UTC

This package is auto-updated.

Last update: 2024-09-29 05:50:09 UTC


README

轻量级PHP库,用于通过PHP发送WhatsApp消息,由hexxondiv提供

安装

只需下载ultramsg.class.php或使用Composer

composer require hexxondiv/whatsapp-php-sdk

示例用法

<?php
require_once ('vendor/autoload.php'); // if you use Composer
//require_once('ultramsg.class.php'); // if you download ultramsg.class.php

$ultramsg_token="tof7lsdJasdloaa57e"; // Ultramsg.com token
$instance_id="instance1150"; // Ultramsg.com instance id
$client = new Hexxondiv\WhatsAppApi($ultramsg_token,$instance_id);

$to="put_your_mobile_number_here"; 
$body="Hello world"; 
$referenceId="WA12348765";
$api=$client->sendChatMessage($to,$body,$referenceId);
print_r($api);

注意:如果您没有ultramsg.com账户,需要在此处创建一个账户,并使用您的instance_id和token替换此处的账户信息。

$referenceId是可选的。即使没有它,系统也会返回一个消息ID。

YouTube

Send Message by WhatsApp api using PHP SDK | Ultramsg PHP SDK

发送消息

  • $to : 测试时使用的您的号码(使用国际格式,例如+14155552671)或联系人或群组的chatID,例如 14155552671@c.us14155552671-441234567890@g.us
  • $body : 消息文本,UTF-8或UTF-16字符串,包含emoji。
  • $priority : 此参数是可选的,

您可以使用它为消息创建专业队列,优先级值较低的邮件将首先发送。

使用示例

priority = 0:用于高优先级消息,如OTP消息。

priority = 5:用于普通消息。

priority =10:非紧急促销活动和通知给您的客户。

默认值 : 10

$to="put_your_mobile_number_here"; 
$body="Hello world";
$priority=10;
$referenceId="WA-1232948374";
$api=$client->sendChatMessage($to,$body,$priority,$referenceId);
print_r($api);

发送图片

  • $caption : 图片标题,UTF-8或UTF-16字符串,包含emoji。
  • $image : HTTP链接图片或包含mime数据的base64编码文件

支持的扩展名(jpg, jpeg, gif, png, svg, webp, bmp)。

最大文件大小:64MB。

示例图片链接 :

$to="put_your_mobile_number_here"; 
$caption="image Caption"; 
$image="https://file-example.s3-accelerate.amazonaws.com/images/test.jpg"; 
$referenceId="WA-1232948374";
$api=$client->sendImageMessage($to,$image,$caption,$referenceId);
print_r($api);

发送文档

  • $filename : 文件名,例如1.jpg或Hello.pdf
  • $document : HTTP链接文件或包含mime数据的base64编码文件

支持大多数扩展名,如(zip, xlsx, csv, txt, pptx, docx等)。

最大文件大小:64MB。

$to="put_your_mobile_number_here"; 
$filename="image Caption"; 
$document="https://file-example.s3-accelerate.amazonaws.com/documents/cv.pdf"; 
$referenceId="34564563563";
$caption="Document Caption";
$api=$client->sendDocumentMessage($to,$filename,$document,$caption,$referenceId);
print_r($api);

发送音频

  • $audio : HTTP链接音频或包含mime数据的base64编码音频

支持的扩展名(mp3, aac, ogg)。

最大文件大小:64MB。

$to="put_your_mobile_number_here"; 
$audio="https://file-example.s3-accelerate.amazonaws.com/audio/2.mp3"; 
$caption="Document Caption";
$api=$client->sendAudioMessage($to,$audio,$caption);
print_r($api);

发送语音

  • $audio : ogg格式的HTTP链接音频文件或opus编解码器的base64编码ogg文件
$to="put_your_mobile_number_here"; 
$audio="https://file-example.s3-accelerate.amazonaws.com/voice/oog_example.ogg"; 
$api=$client->sendVoiceMessage($to,$audio);
print_r($api);

发送视频

  • $video : HTTP链接视频或带有MIME数据的base64编码视频

支持的扩展名(mp4, 3gp, mov)。

最大文件大小:64MB。

$to="put_your_mobile_number_here"; 
$video="https://file-example.s3-accelerate.amazonaws.com/video/test.mp4"; 
$caption="Video Caption"
$api=$client->sendVideoMessage($to,$video,$caption);
print_r($api);

发送链接

  • $link : HTTP或HTTPS链接
$to="put_your_mobile_number_here"; 
$link="https://ultramsg.com"; 
$caption="link Caption"
$api=$client->sendLinkMessage($to,$link,$caption);
print_r($api);

发送联系人

  • $contact : 联系人ID或联系人ID数组示例

示例

14000000001@c.us

14000000001@c.us,14000000002@c.us,14000000003@c.us

最大长度:300个字符,大约15个联系人

$to="put_your_mobile_number_here"; 
$contact="14000000001@c.us"; 
$api=$client->sendContactMessage($to,$contact);
print_r($api);

发送位置

  • $address : 位置下的文本。

支持两行。要使用两行,请使用\n符号。

最大长度:300个字符。

  • $lat : 纬度
  • $lng : 经度
$to="put_your_mobile_number_here"; 
$address="ABC company \n Sixth floor , office 38"; 
$lat="25.197197"; 
$lng="55.2721877"; 
$api=$client->sendLocationMessage($to,$address,$lat,$lng);
print_r($api);

发送vcard

  • $vcard : vcard 3.0文本值

最大长度:4096个字符

$to="put_your_mobile_number_here"; 
$vcard="BEGIN:VCARD
VERSION:3.0
N:lastname;firstname
FN:firstname lastname
TEL;TYPE=CELL;waid=14000000001:14000000002
NICKNAME:nickname
BDAY:01.01.1987
X-GENDER:M
NOTE:note
ADR;TYPE=home
ADR;TYPE=work
END:VCARD";
$vcard = preg_replace("/[\n\r]/", "\n", $vcard);
$api=$client->sendVcardMessage($to,$vcard);
print_r($api);

获取消息

获取通过API发送的消息

  • $page : 分页页码
  • $limit : 每个请求的消息数量。最大值:100。
  • $status : 消息状态 [已发送, 队列, 未发送]
    • 已发送:获取已发送的消息。
    • 队列:获取队列消息。
    • 未发送:获取未发送的消息。
    • 所有:获取所有消息。
  • $sort :
    • asc:按ID从小到大排序消息。
    • desc:按ID从大到小排序消息。
$page=1;
$limit=100;
$status="all";
$sort="asc";
$api=$client->getMessages($page,$limit,$status,$sort);
print_r($api);

获取消息统计信息

$api=$client->getMessageStatistics();
print_r($api);

获取实例状态

$api=$client->getInstanceStatus();
print_r($api);

获取实例QR图像

header('Content-Type: image/png');
$api=$client->getInstanceQr();
print_r($api);

获取实例QR码

$api=$client->getInstanceQrCode();
print_r($api);

获取实例截图

header('Content-Type: image/png');
$api=$client->getInstanceScreenshot();
print_r($api);

或base64

$api=$client->getInstanceScreenshot("base64");
print_r($api);

获取实例信息

获取连接的电话信息:号码,名称,图片等。

$api=$client->getInstanceMe();
print_r($api);

获取实例设置

sendDelay:发送消息之间的延迟(秒),默认1秒

webhook_url:接收通知的Http或https URL。

webhook_message_ack:在webhooks中开启/关闭(消息送达和消息查看)通知。

webhook_message_received:在webhooks中接收消息时开启/关闭通知。

webhook_message_create:在webhooks中创建消息时开启/关闭通知。

$api=$client->getInstanceSettings();
print_r($api);

实例接管

如果设备已连接到Web WhatsApp的另一个实例,则返回活动会话。

$api=$client->sendInstanceTakeover();
print_r($api);

实例注销

从WhatsApp Web注销以获取新的QR码。

$api=$client->sendInstanceLogout();
print_r($api);

实例重启

重启您的实例。

$api=$client->sendInstanceRestart();
print_r($api);

实例设置更新

  • sendDelay:发送消息之间的延迟(秒)。

  • webhook_url:接收通知的Http或https URL。

  • webhook_message_received:在webhooks中接收消息时开启/关闭通知。

  • webhook_message_create:在webhooks中创建消息时开启/关闭通知。

  • webhook_message_ack:在webhooks中开启/关闭(消息送达和消息查看)通知。

$sendDelay=1;
$webhook_url="";
$webhook_message_received=false;
$webhook_message_create=false;
$webhook_message_ack=false;

$api=$client->sendInstanceSettings($sendDelay,$webhook_url,$webhook_message_received,$webhook_message_create,$webhook_message_ack);
print_r($api);

支持

使用问题与我联系

请注意,这仅是ultramsg.com原始库的一个分支。

我只包含了referenceId部分,因为我需要用它来区分我服务的多个业务实例。