The first PHP API to send and receive messages using a cloud-hosted version of the WhatsApp Business Platform
The first PHP API to send and receive messages using a cloud-hosted version of the WhatsApp Business Platform
This package makes it easy for developers to access WhatsApp Cloud API service in their PHP code.
The first 1,000 conversations each month are free from WhatsApp Cloud API. A conversation.
Please create and configure your Facebook WhatsApp application following the "Get Stared" section of the official guide.
Minimum requirements – To run the SDK, your system will require PHP >= 7.4 with a recent version of CURL >=7.19.4 compiled with OpenSSL and zlib.
composer require netflie/whatsapp-cloud-api
'your-configured-from-phone-number-id',
'access_token' => 'your-facebook-whatsapp-application-token',
]);
$whatsapp_cloud_api->sendTextMessage('34676104574', 'Hey there! I\'m using WhatsApp Cloud API. Visit https://www.netflie.es');sendTextMessage(null, 'Hello from BSUID', false, $recipient_bsuid);
// Send both values in the same request (WhatsApp Cloud API gives precedence to `to`)
$whatsapp_cloud_api->sendTextMessage('34676104574', 'Hello with both identifiers', false, $recipient_bsuid);The same optional $recipient parameter is available in all send* methods that send messages to /messages.
For sendContact, pass the BSUID before the phone entries:
$contact_name = new \Netflie\WhatsAppCloudApi\Message\Contact\ContactName('John', 'Doe');
$phone = new \Netflie\WhatsAppCloudApi\Message\Contact\Phone('+14155550100', \Netflie\WhatsAppCloudApi\Message\Contact\PhoneType::CELL());
$whatsapp_cloud_api->sendContact(null, $contact_name, $recipient_bsuid, $phone);You can send documents in two ways: by uploading a file to the WhatsApp Cloud servers (where you will receive an identifier) or from a link to a document published on internet.
sendDocument('34676104574', $media_id, $document_name, $document_caption);
// Or
$document_link = 'https://netflie.es/wp-content/uploads/2022/05/image.png';
$link_id = new LinkID($document_link);
$whatsapp_cloud_api->sendDocument('34676104574', $link_id, $document_name, $document_caption);sendTemplate('34676104574', 'hello_world', 'en_US'); // If not specified, Language will be default to en_US and otherwise it will be required.You also can build templates with parameters:
…sendAudio('34676104574', $link_id);sendImage('', $link_id);
//or
$media_id = new MediaObjectID('');
$whatsapp_cloud_api->sendImage('', $media_id);sendVideo('', $link_id, '');
//or
$media_id = new MediaObjectID('');
$whatsapp_cloud_api->sendVideo('', $media_id, '');Stickers sample: https://github.com/WhatsApp/stickers
sendSticker('', $link_id);
//or
$media_id = new MediaObjectID('');
$whatsapp_cloud_api->sendSticker('', $media_id);sendLocation('', $longitude, $latitude, $name, $address);sendLocationRequest('', $body);sendContact('', $name, null, $phone);sendList(
'',
'Rate your experience',
'Please consider rating your shopping experience in our website',
'Thanks for your time',
$action
);sendCtaUrl(
'',
'See Dates',
'https://www.example.com',
$header,
'Tap the button below to see available dates.',
'Dates subject to change.',
);sendCatalog(
'',
$body,
$footer, // optional
$sku_thumbnail // optional
); 'your-configured-from-phone-number-id',
'access_token' => 'your-facebook-whatsapp-application-token'
]);
$rows = [
new Button('button-1', 'Yes'),
new Button('button-2', 'No'),
new Button('button-3', 'Not Now'),
];
$action = new ButtonAction($rows);
$whatsapp_cloud_api->sendButton(
'',
'Would you like to rate us on Trustpilot?',
$action,
'RATE US', // Optional: Specify a header (type "text")
'Please choose an option' // Optional: Specify a footer
);';
$header = 'Grocery Collections';
$body = 'Hello! Thanks for your interest. Here\'s what we can offer you under our grocery collection. Thank you for shopping with us.';
$footer = 'Subject to T&C';
$whatsapp_cloud_api->sendMultiProduct(
'',
$catalog_id,
$action,
$header,
$body,
$footer // optional
);';
$sku_id = '';
$body = 'Hello! Here\'s your requested product. Thanks for shopping with us.';
$footer = 'Subject to T&C';
$whatsapp_cloud_api->sendSingleProduct(
'',
$catalog_id,
$sku_id,
$body, // body: optional
$footer // footer: optional
);You can reply a previous sent message:
replyTo('')
->sendTextMessage(
'34676104574',
'Hey there! I\'m using WhatsApp Cloud API. Visit https://www.netflie.es'
);You can react to a message from your conversations if you know the messageid
sendReaction(
'',
'',
'', // the emoji
);
// Unreact to a message
$whatsapp_cloud_api->sendReaction(
'',
''
);
Media messages accept as identifiers an Internet URL pointing to a public resource (image, video, audio, etc.). When you try to send a media message from a URL you must instantiate the LinkID object.
You can also upload your media resources to WhatsApp servers and you will receive a resource identifier:
$response = $whatsapp_cloud_api->uploadMedia('my-image.png');
$media_id = new MediaObjectID($response->decodedBody()['id']);
$whatsapp_cloud_api->sendImage('', $media_id);
To download a media resource:
$response = $whatsapp_cloud_api->downloadMedia('');WhatsAppCloudApi instance returns a Response class or a ResponseException if WhatsApp servers return an error.
try {
$response = $this->whatsapp_app_cloud_api->sendTextMessage(
',
'Hey there! I\'m using WhatsApp Cloud API. Visit https://www.netflie.es',
true
);
} catch (\Netflie\WhatsAppCloudApi\Response\ResponseException $e) {
print_r($e->response()); // You can still check the Response returned from Meta servers
}Add your webhook in your Meta App dashboard. You need to verify your webhook:
verify($_GET, "");Webhook is now verified, you will start receiving notifications every time your customers send messages.
read(json_decode($payload, true)), true) . "\n");
//Read all messages in case Meta decided to batch them
fwrite(STDOUT, print_r($webhook->readAll(json_decode($payload, true)), true) . "\n");The Webhook::read function will return a Notification instance. Please, explore the different notifications availables.
When you receive an incoming message from Webhooks, you can mark the message as read by changing its status to read. Messages marked as read display two blue check marks alongside their timestamp.
Marking a message as read will also mark earlier messages in the conversation as read.
markMessageAsRead('');Show a "typing..." indicator in the user's chat for up to 25 seconds or until you send a message, whichever comes first. This also marks the referenced message as read.
sendTypingIndicator('');businessProfile('');updateBusinessProfile([
'about' => '',
'email' => ''
]);……Fields list: https://developers.facebook.com/docs/whatsapp/cloud-api/reference/business-profiles
Please see UPGRADE for more information on how to upgrade to v2.
Please see CHANGELOG for more information what has changed recently.
composer unit-testYou also can run tests making real calls to the WhastApp Clou API. Please put your testing credentials on WhatsAppCloudApiTestConfiguration file.
composer integration-testPlease see CONTRIBUTING for details.
The MIT License (MIT). Please see License File for more information. Please see License file for more information.
This package is not officially maintained by Facebook. WhatsApp and Facebook trademarks and logos are the property of Meta Platforms, Inc.
No open issues yet, or sync has not completed.