SDK PHP – SoftAfrik Push API
Client PHP léger pour consommer SoftAfrik Push API (enregistrement PUID, devices, notifications push, inbox).
1. Installation
Le SDK PHP est une classe autonome. Téléchargez l’archive ZIP ou copiez simplement la classe.
/your-project
/sdk
/php
SoftAfrikPushClient.php
README.md
examples/
basic_push.php
2. Classe SoftAfrikPushClient
Copiez ce code dans sdk/php/SoftAfrikPushClient.php.
<?php
declare(strict_types=1);
class SoftAfrikPushClient
{
private string $apiKey;
private string $baseUrl;
public function __construct(string $apiKey, string $baseUrl = 'https://softafrik-push-api.brillanciel.com')
{
$this->apiKey = $apiKey;
$this->baseUrl = rtrim($baseUrl, '/');
}
/* ----------------------------------------------------------
* USERS
* ----------------------------------------------------------*/
public function registerUser(string $externalUserRef, array $meta = []): array
{
return $this->post('/api/users/register', [
'external_user_ref' => $externalUserRef,
'meta' => $meta,
]);
}
/* ----------------------------------------------------------
* DEVICES
* ----------------------------------------------------------*/
public function registerDevice(string $puid, string $appName, string $platform, string $deviceToken, ?string $deviceUid = null, ?string $lang = null, ?string $country = null): array
{
$payload = [
'puid' => $puid,
'app_name' => $appName,
'platform' => $platform,
'device_token' => $deviceToken,
];
if ($deviceUid !== null) $payload['device_uid'] = $deviceUid;
if ($lang !== null) $payload['lang'] = $lang;
if ($country !== null) $payload['country'] = $country;
return $this->post('/api/devices/register', $payload);
}
public function unregisterDevice(string $deviceToken): array
{
return $this->post('/api/devices/unregister', [
'device_token' => $deviceToken,
]);
}
/* ----------------------------------------------------------
* PUSH
* ----------------------------------------------------------*/
public function pushToUser(string $puid, string $title, string $body, array $data = []): array
{
return $this->post('/api/push/to-user', [
'puid' => $puid,
'title' => $title,
'body' => $body,
'data' => $data,
]);
}
public function pushToDevice(string $deviceToken, string $title, string $body, array $data = []): array
{
return $this->post('/api/push/to-device', [
'device_token' => $deviceToken,
'title' => $title,
'body' => $body,
'data' => $data,
]);
}
public function broadcast(?string $appName, ?string $country, string $title, string $body, array $data = []): array
{
$payload = [
'title' => $title,
'body' => $body,
'data' => $data,
];
if ($appName !== null) $payload['app_name'] = $appName;
if ($country !== null) $payload['country'] = $country;
return $this->post('/api/push/broadcast', $payload);
}
/* ----------------------------------------------------------
* INBOX
* ----------------------------------------------------------*/
public function inbox(string $puid): array
{
return $this->get('/api/push/inbox?puid=' . urlencode($puid));
}
public function markAsRead(int $id): array
{
return $this->post('/api/push/read', ['id' => $id]);
}
public function markAllAsRead(string $puid): array
{
return $this->post('/api/push/read-all', ['puid' => $puid]);
}
/* ----------------------------------------------------------
* HTTP HELPERS
* ----------------------------------------------------------*/
private function get(string $path): array
{
$url = $this->baseUrl . $path;
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'X-API-KEY: ' . $this->apiKey,
],
]);
$response = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return json_decode($response, true);
}
private function post(string $path, array $payload): array
{
$url = $this->baseUrl . $path;
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'X-API-KEY: ' . $this->apiKey,
'Content-Type: application/json',
],
CURLOPT_POSTFIELDS => json_encode($payload),
]);
$response = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return json_decode($response, true);
}
}
3. Exemple complet
<?php
require __DIR__ . '/../SoftAfrikPushClient.php';
$client = new SoftAfrikPushClient('VOTRE_API_KEY');
// 1) Register user
$user = $client->registerUser('user_123', ['full_name' => 'John Doe']);
$puid = $user['puid'];
// 2) Register device
$client->registerDevice($puid, 'MyApp', 'android', 'AAAAxxxxx', 'uuid-123');
// 3) Send push
$client->pushToUser($puid, 'Bonjour', 'Votre paiement est confirmé');
// 4) Inbox
$messages = $client->inbox($puid);
// 5) Mark as read
$client->markAsRead($messages[0]['id']);
Rappel des endpoints API
Users
POST /api/users/register— Créer / récupérer un PUID
Devices
POST /api/devices/register— Associer un devicePOST /api/devices/unregister— Supprimer un device
Notifications
POST /api/push/to-user— Push vers un PUIDPOST /api/push/to-device— Push vers un devicePOST /api/push/broadcast— Push broadcast
Inbox (Historique des notifications)
GET /api/push/inbox?puid={PUID}— Récupérer toutes les notificationsPOST /api/push/read— Marquer une notification comme luePOST /api/push/read-all— Tout marquer comme lu
Exemple rapide
<?php
$client = new SoftAfrikPushClient('VOTRE_API_KEY');
// Envoyer une notification simple
$client->pushToUser(
'sa_1001_xxxxxxxx',
'Ping',
'Hello from SoftAfrik Push API'
);