Integrazione PHP
Integrare l'API SMSBAT nella tua applicazione PHP è semplice utilizzando la libreria cURL integrata o il popolare client HTTP Guzzle.
Utilizzo del client HTTP Guzzle (consigliato)
Innanzitutto, installa Guzzle utilizzando Composer:
Quindi, utilizza il seguente codice per inviare un messaggio:
<?php
require 'vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
$client = new Client([
'base_uri' => 'https://api.smsbat.com',
'timeout' => 10.0,
]);
$apiKey = 'YOUR_API_KEY_HERE';
$payload = [
'messages' => [
[
'from' => 'ALPHANAME',
'to' => '380501234567',
'text' => 'Hello from PHP and SMSBAT!',
'type' => 'sms'
]
]
];
try {
$response = $client->post('/bat/messagelist', [
'headers' => [
'Content-Type' => 'application/json',
'X-Authorization-Key' => $apiKey
],
'json' => $payload
]);
echo "Status Code: " . $response->getStatusCode() . "\n";
echo "Response: " . $response->getBody() . "\n";
} catch (RequestException $e) {
echo "Error: " . $e->getMessage() . "\n";
if ($e->hasResponse()) {
echo "Response: " . $e->getResponse()->getBody() . "\n";
}
}
Utilizzo del cURL nativo
Se preferisci non utilizzare pacchetti di terze parti, puoi utilizzare l'estensione cURL nativa di PHP:
CODICE_BLOCCO_2