ETA Notifications with PHP and Laravel
Companies like Uber, TaskRabbit, and Instacart have built an entire industry around the fact that we, the customers, like to order things instantly, wherever we are.
The key to these services working is notifying customers instantly when things change. Customers appreciate being in the loop on order status, and (of course) like to know when something changes.
Uber relies on Twilio SMS to keep customers up to date on their ridesharing requests. Learn more here.
In this tutorial, we'll build a notification system for a fake on-demand laundy service Laundr.io with PHP and Laravel.
Let's get started! Click the below button to begin.
Trigger the Notifications
The delivery person's screen will have two buttons to update orders which are wired to the appropriate routes:
- Delivery person picks up laundry to be delivered (
- Delivery person is arriving at the customer's house (
The buttons will suffice for our demo app. In a production app you would likely use GPS to trigger
deliver, but the concept is the same.
Now let's look at how we set up the PHP Twilio REST Client.
Setting Up the Twilio REST Client
Here we create an authenticated Twilio REST API client that we can use anytime we need to send a text message.
We initialize the client with our Twilio account credentials stored as environment variables. You can find the Auth Token and Account SID in the console:
Next up, let's look at what we do with incoming notification triggers.
Handle Notification Triggers
OrderController we extract the phone number stored in each order, and then simply send an SMS message with an appropriate message body. Easy!
Next, let's look closer at how we send the SMS itself.
Send a SMS (or MMS) To the Customer
This code shows how we send the SMS with the Twilio PHP Client.
Better shown than said? We agree - you can add an image of the order with the optional
'mediaUrl' => 'http://lorempixel.com/image_output/fashion-q-c-640-480-1.jpg'
In addition to the required parameters (and optional
mediaUrl), we can pass a
statusCallback url to let us know if the message was delivered.
The message delivery status updates are interesting. Let's zoom in on those next.
Handling a Twilio Status Callback
Twilio will make a post request to this controller each time our message status changes to one of the following:
We then update this
notification_status on the
Order and your own business logic would take it from there. This is an excellent place to add logic that would resend the message in the case of a message failure, or send out an automated survey when there is a successful delivery to the customer.
That's a wrap (a fold?)!
We've just implemented an on-demand notification service that alerts our customers when their laundry order is arriving. Let's take a look at other features that are easy to add with Twilio.
Where to next?
We've got a lot of great PHP content here on the Docs site but we've selected just a couple you might like to visit next:
Increase your rate of response by automating the workflows that are key to your business. In this tutorial, learn how to build a ready-for-scale automated SMS workflow for a vacation rental company.
Protect your users' privacy by anonymously connecting them with Twilio Voice and SMS. Learn how to create disposable phone numbers on-demand so two users can communicate without exchanging personal information.
Did this help?
Thanks for checking this tutorial out! Let us know what you've built - or what you're building - on Twitter.
Need some help?
We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.