PHP code example of vishwaraj / whatsapp-cloud-api
1. Go to this page and download the library: Download vishwaraj/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/ */
vishwaraj / whatsapp-cloud-api example snippets
use Vishwaraj\WhatsAppCloud\Facades\WhatsAppCloud;
$response = WhatsAppCloud::text()
->to('919999999999')
->body('Hello from WhatsApp Cloud API!')
->send();
echo $response->messageId(); // wamid.xxx
WhatsAppCloud::text()
->to('919999999999')
->body('This is a reply')
->replyTo('wamid.previous_message_id')
->send();
// From URL
WhatsAppCloud::image()
->to('919999999999')
->fromUrl('https://example.com/photo.jpg')
->caption('A beautiful photo')
->send();
// From uploaded media ID
WhatsAppCloud::image()
->to('919999999999')
->fromMediaId('media-object-id')
->send();
// Auto-upload from local path
WhatsAppCloud::image()
->to('919999999999')
->fromPath('/path/to/image.jpg')
->caption('Uploaded photo')
->send();
WhatsAppCloud::interactive()
->product()
->to('919999999999')
->body('Check out this product')
->catalogId('your-catalog-id')
->productId('product-sku-123')
->send();
WhatsAppCloud::interactive()
->productList()
->to('919999999999')
->headerText('Our Top Picks')
->body('Browse and order directly')
->catalogId('your-catalog-id')
->section('Electronics', ['sku-laptop', 'sku-phone', 'sku-tablet'])
->section('Accessories', ['sku-case', 'sku-charger'])
->send();
WhatsAppCloud::interactive()
->orderStatus()
->to('919999999999')
->body('Order update')
->referenceId('ORD-2024-001')
->status('processing')
->description('Your order is being prepared')
->send();
use Vishwaraj\WhatsAppCloud\DTO\Templates\TemplateDefinitionData;
use Vishwaraj\WhatsAppCloud\Enums\TemplateCategory;
// Create template
$template = new TemplateDefinitionData(
name: 'summer_promo_2024',
language: 'en_US',
category: TemplateCategory::Marketing,
components: [
[
'type' => 'HEADER',
'format' => 'TEXT',
'text' => 'Summer Sale 🌞',
],
[
'type' => 'BODY',
'text' => 'Hi {{1}}, use code {{2}} for {{3}} off!',
'example' => ['body_text' => [['John', 'SAVE25', '25%']]],
],
[
'type' => 'FOOTER',
'text' => 'Tap Stop to unsubscribe',
],
[
'type' => 'BUTTONS',
'buttons' => [
['type' => 'QUICK_REPLY', 'text' => 'Stop promotions'],
],
],
],
);
$response = WhatsAppCloud::templates()->create($template);
echo $response->id; // template id
echo $response->status; // PENDING or APPROVED
// List all templates
$templates = WhatsAppCloud::templates()->list();
// Find by name
$results = WhatsAppCloud::templates()->find('summer_promo_2024');
// Delete by name
WhatsAppCloud::templates()->delete('summer_promo_2024');
// Upload
$upload = WhatsAppCloud::media()->upload('/path/to/image.jpg');
echo $upload->id; // use this as media ID
// Get media info
$info = WhatsAppCloud::media()->get('media-id-123');
echo $info->url; // temporary download URL (valid 5 min)
echo $info->mimeType;
// Download to file
WhatsAppCloud::media()->download('media-id-123', '/local/save/path.jpg');
// Download as string
$bytes = WhatsAppCloud::media()->download('media-id-123');
// Get download URL only
$url = WhatsAppCloud::media()->downloadUrl('media-id-123');
// Delete
WhatsAppCloud::media()->delete('media-id-123');
// Use a different phone number for this request
WhatsAppCloud::usingPhoneNumber('987654321')
->text()
->to('919999999999')
->body('From a different number')
->send();
// Use a different token
WhatsAppCloud::usingToken('another-access-token')
->text()
->to('919999999999')
->body('With different token')
->send();
// Use a different API version
WhatsAppCloud::usingVersion('v24.0')
->text()
->to('919999999999')
->body('Using v24')
->send();
use Vishwaraj\WhatsAppCloud\Exceptions\WhatsAppApiException;
use Vishwaraj\WhatsAppCloud\Exceptions\AuthenticationException;
use Vishwaraj\WhatsAppCloud\Exceptions\RateLimitException;
try {
WhatsAppCloud::text()->to('919999999999')->body('Hello')->send();
} catch (AuthenticationException $e) {
// Invalid or expired token
} catch (RateLimitException $e) {
// Too many requests
} catch (WhatsAppApiException $e) {
echo $e->getCode(); // Meta error code
echo $e->getMessage(); // Human-readable message
echo $e->getDetails(); // Additional details if available
}