Trying to use bulksms.com API using guzzle in Laravel but returning errors

1.1k views Asked by At

I have been trying to use guzzle for sending bulk sms from bulksms.com and it is returning this error,

guzzlehttp\exception\clientexception client error: post https://api.bulksms.com/v1/messages resulted in a 401 full authentication is required to access this resource response: : "type" "https://developer.bulksms.com/json/v1/errors#authentication-failed

My code

$client = new Client([
    'base_uri'=>'https://www.bulksms.com/',
    'timeout'=>'900.0'
]); 

//$result = $client->post('', [
//    'form_params' => [
//        'sample-form-data' => 'value'
//    ]
//]);

$result = $client->request('POST','https://api.bulksms.com/v1/messages', [
    'form_params' => [
        'username' => 'username',
        'password' => '****',
        'sender' => 'my appname',
        'recipients' => '+5555555555',
        'message' => 'Testing message',
    ]
]);
2

There are 2 answers

3
PaoloC On BEST ANSWER

Other people have already pointed you towards using authentication correctly, and using JSON as the format of your request. Additionally, you're using the wrong variable names. For example, the documentation uses the variable name to, and you have used recipients instead (maybe you copied and pasted that code from somewhere else?).

The documentation has a PHP code sample the uses curl, at: https://www.bulksms.com/developer/json/v1/#tag/Message - why not use that as a basis, and then convert it to a working Guzzle request, as a starting point?

0
SafwanA On

Did you have a look at the Authentication section in the API docs? You should authenticate with the API using HTTP Basic Auth.