<?php


namespace App\Helpers;
//use \Unifonic\API\Client;
use UnifonicNextGenLib\UnifonicNextGenClient;

class Sms {


    public static function send($to, $message)
    {

        //581524713-565346420

//        $message = \App\Helpers\Sms::send('581524713', 'hello from CREATIVE-EYE');
//        dd($message);

        $re = '/^((00966)|(\+966)|(0))?([0-9]{7,9})$/';
        preg_match($re, $to, $matches);

        if($matches && $matches[5]) {
            $url = 'https://www.msegat.com/gw/sendsms.php';
            $fields = [
                'userName' => 'Ienmtr@2050',
                'apiKey' => '4a65fc3792d92458ae2980e685712b64',
                'numbers' => '966'.$to,
                'userSender' => 'ienmtr',
                "msg" => $message,
                "msgEncoding " => 'UTF8', // [UTF8, windows-1256]
            ];

            $curl = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_URL => $url,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_HEADER => true,
                CURLOPT_POST => true,
                CURLOPT_HTTPHEADER => [
                    "Content-Type: application/json"
                ],
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => "POST",
                CURLOPT_POSTFIELDS => json_encode($fields),
            ));
            $response = curl_exec($curl);
            $info = curl_getinfo($curl);
            curl_close($curl);
            if ($info['http_code'] == 200){
                return true;
//                return $response;
            } else {
                return false;
            }
        }
    }
}
