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.
Via Composer:
composer require green-api/whatsapp-api-client-phprequire './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.
php -S localhost:8080
$greenApi = new GreenApiClient( ID_INSTANCE, API_TOKEN_INSTANCE );
$result = $greenApi->sending->sendMessage('11001234567@g.us', 'Message text');
Example url: sendTextMessage.php
Please note that keys can be obtained from environment variables:
<?php
require './vendor/autoload.php';
define( "ID_INSTANCE", getenv("ID_INSTANCE" ));
define( "API_TOKEN_INSTANCE", getenv("API_TOKEN_INSTANCE") );
$result = $greenApi->serviceMethods->sendTyping('11001234567@c.us', 5000);
Example url: sendTyping.php
$result = $greenApi->serviceMethods->sendTyping('11001234567@c.us', 5000, 'recording');
Example url: sendTyping.php
$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');
Example url: sendPictureByLink.php
$result = $greenApi->sending->sendFileByUpload('11001234567@c.us',
'C:\Games\PicFromDisk.png', 'PicFromDisk.jpg', 'Picture from disk');
Example url: sendPictureByUpload.php
$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.
Example url: createGroupAndSendMessage.php
The calls methods control WhatsApp voice calls of the instance: 1:1 calls only, one call at a time, on plans that
include calls. They start, accept, reject and end calls and return the call state and the recording. The call audio
needs WebRTC, which this library does not include, so a call answered from PHP is silent. Outgoing calls work only to
chats that wrote or called the instance in the last 72 hours.
$dial = $greenApi->calls->callsDial('11001234567@c.us');
$callId = $dial->data->callId;
$state = $greenApi->calls->callsState();
echo $state->data->state;
$greenApi->calls->callsHangUp();
$recording = $greenApi->calls->getCallRecording($callId);
$incoming = $greenApi->journals->lastIncomingCalls(60);
callId is the idMessage of the call webhooks (incomingCall, outgoingCall), enable them with
incomingCallWebhook and outgoingCallWebhook in setSettings. The methods that need a call return code 409 when
there is none.
Example url: calls.php
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 typing or audio recording notification | sendTyping.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 |
| Example if working with contacts | contactsMethods.php |
| Example of sending a poll | sendPoll.php |
| Example of sending interactive buttons | sendInteractiveButtons.php |
| Example of updating group settings | updateGroupSettings.php |
| Example of getting chats list | getChats.php |
| Example of voice calls | calls.php |
| 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.getAuthorizationCode |
The method is designed to authorize an instance by phone number | GetAuthorizationCode |
account.setProfilePicture |
The method is designed to set the avatar of the account | SetProfilePicture |
account.updateApiToken |
The method is designed to update the instance API token | UpdateApiToken |
calls.callsDial |
The method starts an outgoing voice call | CallsDial |
calls.callsAccept |
The method accepts the incoming call | CallsAccept |
calls.callsReject |
The method rejects the incoming call | CallsReject |
calls.callsHangUp |
The method ends the current call | CallsHangUp |
calls.callsState |
The method returns the current call state | CallsState |
calls.callsGetIceServers |
The method returns the ICE servers for the call audio | CallsGetIceServers |
calls.getCallRecording |
The method returns the recording of a call | |
contacts.addContact() |
The method is aimed for adding a number to contacts | AddContact |
contacts.editContact() |
The method is used to edit a number in contacts | EditContact |
contacts.deleteContact() |
The method is used to remove a number from contacts | DeleteContact |
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 |
groups.updateGroupSettings |
The method changes group chat settings | UpdateGroupSettings |
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 |
journals.lastIncomingCalls |
The method returns the last incoming calls of the account | LastIncomingCalls |
journals.lastOutgoingCalls |
The method returns the last outgoing calls of the account | LastOutgoingCalls |
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 |
sending.sendPoll |
The method is designed to send a poll message to a personal or group chat | SendPoll |
sending.sendInteractiveButtons |
The method is designed to send a message with interactive buttons to a personal or group chat | SendInteractiveButtons |
sending.sendInteractiveButtonsReply |
The method is designed to send a reply message with interactive buttons to a personal or group chat | SendInteractiveButtonsReply |
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.editMessage |
The method edits a message in a chat | EditMessage |
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 |
serviceMethods.getChats |
The method returns the list of chats of the current account | GetChats |
serviceMethods.sendTyping |
The method is designed to send a typing or audio recording notification to a chat | SendTyping |
webhooks.startReceivingNotifications |
The method is designed to start receiving new notifications | |
webhooks.stopReceivingNotifications |
The method is designed to stop receiving new notifications | |
statuses.sendTextStatus |
The method is aimed for sending a text status | SendTextStatus |
statuses.sendVoiceStatus |
The method is aimed for sending a voice status | SendVoiceStatus |
statuses.sendMediaStatus |
The method is aimed for sending a pictures or video status | SendMediaStatus |
statuses.getIncomingStatuses |
The method returns the incoming status messages of the instance | GetIncomingStatuses |
statuses.getOutgoingStatuses |
The method returns the outgoing statuses of the account | GetOutgoingStatuses |
statuses.getStatusStatistic |
The method returns an array of recipients marked sent/delivered/read for a given status | GetStatusStatistic |
partner.getInstances |
The method is designed to get the instances of the account | GetInstances |
partner.createInstance |
The method is designed to create the instance in the account | CreateInstance |
partner.deleteInstanceAccount |
The method is designed to remove the instance in the account | DeleteInstanceAccount |
https://green-api.com/en/docs/api/
Licensed under MIT terms. Please see file LICENSE