green-api / whatsapp-api-client-php
Green-api.com REST API Client
Installs: 7 268
Dependents: 0
Suggesters: 0
Security: 0
Stars: 14
Watchers: 0
Forks: 7
Open Issues: 4
Requires
- php: >=7.0
- ext-curl: *
- ext-fileinfo: *
- ext-json: *
This package is auto-updated.
Last update: 2024-12-06 16:21:49 UTC
README
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.
API
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.
Installation
Via Composer:
composer require green-api/whatsapp-api-client-php
Import
require './vendor/autoload.php';
Authorization
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.
Running index.php
php -S localhost:8080
Examples
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');
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") );
Sending an image via URL
$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
Sending an image by uploading from the disk
$result = $greenApi->sending->sendFileByUpload('11001234567@c.us',
'C:\Games\PicFromDisk.png', 'PicFromDisk.jpg', 'Picture from disk');
Example url: sendPictureByUpload.php
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.
Example url: createGroupAndSendMessage.php
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:
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.
Examples list
The full list of the library methods
Service methods documentation
https://green-api.com/en/docs/api/
License
Licensed under MIT terms. Please see file LICENSE