How to Send SMS with Twilio in PHP and Run it as a Cron Job


In this post, I will show you how to use Twilio’s PHP SDK to send SMS messages. You’ll also learn how to automate the process by setting up a cron job that runs the PHP script at regular intervals.

Whether you want to notify users or send administrative alerts, integrating Twilio SMS in PHP is a great solution. By the end of this tutorial, you will have a working PHP script that sends an SMS, and we’ll automate it using cron jobs.

What You Will Learn:

  1. Installing and using Twilio SDK without Composer
  2. Writing a PHP script to send SMS
  3. Setting up a cron job to run the script automatically

Step 1: Download the Twilio SDK

The first step is to download the Twilio PHP SDK. If you prefer not to use Composer, you can manually download the SDK from Twilio’s GitHub repository.

  1. Visit the Twilio PHP SDK GitHub page.
  2. Download the SDK as a ZIP file and extract it into your project directory.

You should now have a folder called twilio/src/ in your project directory, which contains the SDK files.

Step 2: Write the PHP Script to Send SMS

Now, let’s create the PHP script that will use the Twilio SDK to send an SMS. Here’s a step-by-step guide:

  1. Create a PHP file named send_test_sms.php.
  2. Paste the following code into the file:

Key Components of the Code:

  • Twilio Credentials: Replace 'your_twilio_sid', 'your_twilio_auth_token', and '+your_twilio_number' with your Twilio account details.
  • Admin Phone: Set the '$admin_phone' variable to the phone number that should receive the test SMS.
  • Message: You can customize the $message variable with the content of your SMS.

Save the file once you have updated the details.

Step 3: Test the PHP Script

Before setting up the cron job, it’s important to test that the PHP script works correctly.

1.Run the script locally or on your server:

php /path/to/send_test_sms.php

2.Check your phone for the test message. If everything is set up correctly, you should receive an SMS from the Twilio number.

Step 4: Set Up a Cron Job

Once the script works, you can automate the SMS sending by scheduling it with a cron job. Cron jobs allow you to run the script at regular intervals, such as hourly, daily, or weekly.

How to Set Up the Cron Job:

1.Open the Crontab: Use the following command to edit your server’s crontab file:

crontab -e

2.Add a Cron Job: Add a new entry to run the PHP script. For example, to run the script every hour, add this line:

0 * * * * /usr/bin/php /path/to/send_test_sms.php

Replace /usr/bin/php with the actual path to your PHP executable, and /path/to/send_test_sms.php with the actual path to your script.

Cron Job Timing:

  • 0: Minute (the 0th minute of the hour).
  • *: Every hour.
  • *: Every day of the month.
  • *: Every month.
  • *: Every day of the week.

If you want to run it every 10 minutes, for example, use:

*/10 * * * * /usr/bin/php /path/to/send_test_sms.php

3.Save and Exit: Once you’ve added the cron job, save and exit the crontab editor.

Conclusion

You’ve now created a PHP script that sends SMS messages via Twilio and set it to run automatically using a cron job. This is a powerful way to automate SMS notifications for various purposes, such as alerting admins or customers.

By following this guide, you’ve learned:

  • How to download and include the Twilio SDK without Composer.
  • How to write a PHP script to send SMS messages.
  • How to automate SMS sending with cron jobs.

Pro Tip: Ensure you keep your Twilio credentials secure and consider implementing error handling to track issues that might arise when sending messages.

Frequently Asked Questions

1. Can I use this method to send SMS to multiple recipients? Yes, simply modify the PHP script to loop through an array of phone numbers and send the message to each recipient.

2. How do I check if the cron job is working? You can check the cron job logs on your server using the following command:

grep CRON /var/log/syslog

3. How can I customize the message content? You can dynamically generate the message content by using variables in the $message string. For example, you could include order details if you’re integrating this with WooCommerce.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *