Skip to main content

PHP

Client class

<?php
// NotifoClient.php

class NotifoClient
{
private string $baseUrl = 'https://api.notifo.cloud';
private string $apiKey;

public function __construct(string $apiKey)
{
$this->apiKey = $apiKey;
}

public function sendText(string $channel, string $to, string $text, array $opts = []): array
{
return $this->post("/v1/notify/{$channel}/text", array_merge(['to' => $to, 'text' => $text], $opts));
}

public function sendMedia(string $channel, string $to, string $type, string $url, array $opts = []): array
{
return $this->post("/v1/notify/{$channel}/media", array_merge([
'to' => $to, 'type' => $type, 'url' => $url,
], $opts));
}

public function sendBulk(string $channel, array $messages): array
{
return $this->post("/v1/notify/{$channel}/bulk", ['messages' => $messages]);
}

private function post(string $path, array $body): array
{
$ch = curl_init($this->baseUrl . $path);
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'X-Api-Key: ' . $this->apiKey,
],
CURLOPT_POSTFIELDS => json_encode($body),
CURLOPT_RETURNTRANSFER => true,
]);
$response = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($status < 200 || $status >= 300) {
throw new \RuntimeException("Notifo error {$status}: {$response}");
}

return json_decode($response, true);
}
}

Usage

<?php
require 'NotifoClient.php';

$client = new NotifoClient($_ENV['NOTIFO_API_KEY']);

// Text
$msg = $client->sendText('whatsapp', '+905551234567', 'Hello from Notifo!');
echo $msg['id'] . ' ' . $msg['status'];

// Image
$client->sendMedia('whatsapp', '+905551234567', 'image', 'https://example.com/photo.jpg', [
'caption' => 'Check this out!',
]);

// Bulk
$results = $client->sendBulk('whatsapp', [
['to' => '+905551111111', 'text' => 'Hi Alice'],
['to' => '+905552222222', 'text' => 'Hi Bob'],
]);

// Idempotent
$client->sendText('whatsapp', '+905551234567', 'OTP: 789012', [
'idempotencyKey' => 'otp-user-42-1700000000',
]);

Webhook verification

<?php
$sig = $_SERVER['HTTP_X_NOTIFO_SIGNATURE'] ?? '';
$rawBody = file_get_contents('php://input');
$secret = $_ENV['NOTIFO_WEBHOOK_SECRET'];

$secretHash = hash('sha256', $secret);
$expected = 'sha256=' . hash_hmac('sha256', $rawBody, $secretHash);

if (!hash_equals($expected, $sig)) {
http_response_code(401);
exit('Invalid signature');
}

$event = json_decode($rawBody, true);
error_log("[{$event['event']}] message={$event['messageId']} status={$event['status']}");
http_response_code(200);