Viewing file: Cheapglobalsms.php (3.86 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 Cheapglobalsms
{
private $credentials;
private $to_numbers;
private $body;
private $from_number;
private $mms_files;
private $message;
private $send_fail=[];
public function __construct($credentials)
{
$this->credentials=$credentials;
}
public function setTo($to)
{
$this->to_numbers = $to;
return $this;
}
public function setBody($body)
{
$this->body = $body;
return $this;
}
public function setFrom($from)
{
$this->from_number = $from;
return $this;
}
public function setFiles($files){
$this->mms_files=$files;
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()
{
try {
if (!isset($this->credentials->cheap_global_account) || !isset($this->credentials->cheap_global_password)){
throw new \Exception('This provider is not active. Please contact with the administrator');
}
foreach ($this->to_numbers as $number){
if ($this->sms_type && $this->sms_type=='inbox') {
$type = 'inbox';
}else{
$type = 'sent';
}
if($this->queue){
$this->message_log=MessageLog::updateOrCreate([
'to'=>$number,
'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'=>$number,
'from'=>$this->from_number,
'body'=>$this->body,
'message_id'=>$this->message->id,
'customer_id'=>$this->message->customer_id,
'type'=> $type,
'status'=>'succeed'
]);
}
}
$post_data = array(
'sub_account' => "$this->credentials->cheap_global_account",
'sub_account_pass' => "$this->credentials->cheap_global_password",
'action' => 'send_sms',
'sender_id' => 'CGSMS',
'recipients' => "$this->to_numbers",
'message' => "$this->body"
);
$api_url = 'http://cheapglobalsms.com/api_v1';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_POST, true);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($response_code != 200) $response = curl_error($ch);
curl_close($ch);
} catch (\Exception $ex) {
Log::error($ex);
}
}
public function getSendFail(){
return $this->send_fail;
}
private function setSendFail($sendFailArray){
$this->send_fail[]=$sendFailArray;
}
}
|