في المغرب، يتطلب إرسال الرسائل الموثوقة استخدام مسارات مباشرة مع مشغلي الاتصالات (اتصالات المغرب، إنوي، أورنج) والامتثال للأنظمة المحلية. Pour démarrer rapidement, l'API EnvoiSMS.ma propose un endpoint RESTful propre. Voici les implémentations standards recommandées pour vos applications.
// Exemple 1 : Requête cURL
curl -X POST https://api.envoisms.ma/v1/messages \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"to": "+212612345678",
"body": "Votre code de sécurité EnvoiSMS est 849301",
"sender": "EnvoiSMS"
}'
// Exemple 2 : Intégration en Node.js (Axios)
const axios = require('axios');
async function sendSMS() {
try {
const response = await axios.post('https://api.envoisms.ma/v1/messages', {
to: '+212612345678',
body: 'Votre code de sécurité EnvoiSMS est 849301',
sender: 'EnvoiSMS'
}, {
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
}
});
console.log('Message ID:', response.data.id);
} catch (error) {
console.error('Erreur d\'envoi:', error.response.data);
}
}
sendSMS();
// Exemple 3 : Intégration en PHP (cURL natif)
<?php
$ch = curl_init('https://api.envoisms.ma/v1/messages');
$payload = json_encode([
"to" => "+212612345678",
"body" => "Votre code de sécurité EnvoiSMS est 849301",
"sender" => "EnvoiSMS"
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer YOUR_API_KEY',
'Content-Type: application/json'
]);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Erreur : ' . curl_error($ch);
} else {
$result = json_decode($response, true);
echo 'Message ID : ' . $result['id'];
}
curl_close($ch);
?>