1.HutchSmaGateway.php
<?php
// app/Services/HutchSmsGateway.php
namespace App\Services;
use GuzzleHttp\Client;
class HutchSmsGateway
{
protected $client;
protected $baseUrl;
protected $accessToken;
public function __construct()
{
$this->client = new Client();
$this->baseUrl = config('https://bsms.hutch.lk/api/login'); // Remove duplicate URL and set only the base URL
$this->accessToken = $this->getAccessToken();
}
protected function getAccessToken()
{
echo "Trying to login\n";
$post_data = ["username" => "[email protected]", "password" => "####"];
$ch = curl_init('https://bsms.hutch.lk/api/login');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Accept: */*',
'X-API-VERSION: v1',
],
CURLOPT_POSTFIELDS => json_encode($post_data),
]);
$response = curl_exec($ch);
if ($response === false) {
throw new \Exception(curl_error($ch));
}
$responseData = json_decode($response, true);
curl_close($ch);
// Print the response data for debugging
var_dump($responseData);
// Ensure that the access token is set before returning
if (isset($responseData['accessToken'])) {
return $responseData['accessToken'];
} else {
// Handle the case when the access token is not present in the response
throw new \Exception("Error: Unable to retrieve access token.");
}
}
public function sendSms($campaignName, $mask, $numbers, $content, $deliveryReportRequest = false)
{
$url = $this->baseUrl . 'sendsms'; // Concatenate the base URL properly
$headers = [
'Content-Type' => 'application/json',
'Accept' => '*/*',
'X-API-VERSION' => 'v1',
'Authorization' => 'Bearer ' . $this->accessToken,
];
$data = [
'campaignName' => $campaignName,
'mask' => $mask,
'numbers' => $numbers,
'content' => $content,
'deliveryReportRequest' => $deliveryReportRequest,
];
}
}