WhatsApp API with PHP and Laravel
PHP can talk to the WhatsApp API with nothing more than the cURL extension, and Laravel's HTTP client makes the code shorter still. This guide sends messages through InfiQ's API, which accepts Meta's WhatsApp Cloud API message bodies, and adds a webhook route for replies and delivery updates, in plain PHP and in Laravel.
In short
POST the message JSON to https://api.infiq.in/v1/messages with your key in X-INFIQ-API-KEY, using cURL or Http::withHeaders() in Laravel. Keep the key in .env, send an Idempotency-Key so a retry can't send twice, and back off on 429. For webhooks, read the raw body (php://input or $request->getContent()) and verify X-InfiQ-Signature before trusting it.Plain PHP with cURL
A small function that sends any message body and returns the wamid. It needs PHP 8.0 or later with the cURL extension:
<?phpfunction infiqSend(array $body, ?string $idempotencyKey = null): string{$ch = curl_init('https://api.infiq.in/v1/messages');curl_setopt_array($ch, [CURLOPT_POST => true,CURLOPT_RETURNTRANSFER => true,CURLOPT_TIMEOUT => 30,CURLOPT_HTTPHEADER => ['X-INFIQ-API-KEY: ' . getenv('INFIQ_API_KEY'),'Content-Type: application/json','Idempotency-Key: ' . ($idempotencyKey ?? bin2hex(random_bytes(16))),],CURLOPT_POSTFIELDS => json_encode($body),]);$data = json_decode(curl_exec($ch), true);$status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);curl_close($ch);if ($status >= 400) {$error = $data['error'] ?? [];throw new RuntimeException(($error['code'] ?? $status) . ': ' . ($error['message'] ?? 'Send failed'));}return $data['messages'][0]['id']; // wamid…}$wamid = infiqSend(['messaging_product' => 'whatsapp','to' => '919820000001','type' => 'document','document' => ['link' => 'https://www.infiq.in/media/INF-3091.pdf','filename' => 'Invoice-INF-3091.pdf','caption' => 'Invoice for your order',],], 'INF-3091-invoice');
Laravel: configure the key
Put the keys in .env and read them through config/services.php, so they are cached with your config and never hard-coded:
INFIQ_API_KEY=isk_live_…INFIQ_WEBHOOK_KEY=…
Laravel: send with the HTTP client
Laravel's HTTP client encodes the JSON and can retry. Retry only on 429 and server errors, and keep the same Idempotency-Key across the retries:
use Illuminate\Http\Client\RequestException;use Illuminate\Support\Facades\Http;$response = Http::withHeaders(['X-INFIQ-API-KEY' => config('services.infiq.key'),'Idempotency-Key' => "renewal-reminder:{$customer->phone}",])->retry(3, 1000, fn ($e) => $e instanceof RequestException&& ($e->response->status() === 429 || $e->response->serverError()))->post('https://api.infiq.in/v1/messages', ['messaging_product' => 'whatsapp','to' => $customer->phone, // 919820000001'type' => 'template','template' => ['name' => 'renewal_reminder','language' => ['code' => 'en'],'components' => [['type' => 'body','parameters' => [['type' => 'text', 'text' => $customer->name],['type' => 'text', 'text' => '30 Nov'],],]],],]);$wamid = $response->json('messages.0.id');
Laravel: receive webhooks
Define the route in routes/api.php, which has no CSRF check (in Laravel 11 and later, create that file with php artisan install:api). Verify the signature against the raw body from $request->getContent(), then queue the work:
use App\Jobs\ProcessInfiqEvent;use Illuminate\Http\Request;use Illuminate\Support\Facades\Route;Route::post('/webhooks/infiq', function (Request $request) {$raw = $request->getContent();$valid = verifyInfiqSignature($request->header('X-InfiQ-Signature', ''),$raw,config('services.infiq.webhook_key'),);abort_unless($valid, 401);ProcessInfiqEvent::dispatch(json_decode($raw, true)); // dedupe on the event id therereturn response()->noContent();});
PHP mistakes that break sends
When a PHP integration misbehaves, it is usually one of these:
- Reading $_POST in the webhook. InfiQ posts JSON, so $_POST is empty; read php://input (or $request->getContent() in Laravel) instead.
- A number with a plus sign or spaces. Send to as digits only, with the country code: 919820000001.
- No timeout. Without CURLOPT_TIMEOUT a stuck request can hold a PHP worker indefinitely; 30 seconds is plenty for one send.
- Sending inside the page request. For more than a handful of messages, push each send to a queue (Laravel queues, or a cron worker) so no page load waits on hundreds of API calls.
WordPress and shared hosting
On WordPress, call the API from server-side PHP with wp_remote_post and define the key in wp-config.php; never put it in JavaScript that runs in the browser. Shared hosting works too, as long as outbound HTTPS requests are allowed.
// wp-config.php: define('INFIQ_API_KEY', 'isk_live_…');$response = wp_remote_post('https://api.infiq.in/v1/messages', ['headers' => ['X-INFIQ-API-KEY' => INFIQ_API_KEY,'Content-Type' => 'application/json',],'body' => wp_json_encode($body),'timeout' => 30,]);if (is_wp_error($response) || wp_remote_retrieve_response_code($response) >= 400) {error_log('InfiQ send failed: ' . wp_remote_retrieve_body($response));}
Frequently asked questions
Is there a WhatsApp API SDK for PHP?+
Why does my Laravel webhook return 419?+
Can I send WhatsApp messages from a PHP form on my website?+
How do I send a PDF on WhatsApp with PHP?+
Add WhatsApp to your PHP app this week
Start with a sandbox key, send from cURL or Laravel, and switch to live when your number is verified.