PHP code example of onetech / whatsapp-360-dialog-sdk
1. Go to this page and download the library: Download onetech/whatsapp-360-dialog-sdk library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
onetech / whatsapp-360-dialog-sdk example snippets
use Dialog360\Dialog360Client;
use Dialog360\Message\TextMessage;
use Dialog360\EnvironmentLoader;
// 加载环境变量
EnvironmentLoader::load();
// 初始化客户端
$client = new Dialog360Client(
EnvironmentLoader::get('DIALOG360_API_KEY'),
EnvironmentLoader::get('DIALOG360_PHONE_NUMBER_ID')
);
// 发送文本消息
$message = new TextMessage('1234567890', 'Hello from 360 Dialog!');
$response = $client->sendMessage($message);
// 检查响应
if ($response->isSuccess()) {
echo "消息发送成功!";
// Cloud API 可查询健康状态(替代 v1 的消息状态查询)
$health = $client->getHealthStatus();
echo "健康状态: " . ($health['health_status']['can_send_message'] ?? 'UNKNOWN');
} else {
echo "发送失败: " . $response->getErrorMessage();
}
// 消息:发送各类消息(文本/媒体/模板/交互/联系人/位置/表情回应/贴纸)
$response = $client->messages()->send($message);
// 媒体:上传 / 查询 / 下载 / 删除
$mediaId = $client->media()->upload('/path/to/file.jpg', 'image/jpeg');
$mediaInfo = $client->media()->getInfo($mediaId);
$content = $client->media()->download($mediaId, '/tmp/image.jpg'); // 可选保存到本地
$client->media()->delete($mediaId);
// 媒体:分块续传上传(Resumable Upload,适合大文件/弱网,返回 handle)
$handle = $client->media()->uploadResumable('/path/to/large.mp4', 'video/mp4');
$session = $client->media()->createUploadSession('large.mp4', filesize('/path/to/large.mp4'), 'video/mp4');
$status = $client->media()->getUploadSessionStatus($session->getSessionId()); // 断点续传:查已收偏移
$client->media()->uploadChunk($session->getSessionId(), $chunk, $status->getFileOffset()); // 逐块上传
// Webhook:电话号码级
$client->webhook()->setUrl('https://example.com/webhook');
$webhook = $client->webhook()->getUrl();
// Webhook:WABA 级
$client->webhook()->setWabaUrl('https://example.com/webhook', headers: [], overrideAll: false);
$config = $client->webhook()->getWabaUrl();
// 模板:查询模板列表
$templates = $client->templates()->list(); // 旧端点 /v1/configs/templates(已废弃)
// 模板:完整生命周期(/message_templates)
$list = $client->templates()->listMessageTemplates(limit: 25);
$library = $client->templates()->getTemplateLibrary();
$created = $client->templates()->create('promo', 'en_US', 'MARKETING', [['type' => 'BODY', 'text' => 'Hi {{1}}']]);
$template = $client->templates()->get($created->getTemplateId());
$client->templates()->update($created->getTemplateId(), ['category' => 'UTILITY']);
$client->templates()->deleteByName('promo');
$client->templates()->archive([$created->getTemplateId()]);
// 健康状态
$health = $client->health()->status();
// Conversational Components:会话自动化(欢迎消息 / 命令 / 提示语,/conversational_automation)
$automation = $client->conversationalComponents()->get();
$response = $client->conversationalComponents()->configure(
[ConversationalComponentsApi::command('support', '联系人工客服')], // commands
true, // enable_welcome_message
['您好!请问需要什么帮助?'] // prompts
);
// 拉黑用户:查询 / 拉黑 / 解除拉黑(/block_users)
$list = $client->blockUsers()->list(limit: 10);
$response = $client->blockUsers()->block(['+1234567890']);
$response = $client->blockUsers()->unblock(['+1234567890']);
// 主页资料:查询 / 更新 WhatsApp Business Profile(/whatsapp_business_profile)
$profile = $client->profile()->get(['about', 'email']);
$response = $client->profile()->update(['about' => 'Hi!', 'vertical' => 'RETAIL']);
// 群组:建群 / 管理 / 成员与加群请求(/groups)
$groupId = $client->groups()->create('Group subject')->getGroupId();
$list = $client->groups()->list(limit: 25);
$client->groups()->update($groupId, ['subject' => 'New subject']);
$client->groups()->removeParticipants($groupId, ['+1234567890']); // 单次最多 8 人
$client->groups()->approveJoinRequests($groupId, ['join-request-id']);
$client->groups()->delete($groupId);
// 营销消息:发送 / 数据集 / 模板 / 触达估算 / 分析(/marketing_messages 与 /marketing/*)
$response = $client->marketing()->send('+1234567890', ['name' => 'promo', 'language' => 'en']);
$datasetId = $client->marketing()->getDataset()->getDatasetId();
$estimate = $client->marketing()->getReachEstimate(['geo_locations' => ['countries' => ['BR']]], 'L7D');
use Dialog360\Dialog360Client;
use Dialog360\EnvironmentLoader;
// 加载环境变量
EnvironmentLoader::load();
$client = new Dialog360Client(
apiKey: EnvironmentLoader::get('DIALOG360_API_KEY'),
phoneNumberId: EnvironmentLoader::get('DIALOG360_PHONE_NUMBER_ID'),
baseUrl: EnvironmentLoader::get('DIALOG360_BASE_URL', 'https://waba-v2.360dialog.io'),
timeout: (int) EnvironmentLoader::get('DIALOG360_TIMEOUT', 30),
retryAttempts: (int) EnvironmentLoader::get('DIALOG360_RETRY_ATTEMPTS', 3)
);
use Dialog360\Message\TextMessage;
$message = new TextMessage(
to: '1234567890',
text: 'Hello World!',
previewUrl: false // 可选,是否显示链接预览
);
use Dialog360\Message\MediaMessage;
// 图片消息
$imageMessage = new MediaMessage(
to: '1234567890',
type: 'image',
url: 'https://example.com/image.jpg',
caption: 'Beautiful image!' // 可选
);
// 音频消息
$audioMessage = new MediaMessage(
to: '1234567890',
type: 'audio',
url: 'https://example.com/audio.mp3'
);
// 视频消息
$videoMessage = new MediaMessage(
to: '1234567890',
type: 'video',
url: 'https://example.com/video.mp4',
caption: 'Check out this video!'
);
// 文档消息
$documentMessage = new MediaMessage(
to: '1234567890',
type: 'document',
url: 'https://example.com/document.pdf',
filename: 'document.pdf' // 可选
);
use Dialog360\Message\TemplateMessage;
$templateMessage = new TemplateMessage(
to: '1234567890',
templateName: 'hello_world',
language: 'en_US',
components: [
[
'type' => 'body',
'parameters' => [
[
'type' => 'text',
'text' => 'John'
]
]
]
]
);
use Dialog360\Message\InteractiveMessage;
// 按钮消息
$buttonMessage = new InteractiveMessage(
to: '1234567890',
type: 'button',
body: 'Choose an option:',
buttons: [
[
'type' => 'reply',
'reply' => [
'id' => 'btn_1',
'title' => 'Option 1'
]
],
[
'type' => 'reply',
'reply' => [
'id' => 'btn_2',
'title' => 'Option 2'
]
]
]
);
// 列表消息
$listMessage = new InteractiveMessage(
to: '1234567890',
type: 'list',
body: 'Select from the list:',
action: [
'button' => 'View Options',
'sections' => [
[
'title' => 'Section 1',
'rows' => [
[
'id' => 'item_1',
'title' => 'Item 1',
'description' => 'Description for item 1'
],
[
'id' => 'item_2',
'title' => 'Item 2',
'description' => 'Description for item 2'
]
]
]
]
]
);
$health = $client->getHealthStatus();
echo $health['health_status']['can_send_message'] ?? 'UNKNOWN';
$mediaId = $client->media()->upload('/path/to/file.jpg', 'image/jpeg');
$info = $client->media()->getInfo($mediaId); // GET /{media-id}:url / mime_type / sha256 / file_size
$content = $client->media()->download($mediaId, '/tmp/file.jpg'); // 下载,可选落盘
$client->media()->downloadByUrl($info->getUrl(), '/tmp/file.jpg'); // 用下载URL直接落盘
$client->media()->delete($mediaId); // DELETE /{media-id}
use Dialog360\Api\MediaApi;
// 一步式:自动 创建会话 → 逐块上传 → 返回 handle
$handle = $client->media()->uploadResumable('/path/to/large.mp4', 'video/mp4');
echo $handle->getHandle();
// 手动控制会话
$session = $client->media()->createUploadSession('large.mp4', filesize('/path/to/large.mp4'), 'video/mp4');
$sessionId = $session->getSessionId();
$status = $client->media()->getUploadSessionStatus($sessionId); // GET /upload:{session-id}
$response = $client->media()->uploadChunk($sessionId, $chunk, $status->getFileOffset());
// 或直接带 upload: 前缀的会话ID一步上传
$response = $client->media()->uploadWithSession('upload:' . $sessionId, file_get_contents('/path/to/file.jpg'));
use Dialog360\Exception\Dialog360Exception;
try {
$response = $client->sendMessage($message);
if (!$response->isSuccess()) {
echo "错误: " . $response->getErrorMessage();
echo "错误代码: " . $response->getErrorCode();
}
} catch (Dialog360Exception $e) {
echo "SDK错误: " . $e->getMessage();
} catch (\Exception $e) {
echo "一般错误: " . $e->getMessage();
}