PHP code example of jackie-source / whatsapp-cloud-api
1. Go to this page and download the library: Download jackie-source/whatsapp-cloud-api library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
jackie-source / whatsapp-cloud-api example snippets
// Require the Composer autoloader.
pCloudApi;
// Instantiate the WhatsAppCloudApi super class.
$whatsapp_cloud_api = new WhatsAppCloudApi([
'from_phone_number_id' => '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');
use JackieSource\WhatsAppCloudApi\Message\Media\LinkID;
use JackieSource\WhatsAppCloudApi\Message\Media\MediaObjectID;
$document_id = '341476474779872';
$document_name = 'whatsapp-cloud-api-from-id.pdf';
$document_caption = 'WhastApp API Cloud Guide';
// With the Media Object ID of some document upload on the WhatsApp Cloud servers
$media_id = new MediaObjectID($document_id);
$whatsapp_cloud_api->sendDocument('34676104574', $media_id, $document_name, $document_caption);
// Or
$document_link = 'https://jackie-source.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);
$whatsapp_cloud_api->sendTemplate('34676104574', 'hello_world', 'en_US'); // Language is optional
use JackieSource\WhatsAppCloudApi\Message\Media\LinkID;
$audio_link = 'https://jackie-source.es/wp-content/uploads/2022/05/file_example_OOG_1MG.ogg';
$link_id = new LinkID($audio_link);
$whatsapp_cloud_api->sendAudio('34676104574', $link_id);
use JackieSource\WhatsAppCloudApi\Message\Media\LinkID;
use JackieSource\WhatsAppCloudApi\Message\Media\MediaObjectID;
$link_id = new LinkID('http(s)://image-url');
$whatsapp_cloud_api->sendImage('<destination-phone-number>', $link_id);
//or
$media_id = new MediaObjectID('<image-object-id>');
$whatsapp_cloud_api->sendImage('<destination-phone-number>', $media_id);
use JackieSource\WhatsAppCloudApi\Message\Media\LinkID;
use JackieSource\WhatsAppCloudApi\Message\Media\MediaObjectID;
$link_id = new LinkID('http(s)://video-url');
$whatsapp_cloud_api->sendVideo('<destination-phone-number>', $link_id, '<video-caption>');
//or
$media_id = new MediaObjectID('<image-object-id>');
$whatsapp_cloud_api->sendVideo('<destination-phone-number>', $media_id, '<video-caption>');
use JackieSource\WhatsAppCloudApi\Message\Media\LinkID;
use JackieSource\WhatsAppCloudApi\Message\Media\MediaObjectID;
$link_id = new LinkID('http(s)://sticker-url');
$whatsapp_cloud_api->sendSticker('<destination-phone-number>', $link_id);
//or
$media_id = new MediaObjectID('<sticker-object-id>');
$whatsapp_cloud_api->sendSticker('<destination-phone-number>', $media_id);
use JackieSource\WhatsAppCloudApi\Message\Contact\ContactName;
use JackieSource\WhatsAppCloudApi\Message\Contact\Phone;
use JackieSource\WhatsAppCloudApi\Message\Contact\PhoneType;
$name = new ContactName('Adams', 'Smith');
$phone = new Phone('34676204577', PhoneType::CELL());
$whatsapp_cloud_api->sendContact('<destination-phone-number>', $name, $phone);
use JackieSource\WhatsAppCloudApi\Message\OptionsList\Row;
use JackieSource\WhatsAppCloudApi\Message\OptionsList\Section;
use JackieSource\WhatsAppCloudApi\Message\OptionsList\Action;
$rows = [
new Row('1', '⭐️', "Experience wasn't good enough"),
new Row('2', '⭐⭐️', "Experience could be better"),
new Row('3', '⭐⭐⭐️', "Experience was ok"),
new Row('4', '⭐⭐️⭐⭐', "Experience was good"),
new Row('5', '⭐⭐️⭐⭐⭐️', "Experience was excellent"),
];
$sections = [new Section('Stars', $rows)];
$action = new Action('Submit', $sections);
$whatsapp_cloud_api->sendList(
'<destination-phone-number>',
'Rate your experience',
'Please consider rating your shopping experience in our website',
'Thanks for your time',
$action
);
$response = $whatsapp_cloud_api->uploadMedia('my-image.png');
$media_id = new MediaObjectID($response->decodedBody()['id']);
$whatsapp_cloud_api->sendImage('<destination-phone-number>', $media_id);
try {
$response = $this->whatsapp_app_cloud_api->sendTextMessage(
'<destination-phone-number>,
'Hey there! I\'m using WhatsApp Cloud API. Visit https://www.netflie.es',
true
);
} catch (\JackieSource\WhatsAppCloudApi\Response\ResponseException $e) {
print_r($e->response()); // You can still check the Response returned from Meta servers
}
JackieSource\WhatsAppCloudApi\WebHook;
// Instantiate the WhatsAppCloudApi super class.
$webhook = new WebHook();
echo $webhook->verify($_GET, "<the-verify-token-defined-in-your-app-dashboard>");
e('STDOUT', fopen('php://stdout', 'w'));
use JackieSource\WhatsAppCloudApi\WebHook;
$payload = file_get_contents('php://input');
fwrite(STDOUT, print_r($payload, true) . "\n");
// Instantiate the Webhook super class.
$webhook = new WebHook();
fwrite(STDOUT, print_r($webhook->read(json_decode($payload, true)), true) . "\n");