Send message to whatsapp with php

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

This library helps you easily create a php application with WhatsApp API

License

green-api/whatsapp-api-client-php

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

PHP library for integration with WhatsApp messenger via API of green-api.com service. To use the library you have to get a registration token and an account id in the personal area. There is a free developer account tariff plan.

You can find REST API documentation by url. The library is a wrapper for REST API, so the documentation at the above url applies to the library as well.

composer require green-api/whatsapp-api-client-php
require './vendor/autoload.php'; 

To send a message or to execute some other Green-API method, you have to have the WhatsApp account in the phone application to be authorized. To authorize your account please go to the personal area and scan a QR-code using the WhatsApp application.

How to initialize an object

$greenApi = new GreenApiClient( ID_INSTANCE, API_TOKEN_INSTANCE ); 

Sending a text message to a WhatsApp number

$result = $greenApi->sending->sendMessage('11001234567@g.us', 'Message text'); 

Please note that keys can be obtained from environment variables:

$result = $greenApi->sending->sendFileByUrl( '11001234567@c.us', 'https://www.google.ru/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png', 'googlelogo_color_272x92dp.png', 'Google logo'); 

Sending an image by uploading from the disk

$result = $greenApi->sending->sendFileByUpload('11001234567@c.us', 'C:\Games\PicFromDisk.png', 'PicFromDisk.jpg', 'Picture from disk'); 

Group creation and sending a message to the group

$chatIds = [ '11001234567@c.us' ]; $resultCreate = $greenApi->groups->createGroup('GroupName', $chatIds ); if ($resultCreate->code == 200) $resultSend = $greenApi->sending->sendMessage($resultCreate->data->chatId, 'Message text'); 

IMPORTANT: If one tries to create a group with a non-existent number, WhatsApp may block the sender’s number. The number in the example is non-existent.

Receive incoming messages by HTTP API

The general concept of receiving data in the Green API is described here To start receiving messages by the HTTP API you need to execute the library method:

greenAPI.webhooks.startReceivingNotifications(onEvent) 

onEvent — your method which should contain parameters:

Parameter Description
typewebhook received message type (string)
body message body (json)

Message body types and formats here

This method will be called when an incoming message is received. Next, process messages according to the business logic of your system.

Description Module
Example of sending text sendTextMessage.php
Example of sending a picture by URL sendPictureByLink.php
Example of sending a picture by uploading from the disk sendPictureByUpload.php
Example of a group creation and sending a message to the group createGroupAndSendMessage.php
Example of incoming webhooks receiving receiveNotification.php

The full list of the library methods

