TwilioMatthew SetterTutorial
How to Track Opt-Outs in PHP
Summary
This tutorial demonstrates building a PHP application that records customer opt-out preferences from SMS replies using Twilio’s Incoming Message Webhook and Event Streams, which require PHP 8.5, Composer, and SQLite. When the SMS body is an opt-out keyword such as STOP, START, or HELP, Twilio includes an OptOutType parameter in the webhook request, which the application checks using strcasecmp and array_first. The application, based on the Twilio Slim Base project, stores the opt-out status and account SID in an SQLite database via PHP-DB and laminas-servicemanager, using a TableGateway for the user_optouts table. The handleWebhook() function validates the OptOutType against class constants (OPT_OUT_TYPE_START, OPT_OUT_TYPE_STOP, OPT_OUT_TYPE_HELP) and inserts a record before returning a TwiML response. Twilio already blocks further messages for standard opt-outs, but local recording enables advanced actions like syncing a CRM; the tutorial covers bootstrapping, configuring Twilio messaging services with Advanced Opt-Out, using ngrok, and verifying with an SQL query.
Context
Tracking customer opt-outs from SMS communications is essential for compliance with privacy laws like the Australian Spam Act and CAN-SPAM Act. Twilio provides opt-out detection via webhooks and event streams, but recording preferences locally allows for advanced actions such as CRM synchronization.
Approach / What changed
Build a PHP application based on the Twilio Slim Base project. Update the handleWebhook() function to extract the OptOutType parameter from incoming webhook or event stream requests using strcasecmp and array_first. Validate against class constants (OPT_OUT_TYPE_START, OPT_OUT_TYPE_STOP, OPT_OUT_TYPE_HELP) and insert a record into an SQLite database via PHP-DB and laminas-servicemanager. Configure Twilio messaging service to use the webhook URL and enable Advanced Opt-Out. Optionally configure Event Streams for the inbound message received event. Test by sending an SMS with an opt-out keyword and verify the database record.
Takeaways
Twilio’s Incoming Message Webhook and Event Streams include an OptOutType parameter when the SMS body is an opt-out keyword like STOP, START, or HELP.
Recording opt-out preferences locally in a database allows advanced actions such as keeping a CRM in sync, beyond Twilio’s built-in blocking.
The application uses PHP-DB with laminas-servicemanager to store opt-out records in an SQLite table, and the handleWebhook() function validates the OptOutType against class constants.
FAQ
How does Twilio indicate an opt-out in webhook requests?
Twilio includes an OptOutType parameter in the request body when the SMS body is an opt-out keyword such as STOP, START, or HELP. The application checks for this parameter using strcasecmp and array_first.
What are the allowed opt-out keywords in this tutorial?
The tutorial defines three constants: OPT_OUT_TYPE_START, OPT_OUT_TYPE_STOP, and OPT_OUT_TYPE_HELP, corresponding to the keywords START, STOP, and HELP. The list can be expanded as needed.
Why record opt-outs locally when Twilio already blocks messages?
Twilio blocks further messages for standard opt-outs, but local recording enables more advanced actions like syncing a CRM or applying custom business logic based on opt-out preferences.
Source and freshness
- Source
- twilio.com
- Published
- Added to Yomu
Summaries are generated from the original by a language model. Check the original before relying on it.
Keep reading
Related reading
The Good Stuff
Paid picks we use ourselves. Links carry a referral code — a subscription through one earns us a commission at no extra cost to you.
The best engineering reads, once a week
One useful email. Unsubscribe anytime.