<?php


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

class Sms {


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

        /**
         * http://www.oursms.net/api/sendsms.php?username=@user&password=@pass&message=@messag
        e&numbers=@mobile&sender=@sender&unicode=E&return=string
         * http://www.oursms.net/api/sendsms.php?username=@user&password=@pass&message=@messag
        e&numbers=@mobile&sender=@sender&unicode=U&return=string
         * http://www.oursms.net/api/sendsms.php?username=@user&password=@pass&message=@messag
        e&numbers=@mobile&sender=@sender&unicode=E&return=string
         */
        //581524713-565346420

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

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

        if($matches && $matches[5]) {
            $url = 'http://www.oursms.net/api/sendsms.php';
            $fields = [
                'username' => 'meetat',
                'password' => 'meetat',
                "message" => $message,
                'numbers' => '966'.$to,
                'sender' => 'Meetat',
                "unicode " => 'E', // [U,E]
                "return " => 'string', // [U,E]
            ];

            $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 => $fields,
            ));
            $response = curl_exec($curl);
            $info = curl_getinfo($curl);
            curl_close($curl);
            if ($info['http_code'] == 200 && $response == '100'){
                return true;
            } else {
                return false;
            }
        }
    }
}