API method Description Documentation link
account.getSettings The method is designed to get the current settings of the account GetSettings
account.setSettings The method is designed to set the account settings SetSettings
account.getStateInstance The method is designed to get the state of the account GetStateInstance
account.getStatusInstance The method is designed to get the socket connection state of the account instance with WhatsApp GetStatusInstance
account.reboot The method is designed to restart the account Reboot
account.logout The method is designed to unlogin the account Logout
account.qr The method is designed to get a QR code QR
account.setProfilePicture The method is designed to set the avatar of the account SetProfilePicture
device.getDeviceInfo The method is designed to get information about the device (phone) on which the WhatsApp Business application is running GetDeviceInfo
groups.createGroup The method is designed to create a group chat CreateGroup
groups.updateGroupName The method changes the name of the group chat UpdateGroupName
groups.getGroupData The method gets group chat data GetGroupData
groups.addGroupParticipant The method adds a participant to the group chat AddGroupParticipant
groups.removeGroupParticipant The method removes the participant from the group chat RemoveGroupParticipant
groups.setGroupAdmin The method designates a member of a group chat as an administrator SetGroupAdmin
groups.removeAdmin The method deprives the participant of group chat administration rights RemoveAdmin
groups.setGroupPicture The method sets the avatar of the group SetGroupPicture
groups.leaveGroup The method logs the user of the current account out of the group chat LeaveGroup
journals.getChatHistory The method returns the chat message history GetChatHistory
journals.lastIncomingMessages The method returns the most recent incoming messages of the account LastIncomingMessages
journals.lastOutgoingMessages The method returns the last sent messages of the account LastOutgoingMessages
queues.showMessagesQueue The method is designed to get the list of messages that are in the queue to be sent ShowMessagesQueue
queues.clearMessagesQueue The method is designed to clear the queue of messages to be sent ClearMessagesQueue
marking.readChat The method is designed to mark chat messages as read ReadChat
receiving.receiveNotification The method is designed to receive a single incoming notification from the notification queue ReceiveNotification
receiving.deleteNotification The method is designed to remove an incoming notification from the notification queue DeleteNotification
receiving.downloadFile The method is for downloading received and sent files DownloadFile
sending.sendMessage The method is designed to send a text message to a personal or group chat SendMessage
sending.sendButtons The method is designed to send a message with buttons to a personal or group chat SendButtons
sending.sendTemplateButtons The method is designed to send a message with interactive buttons from the list of templates in a personal or group chat SendTemplateButtons
sending.sendListMessage The method is designed to send a message with a selection button from a list of values to a personal or group chat SendListMessage
sending.sendFileByUpload The method is designed to send a file loaded through a form (form-data) SendFileByUpload
sending.sendFileByUrl The method is designed to send a file downloaded via a link SendFileByUrl
sending.sendLocation The method is designed to send a geolocation message SendLocation
sending.sendContact The method is for sending a message with a contact SendContact
sending.sendLink The method is designed to send a message with a link that will add an image preview, title and description SendLink
sending.forwardMessages The method is designed for forwarding messages to a personal or group chat ForwardMessages
serviceMethods.checkWhatsapp The method checks if there is a WhatsApp account on the phone number CheckWhatsapp
serviceMethods.getAvatar The method returns the avatar of the correspondent or group chat GetAvatar
serviceMethods.getContacts The method is designed to get a list of contacts of the current account GetContacts
serviceMethods.getContactInfo The method is designed to obtain information about the contact GetContactInfo
serviceMethods.deleteMessage The method deletes the message from chat DeleteMessage
serviceMethods.archiveChat The method archives the chat ArchiveChat
serviceMethods.unarchiveChat The method unarchives the chat UnarchiveChat
serviceMethods.setDisappearingChat The method is designed to change the settings of disappearing messages in chats SetDisappearingChat
webhooks.startReceivingNotifications The method is designed to start receiving new notifications
webhooks.stopReceivingNotifications The method is designed to stop receiving new notifications

Service methods documentation

Licensed under MIT terms. Please see file LICENSE

Источник

How to Send a Message by WhatsApp API using PHP easily

Sign up and Go to your instance dashboard and copy your Instance ID and Token which will be used for authenticating.

Example usage for WhatsApp API using PHP:

Send your first WhatsApp message

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: The recipient’s WhatsApp number with international format e.g., +14155552671 or chatID for contact or group e.g [email protected] or [email protected]
  • $body : Message text.

Note: You can see here how to send WhatsApp messages to a group.

Send Image

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

Send Document

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

Send Audio

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

Send Voice

You can Send a ppt audio recording to the phone number or group, But WhatsApp is sensitive to this extension, you need to be in OGG format and the codecs should be the opus, For more information, you can check out this article.

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

Send Video

$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"; $api=$client->sendLinkMessage($to,$link); print_r($api);

Send Contact

$to="put_your_mobile_number_here"; $contact /cdn-cgi/l/email-protection" data-cfemail="7e4f4a4e4e4e4e4e4e4e4e4f3e1d500b0d">[email protected]"; $api=$client->sendContactMessage($to,$contact); print_r($api);

Send Location

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

Send Vcard

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

You can see the previous steps in this video, and you can see Full WhatsApp API Documentation.

Are you using Ultramsg WhatsApp API PHP SDK?

WhatsApp API

Check out the link below for more information about WhatsApp API .

Recent Posts

Источник

Читайте также:  Обо мне
Оцените статью