-2

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',
    ]
]);
SafwanA
  • 85
  • 1
  • 6
tinox
  • 87
  • 1
  • 11
  • 1
    check the credential again which you are passing. like uname and password – fahim152 Nov 19 '19 at 08:59
  • 1
    This require basic authorization as per the document please send the authorization header with basic credentials – Dhaval Purohit Nov 19 '19 at 09:04
  • 1
    @DhavalPurohit — The error message is linking to "authentication-failed" (Verification of credentials failed) not "authentication-required" (No credentials were provided). – Quentin Nov 19 '19 at 09:05
  • 1
    @Quentin credential are correct! – tinox Nov 19 '19 at 09:42
  • have you tried with header Authorization? – Dhaval Purohit Nov 19 '19 at 11:29
  • @DhavalPurohit how can you please guide to do that plz, thanks! – tinox Nov 19 '19 at 13:07
  • `$result = $client->request('POST','https://api.bulksms.com/v1/messages', [ 'form_params' => [ 'sender' => 'my appname', 'recipients' => '+5555555555', 'message' => 'Testing message', ], 'auth' => ['username', 'password'] ]);` – Dhaval Purohit Nov 19 '19 at 13:09
  • Like this one. @tinox – Dhaval Purohit Nov 19 '19 at 13:11
  • @DhavalPurohit thanks for help, somethings changed but still have some errors, Client error: `POST https://www.bulksms.com/` resulted in a `405 Not Allowed` response: – tinox Nov 19 '19 at 13:20
  • try this one. $result = $client->request('POST','https://api.bulksms.com/v1/messages', [ 'json' => [ "to"=>"+91xxxx","body"=>"message body" ], 'auth' => ['username', 'password'] ]); – Dhaval Purohit Nov 19 '19 at 13:57
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/202652/discussion-between-tinox-and-dhaval-purohit). – tinox Nov 19 '19 at 14:30
  • @DhavalPurohit now am getting this error Client error: `POST api.bulksms.com/v1/messages` resulted in a `400 Bad Request` response: { "type" : "https://developer.bulksms.com/json/v1/errors#bad-request", "title" : "Bad Request", "status" : 400 – tinox Nov 19 '19 at 14:44

2 Answers2

1

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?

PaoloC
  • 519
  • 5
  • 5
  • How? please help am new to this Curl stuff! can you provide sample code please! @PaoloC – tinox Nov 20 '19 at 08:56
  • this is the error I am getting right now Client error: `POST https://api.bulksms.com/v1/messages/` resulted in a `400 Bad Request` response: { "type" : "https://developer.bulksms.com/json/v1/errors#bad-request", "title" : "Bad Request", "status" : 400, (truncated...) – tinox Nov 20 '19 at 09:16
  • my code $result = $client->request('POST','/v1/messages/', [ 'json' => [ 'from' => 'App name', 'type'=> 'string', 'title'=> 'string', 'status'=> 0, 'detail'=> 'string', 'to' => '+101221', 'body' => 'Testing message', 'Content-Type'=>'application/json', 'Authorization'=> 'Basic '. base64_encode("$username:$password") ], 'auth' => ['username', '*****', ] ]); – tinox Nov 20 '19 at 09:59
0

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

SafwanA
  • 85
  • 1
  • 6