Как легко отправить сообщение
через WhatsApp API с помощью PHP


Введение

В этом уроке мы создадим страницу для отправки сообщения через API WhatsApp с использованием PHP.

Установка

Просто скачайте ultramsg.class.php или используйте Composer:

composer require ultramsg/whatsapp-php-sdk
composer require ultramsg/whatsapp-php-sdk

Аутентификация

Зарегистрируйтесь и перейдите на панель инструментов своего экземпляра и скопируйте свой идентификатор экземпляра и токен, которые будут использоваться для аутентификации.

Пример использования WhatsApp API с использованием PHP:

Отправьте свое первое сообщение WhatsApp

require_once ('vendor/autoload.php'); // if you use Composer //require_once('ultramsg.class.php'); // if you download ultramsg.class.php $token="tof7lsdJasdloaa57e"; // Ultramsg.com token $instance_id="instance1150"; // Ultramsg.com instance id $client = new UltraMsg\WhatsAppApi($token,$instance_id); $to="put_your_mobile_number_here"; $body="Hello world"; $api=$client->sendChatMessage($to,$body); print_r($api);
require_once ('vendor/autoload.php'); // if you use Composer //require_once('ultramsg.class.php'); // if you download ultramsg.class.php $token="tof7lsdJasdloaa57e"; // Ultramsg.com token $instance_id="instance1150"; // Ultramsg.com instance id $client = new UltraMsg\WhatsAppApi($token,$instance_id); $to="put_your_mobile_number_here"; $body="Hello world"; $api=$client->sendChatMessage($to,$body); print_r($api);
  • $to: номер WhatsApp получателя в международном формате, например, +14155552671, или идентификатор чата для контакта или группы, например , 14155552671@c.us или 14155552671-441234567890@g.us
  • $body : текст сообщения.

Примечание. Здесь вы можете увидеть, как отправлять сообщения WhatsApp группе.

Отправить изображение

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

Отправить документ

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

Отправить аудио

$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);
$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);

Отправить голос

Вы можете Отправить аудиозапись в формате ppt на номер телефона или в группу, Но WhatsApp чувствителен к этому расширению, необходимо, чтобы аудиозапись была в формате OGG, а кодеки должны быть opus, Для получения дополнительной информации, вы можете ознакомиться с этой статьей.

$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);
$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);

Отправить видео

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

Отправить контакт

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

Отправить местоположение

$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);
$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);

Отправить открытку

$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)
$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)

Вы можете увидеть предыдущие шаги в этом видео , а также можете посмотреть полную документацию WhatsApp API .

Используете ли вы Ultramsg WhatsApp API PHP SDK ?

Img