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

rifky49's avatar

Trying to login array(2) { ["accessToken"]=> string(223)

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,
        ];

        }
    }

0 likes
1 reply
Bogey's avatar

It seems like there are a couple of issues in the provided code snippet:

  1. The base URL in $this->baseUrl = config('https://bsms.hutch.lk/api/login'); appears to be incorrect. It should only contain the base URL, not the full endpoint. You should set it as follows:

    $this->baseUrl = 'https://bsms.hutch.lk/api/';
    

    Otherwise $url = $this->baseUrl . 'sendsms'; wouldn't make sense.

  2. some refactoring

    $ch = curl_init($this->baseUrl . 'login');
    
  3. Ensure that you've correctly configured the GuzzleHttp client with the base URI:

    $this->client = new Client(['base_uri' => $this->baseUrl]);
    

Please or to participate in this conversation.