Download the PHP package anburocky3/msg91-php without Composer
On this page you can find all versions of the php package anburocky3/msg91-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download anburocky3/msg91-php
More information about anburocky3/msg91-php
Files in anburocky3/msg91-php
Package msg91-php
Short Description Send SMS seamlessly fast with the help of MSG91 Provider in PHP.
License MIT
Homepage https://github.com/anburocky3/msg91-php
Informations about the package msg91-php
Send SMS Seemlessy with MSG91
This library requires a minimum PHP version of 7.4
This is a PHP Client for Msg91 APIs. Before using it, please make sure you have an account on Msg91 and have an Authkey (Msg91 Dashboard > API > Configure).
NOTE: The project is under active development and so, some apis are subjected to change.
Table of Contents
- Installation
- Usage
- Create a Client
- Configuration
- OTP and SMS Services
- Examples
- Managing OTPs
- Send OTP
- Verify OTP
- Resend OTP
- Sending SMS
- Message Variables
- Receiver Key
- Handling Responses
- Managing OTPs
- Related
- Acknowledgements
Installation
The packages is available on Packagist and can be installed via Composer by executing following command in shell.
Usage
If you're using Composer, make sure the autoloader is included in your project's bootstrap file:
Now the client can be initialised by passing a configured object to the constructor.
The package in distributed under Anburocky3\Msg91
namespace which can used if your are working in a namespace environment.
Continue reading to learn more about configuration and available method on opt
and sms
services with examples.
Create a Client
The client is responsible for interacting with Msg91 apis.
Client can also be initialised without a configuration which can be set by calling setConfig($config)
method on the client instance.
You can also pass a custom GuzzleHttp\Client
as the second argument on the Client's constructor.
Configuration
The module is configurable to your specific needs where you need to set default options for APIs like the default OTP message format, retry method etc.
An example configuration might look something like this:
Following configuration options are available:
Option | Type | Description | Default Value |
---|---|---|---|
key | string | The authentication key for Msg91 apis (required) | null |
otp_message | ?string | Message template used when an OTP is sent. The ##OTP## placeholder is required | Your OTP is ##OTP## |
resend_otp_method | ?enum("text", "voice") | Default method when resending an OTP when previous attempt of OTP send/verification failed. It can take one of "text" or "voice" values. | text |
from | ?string | The default name for sender. This is used the From in messaging applications. It's value can only contain alphanumeric values. | null |
otp_length | ?number | Length of the generated OTP by Msg91 api when you are not generating OTPs on our end.. This can be between [4, 9] | 4 |
otp_expiry | ?number | Duration (in minutes) for which the OTP is valid. | 5 |
route | ?number | Route for SMS | null |
unicode | ?number | Set to this 1 if all your message contains unicode characters | 0 |
NOTE: Setting any if these values as null will override the default values to null too. And so, the default values from Msg91 APIs will be used. For example, setting the otp_message
to null
will let use "Your verification code is ##OTP##" which the default from APIS
OTP and SMS Services
After a client has been created, you can access otp
and sms
services to send OTPs and SMSs respectivily.
NOTE: Configuration must be set before using any other services on the client.
Next, follow along with examples to learn more
Examples
Managing OTPs
OTP services like sending, verifying, and resending etc, can be accessed via otp
method on the client instance e.g. $client->otp()
. All the parameters which are available for the Msg91 API, have a corresponding intuitive method name e.g. to set the digits in otp for the send OTP request, you call the digits
method on the service. You can create there via \Anburocky3\Msg91\Options
class. See following examples to learn more.
Send OTP
Basic Usage
Advanced Usage
Instead of relying on defaults from the Msg91 or the client, you can pass all the custom options that are accepted by Msg91 APIs using the options
helper on the service. This method accepts a close which will be called with underline \Anburocky3\Msg91\OTP\Options
instance and gives your the full flexibility to add any options that is required.
NOTE: If you are generating the OTP at your side, and passing it to the service, along with a custom message, you MUST include the ##OTP##
or actual value of OTP inside the message. Failing to do so will result in error
Verify OTP
As the verification does not send any messages, you just need to provide the required fields to verify the OTP e.g. the sent OTP and Phone Number only.
Resend OTP
To resend an OTP, access to otp
service and call the resend
with to resend the OTP. Method of communication can be
changed by calling viaVoice
or viaText
before sending the OTP. The default values can be set into the configuration
Sending SMS
To send SMS, access the SMSService
by calling ->sms()
method on the client instance
To add any more options to the message, you can call the options
method before sending the message. The options
method accepts a call which will receive a \Anburocky3\Msg91\SMS\Options
instance. Using it, you can modify any desired
option.
Message Variables
To send SMS, you create Flow where you can provide a template for your
SMS. This template may contain some variables which you use as placeholder
for per message data. You can use recipients
method to set the values for
these variables per recipient while sending SMS as follows.
Let's assume that you have create a Flow with following message template
We will to update the name
and date
variables per recipient.
The mobiles
key in the recipients array comes from the receiver field's value
which we set when creating a new Flow. It defaults to mobiles
. If you have provided
a different receiver key, please update the recipients key accordingly.
If you want to put same value for a variable for all recipients, you can use
variable
method as follows
Note: You should call the variable
method only after you have set the recipients.
Receiver Key
When creating a Flow on MSG91, you can create a custom receiver key which default to ##mobiles##
. This default is
being used in this package as well. If you have configured your flow with a different receiver key, you can set it
via receiverKey
method as follows:
Assuming you have set the receiver field to ##contact##
,
Handling Responses
All the services will return \Anburocky3\Msg91\Support\Response
instance for all successfully responses and will throw
exceptions if
\Anburocky3\Msg91\Exceptions\ValidationException
: request validation failed\Anburocky3\Msg91\Exceptions\ResponseErrorException
: there was an error in the response
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Credits
Major source code references have been taken from the following
- Craftsys
- Anbuselvan
- All Contributors
License
The MIT License (MIT). Please see License File for more information.