<?php
//utilisable avec https://github.com/capcom6/android-sms-gateway
class KazSMS
{
    private $sms_api_url;
    private $sms_api_user;
    private $sms_api_password;

    public function __construct($sms_api_url, $sms_api_user, $sms_api_password)
    {
        $this->sms_api_url = $sms_api_url;
        $this->sms_api_user = $sms_api_user;
        $this->sms_api_password = $sms_api_password;
    }

    public function send_sms_by_api($sms, $sms_message)
    {
        $data = json_encode([
            'textMessage' => [
                'text' => $sms_message
            ],
            'phoneNumbers' => [
                $sms
            ]
        ]);

        $ch = curl_init($this->sms_api_url);

        curl_setopt_array($ch, [
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => $data,
            CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
            CURLOPT_USERPWD => $this->sms_api_user . ':' . $this->sms_api_password,
            CURLOPT_HTTPHEADER => [
                'Content-Type: application/json',
                'Content-Length: ' . strlen($data)
            ],
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_CONNECTTIMEOUT => 5,
            CURLOPT_TIMEOUT => 15
        ]);

$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);

curl_close($ch);

if ($error) {
    error_log("KazSMS cURL error: $error");
    return false;
}

if ($http_code < 200 || $http_code >= 300) {
    error_log("KazSMS HTTP error $http_code: $response");
    return false;
}

return true;

    }
}
