Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

maxxxabro's avatar

Laravel API Problem - sms gateway

01 - Code - Controller

case 'SMS':

                    $clphone = str_replace(" ", "", $this->cl_phone); #Remove any whitespace
                    $clphone = str_replace('+', '', $clphone);


                    if (strlen($clphone) > 11) {
                        $client_phone = ltrim($clphone, '94');
                    } else {
                        $client_phone = $clphone;
                    }

                    $message   = urlencode($this->message);
                    $sms_url   = rtrim($gateway_url, '/');
                    $sender_id = urlencode($this->sender_id);
                    $msgcount  = strlen(preg_replace('/\s+/', ' ', trim($message)));

                    if ($msgcount < 161) {
                        $api_key = $gateway_user_name;
                    } else {
                        $api_key = $gateway_password;
                    }

                    try {

                        $sms_sent_to_user = "$sms_url" . "?q=$api_key" . "&from=$sender_id" . "&destination=$client_phone" . "&message=" . $message;

                        $ch = curl_init();

                        curl_setopt($ch, CURLOPT_URL, $sms_sent_to_user);
                        curl_setopt($ch, CURLOPT_HTTPGET, 1);
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

                        $get_sms_status = curl_exec($ch);
                        curl_close($ch);

                        if ($get_sms_status == 0) {
                            $get_sms_status = 'Success';
                        }

                    } catch (\Exception $e) {
                        $get_sms_status = $e->getMessage();
                    }

                    break;

02 - code - PEO PHP Code

$now = date("Y-m-d\TH:i:s"); 
$username = "username"; 
$password = "password"; 
$digest=md5($password); 
$body = '{ 
"messages": [ 

{ 
"clientRef": "79425695", 
"number": "9212345678", 
"mask": "Test", 
"text": "This is another test message", 
"campaignName":"USMSMI16" 
} 
] 
}';
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,"/api/sms/send"); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS,$body); //Post Fields 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

$headers = ['Content-Type: application/json', 'USER: '.$username, 'DIGEST: '.$digest, 'CREATED: '.$now];

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
$server_output = curl_exec ($ch); 
curl_close ($ch); 

How to I applied 02 codes to 01 codes?

0 likes
0 replies

Please or to participate in this conversation.