From d6020f78773c441fa8811a057cfaee46cbd2de68 Mon Sep 17 00:00:00 2001 From: emmanuel Date: Mon, 15 Mar 2021 14:25:01 +0300 Subject: [PATCH] ready for first test --- .phpunit.result.cache | 1 + config/config.php | 4 ++-- src/ApiUtil.php | 32 ++++++++++++++++++++++++++++++++ src/Bulksms.php | 31 ++++++++++++++++++++++++++++++- tests/SendSmsTest.php | 30 ++++++++++++++++++++++++++++++ 5 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 .phpunit.result.cache create mode 100644 tests/SendSmsTest.php diff --git a/.phpunit.result.cache b/.phpunit.result.cache new file mode 100644 index 0000000..48250a5 --- /dev/null +++ b/.phpunit.result.cache @@ -0,0 +1 @@ +C:37:"PHPUnit\Runner\DefaultTestResultCache":222:{a:2:{s:7:"defects";a:1:{s:43:"Epmnzava\Bulksms\Tests\SendSmsTest::sendSms";i:5;}s:5:"times";a:2:{s:48:"Epmnzava\Bulksms\Tests\ExampleTest::true_is_true";d:0.556;s:43:"Epmnzava\Bulksms\Tests\SendSmsTest::sendSms";d:1.787;}}} \ No newline at end of file diff --git a/config/config.php b/config/config.php index 8235311..00baae6 100644 --- a/config/config.php +++ b/config/config.php @@ -14,9 +14,9 @@ "sender_id"=>env("BULKSMS_SENDERID","INFO"), - "api_secret"=>env("BULKSMS_SECRET"), + "token_secret"=>env("BULKSMS_SECRET","ltTQfMQWrJF*cocCx2meq2u4kIbH_"), - "api_id"=>env("BULKSMS_ID"), + "token_id"=>env("BULKSMS_ID"), diff --git a/src/ApiUtil.php b/src/ApiUtil.php index 6979295..19de707 100644 --- a/src/ApiUtil.php +++ b/src/ApiUtil.php @@ -14,4 +14,36 @@ class ApiUtil { + /** + * @param $post_body + * @param $url + * @param $username + * @param $password + * @return array + * + */ + public function send_message ( $post_body, $url, $username, $password) { + $ch = curl_init( ); + $headers = array( + 'Content-Type:application/json', + 'Authorization:Basic '. base64_encode("$username:$password") + ); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + curl_setopt ( $ch, CURLOPT_URL, $url ); + curl_setopt ( $ch, CURLOPT_POST, 1 ); + curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 ); + curl_setopt ( $ch, CURLOPT_POSTFIELDS, $post_body ); + // Allow cUrl functions 20 seconds to execute + curl_setopt ( $ch, CURLOPT_TIMEOUT, 20 ); + // Wait 10 seconds while trying to connect + curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 10 ); + $output = array(); + $output['server_response'] = curl_exec( $ch ); + $curl_info = curl_getinfo( $ch ); + $output['http_status'] = $curl_info[ 'http_code' ]; + $output['error'] = curl_error($ch); + curl_close( $ch ); + return $output; + } + } diff --git a/src/Bulksms.php b/src/Bulksms.php index bf26e1e..9534fa1 100644 --- a/src/Bulksms.php +++ b/src/Bulksms.php @@ -15,6 +15,22 @@ class Bulksms { + protected static $statusMessages = array( + 0 => 'In progress', + 10 => 'Delivered upstream', + 11 => 'Delivered mobile', + 12 => 'Delivered upstream unacknowledged (presume in progress)', + 1 => 'Scheduled', + 22 => 'Internal fatal error', + 23 => 'Authentication error', + 24 => 'Data validation failed', + 25 => 'Insufficient credits', + 26 => 'Upstream credits not available', + 27 => 'Daily quota exceeded', + 28 => 'Upstream quota exceeded', + 40 => 'Temporarily unavailable', + 201 => 'Maximum batch size exceeded', + ); /** * @param $recepient * @param String $message @@ -23,9 +39,22 @@ class Bulksms * /messages?auto-unicode=true&longMessageMaxParts=30 * Functiont to send sms */ - public function sendMessage($recepient,String $message) + public function sendMessage($recepient,String $message) : Array { + $username = config('bulksms.token_id'); + $password = config('bulksms.secret'); + $messages = array( + array('to'=>$recepient, $message), + ); + + $api=new ApiUtil; + + $result=$api->send_message(json_encode($messages),config('bulksms.base_endpoint').'/messages?auto-unicode=true&longMessageMaxParts=30',$username,$password); + + return $result; + + } diff --git a/tests/SendSmsTest.php b/tests/SendSmsTest.php new file mode 100644 index 0000000..543a3db --- /dev/null +++ b/tests/SendSmsTest.php @@ -0,0 +1,30 @@ +sendMessage("255679079774","hellow man"); + + //$this->assertTrue(true); + } + +}