I actually did successfully the send sms using nexmo.
Currently, I am needing now the receive sms feature for my GPS-tracking. I read this nexmo-inbound doc, I have question:
Is It Free Or Paid?
It is paid service, but new signups get a €2.00 credit to start. We do not require any billing information until you exhaust that initial credit. The €2.00 gives you more than enough to play around with many of the APIs, not just SMS.
Sending or receiving an SMS has a (normally) nominal cost, but it can vary depending on region, country, and telephone companies. For most countries it should be around €0.0057 a message.
Numbers are also rented monthly. This is around €0.90/month (again, depending on region of the telephone number and features).
How To Set This Up In Laravel
We do offer a Laravel package, which actually just had a new release to version 2.0.0 - nexmo/laravel
(https://github.com/nexmo/nexmo-laravel). This provides an easy way to get the client from the Service Container, and also sets up a Facade. You can also use nexmo/client
if you want to use it directly.
First off, we will need a controller. We'll create a controller that can send an SMS (as a demo), and then one to receive an SMS. I'm going to create SmsController
in app/Http/Controllers/SmsController.php
:
# app/Http/Controllers/SmsController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class SmsController extends Controller
{
public function send()
{
}
public function receive()
{
}
}
And then we will register those routes in routes/web.php
:
Route::get('/sms/send', 'SmsController@send');
Route::get('/sms/receive', 'SmsController@receive');
To send an SMS is fairly simple. You can grab our object out of the Service Container and send with a single call. We can change our class to this (replace TO_NUMBER
with whatever phone number you want to get the message):
class SmsController extends Controller
{
public function send()
{
$nexmo = $this->app->make(\Nexmo\Client::class);
$message= $nexmo->message()->send([
'to' => TO_NUMBER,
'from' => 'Acme Inc',
'text' => 'A text message sent using the Nexmo SMS API'
]);
$response = $message->getResponseData();
return response()->json($response);
}
public function receive()
{
}
}
If you access http://localhost/sms/send
, it should send an SMS message to whatever number you configured.
Receiving a text does not require our SDK, you just need to have an endpoint that can receive a request. First, let's change our receive()
method to work:
use Illuminate\Http\Request;
class SmsController extends Controller
{
public function send()
{
$nexmo = $this->app->make(\Nexmo\Client::class);
$message= $nexmo->message()->send([
'to' => TO_NUMBER,
'from' => 'Acme Inc',
'text' => 'A text message sent using the Nexmo SMS API'
]);
$response = $message->getResponseData();
return response()->json($response);
}
public function receive(Request $request)
{
$smsFrom = $request->query('msisdn');
$smsMessage = $request->query('text');
return response()->json(['from' => $smsFrom, 'message' => $smsMessage]);
}
}
You could hit https://localhost/sms/receive?msisdn=15555551234&text=Hello%20World
and you should get something similar to the following as output:
{
"from": "15555551234",
"message": "Hello World"
}
Now we need to tell Nexmo where to send SMS messages to. On the Nexmo Dashboard, go to "Numbers," then "Your Numbers." Under "Manage" click the gear icon. In the top textbox, enter a world reachable address (ie: localhost will not work), for example "http://example.com/sms/receive" or "http://example.ngrok.io/sms/receive", and click "OK" to save.
If you send a text message to your Nexmo number, it should try and hit the configured endpoint, and your application can handle the incoming data as needed.
If you do not have a test server that is reachable on the internet, we recommend ngrok. It is a free program that will expose your local development environment through a reachable address. We have a writeup here.
Full docs for the SMS system can be found at https://developer.nexmo.com/messaging/sms/overview. The PHP examples are technically in Slim, but use the SDK directly so there aren't any Slim-specific blocks in there.
You will need an account to use the system, so you can sign up at https://dashboard.nexmo.com/sign-up. You will get €2.00 to start with, which is more than enough to rent a number and start sending and receiving messages.