phplaravelsmsguzzleinfobip-api

Infobip single sms service using guzzle in laravel


I am trying to send sms with my laravel application. I have infobip testing account and using that details to sms sms but getting this error:

ClientException in RequestException.php line 111:
Client error: `POST https://api.infobip.com/sms/1/text/single` resulted in a        `401 Unauthorized` response:
{"requestError":{"serviceException":   {"messageId":"UNAUTHORIZED","text":"Invalid login details"}}}

CODE:

$data= '{  
       "from":"InfoSMS",
       "to":"923227124444",
       "text":"Test SMS."
    }';
    $userstring = 'myuserame:password';
    $id =base64_encode ( 'myuserame:password' );
    echo 'Basic '.$id;
    $client = new \GuzzleHttp\Client();
    $request = $client->post('https://api.infobip.com/sms/1/text/single',array(
            'content-type' => 'application/json'
            ),['auth' =>  ['myusername', 'password']]);
    $request->setHeaders(array(
      'accept' => 'application/json',

      'authorization' => 'Basic '.$id,
      'content-type' => 'application/json'
    ));
    $request->setBody($data); #set body!
    $response = $request->send();
    echo $res->getStatusCode(); // 200
    echo $res->getBody();
    return $response;

The username and pssword are right as I tried to send direct text message from the site and its working there.

Can anyone help me with what am I doing wrong?

Thanks!


Solution

  • So I tired solving the problem using curl and I got it working placing code for others. Code:

    $data_json = '{
           "from":"Infobip",
           "to":"9232271274444",
           "text":"test msg."
        }';
        $authorization = base64_encode('username:password');
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Accept: application/json',"Authorization: Basic $authorization"));
        //curl_setopt($ch, CURLINFO_HEADER_OUT, true);
        curl_setopt($ch, CURLOPT_URL, 'https://api.infobip.com/sms/1/text/single');
    
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response  = curl_exec($ch);
        //var_dump(curl_getinfo($ch));
        var_dump($response);
        curl_close($ch);*/