Cara Mengirim API WhatsApp menggunakan PHP

Pendahuluan

Dalam cuplikan kode ini, Anda belajar cara mengirim WhatsApp API menggunakan PHP, untuk mengirim pesan teks, Template WhatsApp, media, tombol interaktif, daftar, produk … dll, akun Alvochat diperlukan untuk menjalankan kode berikut. Buat Akun jika Anda tidak memilikinya.

Kirim template menggunakan WhatsApp API dan PHP

<?php


$params=array(
'token' => 'YourToken',
'to' => '16315555555',
'name' => 'hello_world',
'language' => 'en_us',
'header' => '',
'body' => '',
'buttons' => '',
'priority' => ''
);
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.alvochat.com/instance1199/messages/template",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_SSL_VERIFYHOST => 0,
  CURLOPT_SSL_VERIFYPEER => 0,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => http_build_query($params),
  CURLOPT_HTTPHEADER => array(
    "content-type: application/x-www-form-urlencoded"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

Mengirim pesan teks menggunakan WhatsApp API dan PHP

<?php


$params=array(
'token' => 'YourToken',
'to' => '16315555555',
'body' => 'WhatsApp API on alvochat.com works good',
'priority' => '',
'preview_url' => '',
'message_id' => ''
);
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.alvochat.com/instance1199/messages/chat",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_SSL_VERIFYHOST => 0,
  CURLOPT_SSL_VERIFYPEER => 0,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => http_build_query($params),
  CURLOPT_HTTPHEADER => array(
    "content-type: application/x-www-form-urlencoded"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

Kirim gambar

<?php


$params=array(
'token' => 'YourToken',
'to' => '16315555555',
'image' => 'https://alvochat-example.s3-accelerate.amazonaws.com/image/1.jpeg',
'caption' => 'image caption',
'priority' => '',
'message_id' => ''
);
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.alvochat.com/instance1199/messages/image",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_SSL_VERIFYHOST => 0,
  CURLOPT_SSL_VERIFYPEER => 0,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => http_build_query($params),
  CURLOPT_HTTPHEADER => array(
    "content-type: application/x-www-form-urlencoded"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

Kirim audio

<?php


$params=array(
'token' => 'YourToken',
'to' => '16315555555',
'audio' => 'https://alvochat-example.s3-accelerate.amazonaws.com/audio/1.mp3',
'priority' => '',
'message_id' => ''
);
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.alvochat.com/instance1199/messages/audio",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_SSL_VERIFYHOST => 0,
  CURLOPT_SSL_VERIFYPEER => 0,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => http_build_query($params),
  CURLOPT_HTTPHEADER => array(
    "content-type: application/x-www-form-urlencoded"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

Kirim video

<?php


$params=array(
'token' => 'YourToken',
'to' => '16315555555',
'video' => 'https://alvochat-example.s3-accelerate.amazonaws.com/video/1.mp4',
'caption' => 'video caption',
'priority' => '',
'message_id' => ''
);
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.alvochat.com/instance1199/messages/video",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_SSL_VERIFYHOST => 0,
  CURLOPT_SSL_VERIFYPEER => 0,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => http_build_query($params),
  CURLOPT_HTTPHEADER => array(
    "content-type: application/x-www-form-urlencoded"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

Kirim dokumen

<?php


$params=array(
'token' => 'YourToken',
'to' => '16315555555',
'document' => 'https://alvochat-example.s3-accelerate.amazonaws.com/document/1.pdf',
'filename' => '',
'priority' => '',
'message_id' => ''
);
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.alvochat.com/instance1199/messages/document",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_SSL_VERIFYHOST => 0,
  CURLOPT_SSL_VERIFYPEER => 0,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => http_build_query($params),
  CURLOPT_HTTPHEADER => array(
    "content-type: application/x-www-form-urlencoded"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

Kirim stiker

<?php


$params=array(
'token' => 'YourToken',
'to' => '16315555555',
'sticker' => 'https://alvochat-example.s3-accelerate.amazonaws.com/sticker/1.webp',
'priority' => '',
'message_id' => ''
);
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.alvochat.com/instance1199/messages/sticker",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_SSL_VERIFYHOST => 0,
  CURLOPT_SSL_VERIFYPEER => 0,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => http_build_query($params),
  CURLOPT_HTTPHEADER => array(
    "content-type: application/x-www-form-urlencoded"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

Kirim Kontak

<?php


$params=array(
'token' => 'YourToken',
'to' => '16315555555',
'contact' => '',
'priority' => '',
'message_id' => ''
);
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.alvochat.com/instance1199/messages/contact",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_SSL_VERIFYHOST => 0,
  CURLOPT_SSL_VERIFYPEER => 0,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => http_build_query($params),
  CURLOPT_HTTPHEADER => array(
    "content-type: application/x-www-form-urlencoded"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

Kirim Lokasi

<?php


$params=array(
'token' => 'YourToken',
'to' => '16315555555',
'lat' => '37.484296',
'lng' => '-122.148703',
'address' => 'Menlo Park, California, United States',
'name' => 'Meta Headquarters',
'priority' => '',
'message_id' => ''
);
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.alvochat.com/instance1199/messages/location",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_SSL_VERIFYHOST => 0,
  CURLOPT_SSL_VERIFYPEER => 0,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => http_build_query($params),
  CURLOPT_HTTPHEADER => array(
    "content-type: application/x-www-form-urlencoded"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

Kirim daftar

<?php


$params=array(
'token' => 'YourToken',
'to' => '16315555555',
'header' => 'header',
'body' => ' please select one of the following options',
'footer' => 'footer',
'button' => 'options',
'sections' => 'option_1,option_2,option_3',
'priority' => '',
'message_id' => ''
);
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.alvochat.com/instance1199/messages/list",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_SSL_VERIFYHOST => 0,
  CURLOPT_SSL_VERIFYPEER => 0,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => http_build_query($params),
  CURLOPT_HTTPHEADER => array(
    "content-type: application/x-www-form-urlencoded"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

Tombol Kirim

<?php


$params=array(
'token' => 'YourToken',
'to' => '16315555555',
'header' => 'header',
'body' => ' please select one of the following options',
'footer' => 'footer',
'buttons' => 'option_1,option_2,option_3',
'priority' => '',
'message_id' => ''
);
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.alvochat.com/instance1199/messages/button",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_SSL_VERIFYHOST => 0,
  CURLOPT_SSL_VERIFYPEER => 0,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => http_build_query($params),
  CURLOPT_HTTPHEADER => array(
    "content-type: application/x-www-form-urlencoded"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

Kirim produk

<?php


$params=array(
'token' => 'YourToken',
'to' => '16315555555',
'header' => 'header',
'body' => 'Hi , check out our new products',
'footer' => 'footer',
'catalog_id' => '',
'product' => '',
'priority' => '',
'message_id' => ''
);
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.alvochat.com/instance1199/messages/product",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_SSL_VERIFYHOST => 0,
  CURLOPT_SSL_VERIFYPEER => 0,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => http_build_query($params),
  CURLOPT_HTTPHEADER => array(
    "content-type: application/x-www-form-urlencoded"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

Mengirim pesan teks dengan PHP menggunakan HTTP Request Lib

<?php

require_once 'HTTP/Request2.php';

$params=array(
'token' => 'YourToken',
'to' => '16315555555',
'body' => 'WhatsApp API on alvochat.com works good',
'priority' => '',
'preview_url' => '',
'message_id' => ''
);

$request = new HTTP_Request2();

$request->setUrl('https://api.alvochat.com/instance1199/messages/chat');
$request->setMethod(HTTP_Request2::METHOD_POST);
$request->setConfig(array(
  'follow_redirects' => TRUE
));
$request->setHeader(array(
  'Content-Type' => 'application/x-www-form-urlencoded'
));
$request->addPostParameter($params);
try {
  $response = $request->send();
  if ($response->getStatus() == 200) {
    echo $response->getBody();
  }
  else {
    echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
    $response->getReasonPhrase();
  }
}
catch(HTTP_Request2_Exception $e) {
  echo 'Error: ' . $e->getMessage();
}

Mengirim pesan teks dengan PHP menggunakan Guzzle Lib

<?php
require 'vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Request;

$params=array(
'token' => 'YourToken',
'to' => '16315555555',
'body' => 'WhatsApp API on alvochat.com works good',
'priority' => '',
'preview_url' => '',
'message_id' => ''
);

$client = new Client();
$headers = [
  'Content-Type' => 'application/x-www-form-urlencoded'
];
$options = ['form_params' =>$params ];
$request = new Request('POST', 'https://api.alvochat.com/instance1199/messages/chat', $headers);
$res = $client->sendAsync($request, $options)->wait();
echo $res->getBody();

Anda dapat melihat Dokumentasi API Whatsapp lengkap di sini.