Viewing file: OneStwoU.php (5.99 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
namespace App\SmsProvider;
use App\Models\MessageLog; use Illuminate\Support\Facades\Log;
class OneStwoU { private $user_name; private $password; private $mt = '0'; private $fl = '0'; private $Sid = 'test'; private $send_fail=[];
private $url = 'https://api.1s2u.io/bulksms'; private $message_log; private $from_number; private $to_array; private $seven_bit_msg; private $message;
public function __construct($username, $password, $ones_two_u_status) { $this->user_name = $username; $this->password = $password; $this->ones_two_u_status = $ones_two_u_status; }
public function setTo($to) { $this->to_array = $to; return $this; } public function setFrom($from) { $this->from_number = $from; return $this; }
public function setBody($body) { $this->seven_bit_msg = $body; $this->body = $body; return $this; } public function setMessage($message){ $this->message=$message; return $this; } public function setQueue($queue){ $this->queue=$queue; return $this; } public function setType($response){ $this->sms_type=$response; return $this; }
public function send() { if (!isset($this->ones_two_u_status) || $this->ones_two_u_status !='active'){ throw new \Exception('This provider is not active. Please contact with the administrator'); }
foreach ($this->to_array as $to) {
try{ if ($this->sms_type && $this->sms_type=='inbox') { $type = 'inbox'; }else{ $type = 'sent'; }
if($this->queue){ $this->message_log=MessageLog::updateOrCreate([ 'to'=>$to, 'from'=>$this->from_number, 'body'=>$this->body, 'message_id'=>$this->message->id, 'customer_id'=>$this->message->customer_id, 'type'=>'sent', ],['status'=>'succeed','queue_id'=>$this->queue->id]); }else{ $this->message_log=MessageLog::create([ 'to'=>$to, 'from'=>$this->from_number, 'body'=>$this->body, 'message_id'=>$this->message->id, 'customer_id'=>$this->message->customer_id, 'type'=> $type, 'status'=>'succeed' ]); } $post_fields = array( 'username' => $this->user_name, 'password' => $this->password, 'mt' => $this->mt, 'fl' => $this->fl, 'Sid' => $this->Sid, 'mno' => $to, 'msg' => $this->seven_bit_msg ); $get_url = $this->url.'?'.http_build_query($post_fields);
$result = $this->send_message($post_fields, $get_url); if (!$result['success']) { if(isset($result['details'])) { throw new \Exception($result['details']); }else{ throw new \Exception('Something went wrong, try again after sometimes'); } } }catch (\Exception $ex){ Log::error($ex->getMessage()); if($this->message_log) { $this->message_log->update(['status' => 'failed']); } if ($type && $type=='sent') { $this->setSendFail([ 'message_id' => $this->message->id, 'from_number' =>$this->from_number, 'to_number' => $to, 'created_at' => now(), 'updated_at' => now(), 'reason' => $ex->getMessage(), ]); } }
} }
function send_message($post_body, $get_url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $get_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Allowing cUrl funtions 20 second to execute curl_setopt($ch, CURLOPT_TIMEOUT, 20); // Waiting 20 seconds while trying to connect curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $response_string = curl_exec($ch); $curl_info = curl_getinfo($ch); $sms_result = array(); $sms_result['success'] = 0; $sms_result['details'] = ''; $sms_result['http_status_code'] = $curl_info['http_code']; $sms_result['api_status_code'] = ''; $sms_result['id'] = $response_string; if ($response_string == FALSE) { $sms_result['details'] .= "cURL error: " . curl_error($ch) . "\n"; } elseif ($curl_info['http_code'] != 200) { $sms_result['details'] .= "Error: non-200 HTTP status code: " . $curl_info['http_code'] . "\n"; } else { $sms_result['details'] .= "Response from server: $response_string\n"; $api_result = substr($response_string, 0, 2); $status_code = $api_result; $sms_result['api_status_code'] = $status_code; if ($api_result != 'OK') { $sms_result['details'] .= "Error: could not parse valid return data from server.\n" . $api_result; } else { if ($status_code == 'OK') { $sms_result['success'] = 1; } } } curl_close($ch); return $sms_result; }
public function getSendFail(){ return $this->send_fail; }
private function setSendFail($sendFailArray){ $this->send_fail[]=$sendFailArray; } }
|