WhatsApp API untuk PHP: Integrasi Website
Tutorial WhatsApp API dengan PHP. Integrasi ke website, kirim notifikasi order, webhook dengan Laravel. Contoh kode lengkap!
PHP adalah bahasa paling populer untuk web — dan integrasi WhatsApp API sangat straightforward dengan cURL.
Basic Send Message:
php
<?php
function sendWhatsApp($to, $message) {
$phoneId = 'YOUR_PHONE_ID';
$token = 'YOUR_TOKEN';
$ch = curl_init("https://graph.facebook.com/v17.0/{$phoneId}/messages");
curl_setopt_array($ch, [
CURLOPT_HTTPHEADER => [
"Authorization: Bearer {$token}",
"Content-Type: application/json"
],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode([
'messaging_product' => 'whatsapp',
'to' => $to,
'type' => 'text',
'text' => ['body' => $message]
])
]);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
// Usage
sendWhatsApp('6281234567890', 'Hello from PHP!');Webhook untuk Terima Pesan:
php
<?php
// GET: Verification
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
if ($_GET['hub_verify_token'] === 'YOUR_TOKEN') {
echo $_GET['hub_challenge'];
}
exit;
}
// POST: Receive messages
$data = json_decode(file_get_contents('php://input'), true);
if ($data['object'] === 'whatsapp_business_account') {
foreach ($data['entry'] as $entry) {
foreach ($entry['changes'] as $change) {
$messages = $change['value']['messages'] ?? [];
foreach ($messages as $msg) {
$sender = $msg['from'];
$text = $msg['text']['body'] ?? '';
// Process message
sendWhatsApp($sender, "Received: $text");
}
}
}
}
http_response_code(200);Use Cases:
- Order confirmation
- Shipping notification
- Payment reminder
- OTP verification
Coba No-Code Solution — Gratis! →