How to Send WhatsApp Messages Using PHP
Any statistics or percentages in this article are general industry figures or illustrations, not QuackAPI measurements or promises. Results depend on your audience, consent and sending patterns. QuackAPI connects to WhatsApp as a linked device (an unofficial method), so read our risk and acceptable use policy before sending.
Prerequisites & Installation
Before sending WhatsApp messages with PHP, you need PHP 7.4 or higher and Composer installed. We will use the Guzzle HTTP client for cleaner request handling, though we also cover native cURL.
Install Guzzle via Composer by running the following command in your project directory:
composer require guzzlehttp/guzzleGetting Your API Key
Log into your QuackAPI dashboard and navigate to Profile Settings. Your API key starts with wa_ and is required for authenticating all API requests.
Store your API key as an environment variable or in a secure configuration file. Never hardcode API keys in your PHP source files or commit them to version control.
<?php
// Load API key from environment variable
$apiKey = getenv('QUACKAPI_API_KEY');
$baseUrl = 'https://quackapi.com/v1/messages/send';Sending Text Messages with cURL
PHP's built-in cURL extension is the most portable way to make HTTP requests. Here is how to send a text message using native cURL functions with proper headers and JSON payload.
<?php
$apiKey = getenv('QUACKAPI_API_KEY');
$url = 'https://quackapi.com/v1/messages/send';
$payload = json_encode([
'deviceId' => 1,
'to' => '923001234567',
'content' => 'Hello from PHP!',
'type' => 'text'
]);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'x-api-key: ' . $apiKey
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$result = json_decode($response, true);
echo "Status: $httpCode\n";
print_r($result);Sending with Guzzle
Guzzle provides a much cleaner and more modern API for HTTP requests in PHP. It supports promises, middleware, and automatic JSON encoding, making it ideal for production applications.
<?php
require 'vendor/autoload.php';
use GuzzleHttp\Client;
$client = new Client([
'base_uri' => 'https://quackapi.com',
'headers' => [
'Content-Type' => 'application/json',
'x-api-key' => getenv('QUACKAPI_API_KEY')
]
]);
try {
$response = $client->post('/v1/messages/send', [
'json' => [
'deviceId' => 1,
'to' => '923001234567',
'content' => 'Hello from Guzzle!',
'type' => 'text'
]
]);
$body = json_decode($response->getBody(), true);
echo "Message sent! ID: " . $body['id'] . "\n";
} catch (\GuzzleHttp\Exception\RequestException $e) {
echo "Error: " . $e->getMessage() . "\n";
}Sending Images and Documents
QuackAPI supports sending images with captions and documents like PDFs. Set the type field to image or pdf and provide the file URL in the content field. For documents, include a filename parameter.
<?php
// Send an image with caption
$client->post('/v1/messages/send', [
'json' => [
'deviceId' => 1,
'to' => '923001234567',
'content' => 'https://example.com/photo.jpg',
'type' => 'image',
'caption' => 'Check out this photo!'
]
]);
// Send a PDF document
$client->post('/v1/messages/send', [
'json' => [
'deviceId' => 1,
'to' => '923001234567',
'content' => 'https://example.com/invoice.pdf',
'type' => 'pdf',
'filename' => 'invoice.pdf'
]
]);Complete PHP Class
Here is a complete, reusable PHP class that wraps the QuackAPI API. You can include this in any PHP project to start sending WhatsApp messages with minimal setup.
<?php
require 'vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
class QuackAPIClient
{
private Client $client;
public function __construct(?string $apiKey = null)
{
$key = $apiKey ?? getenv('QUACKAPI_API_KEY');
$this->client = new Client([
'base_uri' => 'https://quackapi.com',
'headers' => [
'Content-Type' => 'application/json',
'x-api-key' => $key
]
]);
}
public function sendText(int $deviceId, string $to, string $message): array
{
return $this->send([
'deviceId' => $deviceId,
'to' => $to,
'content' => $message,
'type' => 'text'
]);
}
public function sendImage(int $deviceId, string $to, string $url, string $caption = ''): array
{
return $this->send([
'deviceId' => $deviceId,
'to' => $to,
'content' => $url,
'type' => 'image',
'caption' => $caption
]);
}
public function sendDocument(int $deviceId, string $to, string $url, string $filename): array
{
return $this->send([
'deviceId' => $deviceId,
'to' => $to,
'content' => $url,
'type' => 'pdf',
'filename' => $filename
]);
}
private function send(array $payload): array
{
try {
$response = $this->client->post('/v1/messages/send', ['json' => $payload]);
return json_decode($response->getBody(), true);
} catch (RequestException $e) {
throw new \RuntimeException('QuackAPI API error: ' . $e->getMessage());
}
}
}
// Usage
$wa = new QuackAPIClient();
$result = $wa->sendText(1, '923001234567', 'Hello from QuackAPI PHP!');
print_r($result);