PHP code example of sukanyeah / wcapi

1. Go to this page and download the library: Download sukanyeah/wcapi 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/ */

    

sukanyeah / wcapi example snippets




// Require the Composer autoloader.
oudApi;

// 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('919876543210', 'Hey there! I\'m using WhatsApp Cloud API. Visit https://www.sukanyeah.com/wcapi');



use Sukanyeah\WhatsAppCloudApi\Message\Media\LinkID;
use Sukanyeah\WhatsAppCloudApi\Message\Media\MediaObjectID;

$document_id = '546919876543210';
$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('919876543210', $media_id, $document_name, $document_caption);

// Or
$document_link = 'https://www.sukanyeah.com/image.png';
$link_id = new LinkID($document_link);
$whatsapp_cloud_api->sendDocument('919876543210', $link_id, $document_name, $document_caption);



$whatsapp_cloud_api->sendTemplate('919876543210', 'hello_world', 'en_US'); // If not specified, Language will be default to en_US and otherwise it will be 



use Sukanyeah\WhatsAppCloudApi\Message\Template\Component;

$component_header = [];

$component_body = [
    [
        'type' => 'text',
        'text' => '*Mr Jones*',
    ],
];

$component_buttons = [
    [
        'type' => 'button',
        'sub_type' => 'quick_reply',
        'index' => 0,
        'parameters' => [
            [
                'type' => 'text',
                'text' => 'Yes',
            ]
        ]
    ],
    [
        'type' => 'button',
        'sub_type' => 'quick_reply',
        'index' => 1,
        'parameters' => [
            [
                'type' => 'text',
                'text' => 'No',
            ]
        ]
    ]
];

$components = new Component($component_header, $component_body, $component_buttons);
$whatsapp_cloud_api->sendTemplate('919876543210', 'sample_issue_resolution', 'en_US', $components); // Language is optional



use Sukanyeah\WhatsAppCloudApi\Message\Media\LinkID;

$audio_link = 'https://sukanyeah.com/wp-content/uploads/2022/05/file_example_OOG_1MG.ogg';
$link_id = new LinkID($audio_link);
$whatsapp_cloud_api->sendAudio('919876543210', $link_id);



use Sukanyeah\WhatsAppCloudApi\Message\Media\LinkID;
use Sukanyeah\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 Sukanyeah\WhatsAppCloudApi\Message\Media\LinkID;
use Sukanyeah\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 Sukanyeah\WhatsAppCloudApi\Message\Media\LinkID;
use Sukanyeah\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);



$whatsapp_cloud_api->sendLocation('<destination-phone-number>', $longitude, $latitude, $name, $address);



$body = 'Let\'s start with your pickup. You can either manually *enter an address* or *share your current location*.';
$whatsapp_cloud_api->sendLocationRequest('<destination-phone-number>', $body);



use Sukanyeah\WhatsAppCloudApi\Message\Contact\ContactName;
use Sukanyeah\WhatsAppCloudApi\Message\Contact\Phone;
use Sukanyeah\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 Sukanyeah\WhatsAppCloudApi\Message\OptionsList\Row;
use Sukanyeah\WhatsAppCloudApi\Message\OptionsList\Section;
use Sukanyeah\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
);



use Sukanyeah\WhatsAppCloudApi\Message\CtaUrl\TitleHeader;

$header = new TitleHeader('Booking');

$whatsapp_cloud_api->sendCtaUrl(
    '<destination-phone-number>',
    'See Dates',
    'https://www.example.com',
    $header,
    'Tap the button below to see available dates.',
    'Dates subject to change.',
);



$body = 'Hello! Thanks for your interest. Ordering is easy. Just visit our catalog and add items you\'d like to purchase.';
$footer = 'Best grocery deals on WhatsApp!';
$sku_thumbnail = '<product-sku-id>'; // product sku id to use as header thumbnail 

$whatsapp_cloud_api->sendCatalog(
    '<destination-phone-number>',
    $body,
    $footer, // optional
    $sku_thumbnail // optional
);



use Sukanyeah\WhatsAppCloudApi\WhatsAppCloudApi;
use Sukanyeah\WhatsAppCloudApi\Message\ButtonReply\Button;
use Sukanyeah\WhatsAppCloudApi\Message\ButtonReply\ButtonAction;

$whatsapp_cloud_api = new WhatsAppCloudApi([
  'from_phone_number_id' => '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(
    '<destination-phone-number>',
    '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 
);



$whatsapp_cloud_api
    ->replyTo('<whatsapp-message-id-to-reply>')
    ->sendTextMessage(
        '919876543210',
        'Hey there! I\'m using WhatsApp Cloud API. Visit https://www.sukanyeah.com/wcapi'
    );



$whatsapp_cloud_api->sendReaction(
        '<destination-phone-number>',
        '<message-id-to-react-to>',
        '👍', // the emoji
    );

// Unreact to a message
$whatsapp_cloud_api->sendReaction(
        '<destination-phone-number>',
        '<message-id-to-unreact-to>'
    );


$response = $whatsapp_cloud_api->uploadMedia('my-image.png');

$media_id = new MediaObjectID($response->decodedBody()['id']);
$whatsapp_cloud_api->sendImage('<destination-phone-number>', $media_id);


$response = $whatsapp_cloud_api->downloadMedia('<media-id>');

try {
    $response = $this->whatsapp_app_cloud_api->sendTextMessage(
        '<destination-phone-number>,
        'Hey there! I\'m using WhatsApp Cloud API. Visit https://www.sukanyeah.com',
        true
    );
} catch (\Sukanyeah\WhatsAppCloudApi\Response\ResponseException $e) {
    print_r($e->response()); // You can still check the Response returned from Meta servers
}


Sukanyeah\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 Sukanyeah\WhatsAppCloudApi\WebHook;


$payload = file_get_contents('php://input');
fwrite(STDOUT, print_r($payload, true) . "\n");

// Instantiate the Webhook super class.
$webhook = new WebHook();

// Read the first message
fwrite(STDOUT, print_r($webhook->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");



$whatsapp_cloud_api->markMessageAsRead('<message-id>');



$whatsapp_cloud_api->businessProfile('<fields>');



$whatsapp_cloud_api->updateBusinessProfile([
    'about' => '<about_text>',
    'email' => '<email>'
]);

composer unit-test

composer integration-test