ultramsg / whatsapp-php-sdk
ultramsg.com提供的轻量级PHP库,用于通过PHP发送WhatsApp消息
README
ultramsg.com提供的轻量级PHP库,用于通过PHP发送WhatsApp消息
安装
只需下载ultramsg.class.php或使用Composer
composer require ultramsg/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 UltraMsg\WhatsAppApi($ultramsg_token,$instance_id); $to="put_your_mobile_number_here"; $body="Hello world"; $api=$client->sendChatMessage($to,$body); print_r($api);
注意:如果您没有ultramsg.com账户,需要替换instance_id和token,请从这里创建一个。
YouTube
发送消息
- $to : 测试用的您的号码(使用国际格式,例如+14155552671)或联系人的chatID(例如14155552671@c.us)或群组的chatID(例如14155552671-441234567890@g.us)
- $body : 消息文本,UTF-8或UTF-16字符串,支持emoji。
- $priority : 此参数为可选参数,
您可以使用它来创建消息的专业队列,优先级值较小的消息先发送。
使用示例
优先级 = 0: 用于高优先级消息,如OTP消息。
优先级 = 5: 用于一般消息。
优先级 = 10: 非紧急促销和通知给您的客户。
默认值 : 10
- $referenceId : 此消息的您自定义引用ID。
$to="put_your_mobile_number_here"; $body="Hello world"; $priority=10; $referenceId="SDK"; $api=$client->sendChatMessage($to,$body,$priority,$referenceId); print_r($api);
发送图片
- $caption : 图片标题,UTF-8或UTF-16字符串,支持emoji。
- $image : HTTP链接图片或base64编码的文件
支持的扩展名(jpg, jpeg, gif, png, svg, webp, bmp)。
最大文件大小:16MB。
最大Base64长度:10,000,000
- $nocache : 默认false
false : 使用已上传的文件,而不是每次请求时都上传
true : 每次请求都上传
$to="put_your_mobile_number_here"; $image="https://file-example.s3-accelerate.amazonaws.com/images/test.jpg"; $caption="image Caption"; $priority=10; $referenceId="SDK"; $nocache=false; $api=$client->sendImageMessage($to,$image,$caption,$priority,$referenceId,$nocache); print_r($api);
发送贴纸
- $sticker : HTTP链接图片或base64编码的文件
支持的扩展名(jpg, jpeg, gif, png, svg, webp, bmp)。
最大文件大小:16MB。
最大Base64长度:10,000,000
- $nocache : 默认false
false : 使用已上传的文件,而不是每次请求时都上传
true : 每次请求都上传
$to="put_your_mobile_number_here"; $sticker="https://file-example.s3.us-west-2.amazonaws.com/sticker/1.webp"; $priority=10; $referenceId="SDK"; $nocache=false; $api=$client->sendStickerMessage($to,$sticker,$priority,$referenceId,$nocache); print_r($api);
发送文档
- $filename : 文件名,例如1.jpg或Hello.pdf
- $caption : 文档标题,UTF-8或UTF-16字符串,支持emoji。
- $document : HTTP链接文件或base64编码的文件
支持大多数扩展名,如(zip, xlsx, csv, txt, pptx, docx等)。
最大文件大小:100MB。
最大Base64长度:10,000,000
$to="put_your_mobile_number_here"; $filename="File name"; $caption="Document Caption"; $document="https://file-example.s3-accelerate.amazonaws.com/documents/cv.pdf"; $api=$client->sendDocumentMessage($to,$filename,$document,$caption); print_r($api);
发送音频
- $audio : HTTP链接音频或base64编码的音频
支持的扩展名(mp3, aac, ogg)。
最大文件大小:16MB。
最大Base64长度:10,000,000
$to="put_your_mobile_number_here"; $audio="https://file-example.s3-accelerate.amazonaws.com/audio/2.mp3"; $api=$client->sendAudioMessage($to,$audio); print_r($api);
发送语音
- $audio : ogg文件音频链接,使用opus编解码器,或base64编码的opus编解码器ogg文件
最大文件大小:16MB。
最大Base64长度:10,000,000
$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链接视频或base64编码的视频
支持的扩展名(mp4, 3gp, mov)。
最大文件大小:16MB。
最大Base64长度:10,000,000
$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"; $api=$client->sendLinkMessage($to,$link); print_r($api);
发送联系人
- $contact : 联系人ID或联系人ID数组示例
示例
或
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);
按状态重发消息
- $status:未发送或过期
$status="expired"; $api=$client->resendByStatus($status); print_r($api);
按ID重发消息
- $id:消息ID
$id=123; $api=$client->resendById($id); print_r($api);
获取消息
获取通过API发送的消息
- $page:分页页码
- $limit:每次请求的消息数量。最大值:100。
- $status:消息状态 [已发送,队列,未发送]
- 已发送:获取已发送的消息。
- 队列:获取队列中的消息。
- 未发送:获取未发送的消息。
- 无效:获取无效消息。
- 过期:获取过期消息。
- 全部:获取所有消息。
- $sort :
- asc:按ID从小到大排序消息。
- desc:按ID从大到小排序消息。
- $id:按消息ID筛选消息。
- $referenceId:按自定义参考ID筛选消息。
- $from:按WhatsApp发送者号码筛选消息,例如 14155552671@c.us。
- $to:按接收者号码筛选消息,例如 14155552671@c.us 或 14155552671-441234567890@g.us。
- $ack:按消息确认状态筛选消息 [待处理,服务器,设备,已读,播放]。
$page=1; $limit=100; $status="all"; $sort="asc"; $id=""; $referenceId=""; $from=""; $to=""; $ack=""; $api=$client->getMessages($page,$limit,$status,$sort,$id,$referenceId,$from,$to,$ack); 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);
获取实例二维码
$api=$client->getInstanceQrCode(); print_r($api);
获取实例信息
获取连接的手机的详细信息:号码,姓名,图片等。
$api=$client->getInstanceMe(); print_r($api);
获取实例设置
sendDelay:发送消息之间的延迟(秒),默认1秒
webhook_url:接收通知的Http或https URL。
webhook_message_ack:在webhook中开启/关闭(消息已送达和消息已查看)通知。
webhook_message_received:在webhook中开启/关闭消息接收通知。
webhook_message_create:在webhook中开启/关闭消息创建通知。
webhook_message_download_media:在webhook中开启/关闭获取接收到的文档/媒体文件。
$api=$client->getInstanceSettings(); print_r($api);
实例接管
如果设备已连接到Web WhatsApp的另一个实例,则返回活动会话。
$api=$client->sendInstanceTakeover(); print_r($api);
实例登出
从WhatsApp Web登出以获取新二维码。
$api=$client->sendInstanceLogout(); print_r($api);
实例重启
重启您的实例。
$api=$client->sendInstanceRestart(); print_r($api);
实例设置更新
-
sendDelay:发送消息之间的延迟(秒)。
-
webhook_url:接收通知的Http或https URL。
-
webhook_message_received:在webhook中接收消息通知的true/false。
-
webhook_message_create:在webhook中创建消息通知的true/false。
-
webhook_message_ack:在webhook中确认(消息已送达和消息已查看)通知的true/false。
$sendDelay=1; $webhook_url=""; $webhook_message_received=false; $webhook_message_create=false; $webhook_message_ack=false; $webhook_message_download_media=false; $api=$client->sendInstanceSettings($sendDelay,$webhook_url,$webhook_message_received,$webhook_message_create,$webhook_message_ack,$webhook_message_download_media); print_r($api);
获取聊天列表
$api=$client->getChats(); print_r($api);
获取聊天对话中的最后一条消息
- $chatId:联系人或群组的chatID,例如 14155552671@c.us 或 14155552671-441234567890@g.us
- $limit:每次请求的消息数量。
最大值:1000。
$chatId="14155552671@c.us"; $limit=100; $api=$client->getChatsMessages($chatId,$limit); print_r($api);
获取联系人列表
$api=$client->getContacts(); print_r($api);
获取联系人信息
- $chatId:联系人的chatID,例如 14155552671@c.us
$chatId="14155552671@c.us"; $api=$client->getContact($chatId); print_r($api);
获取所有被屏蔽的联系人
$api=$client->getBlockedContacts(); print_r($api);
在WhatsApp上屏蔽联系人
- $chatId:联系人的chatID,例如 14155552671@c.us
$chatId="14155552671@c.us"; $api=$client->blockContact($chatId); print_r($api);
从WhatsApp上解除联系人的屏蔽
- $chatId:联系人的chatID,例如 14155552671@c.us
$chatId="14155552671@c.us"; $api=$client->unblockContact($chatId); print_r($api);
检查号码是否为WhatsApp用户
- $chatId:联系人的chatID,例如 14155552671@c.us
$chatId="14155552671@c.us"; $api=$client->checkContact($chatId); print_r($api);
支持
使用 问题 联系我