Sending SMS via Cron Job PHP

Sending SMS via Cron Job PHP

In this post we will show you Sending SMS via Cron Job PHP, hear for Sending SMS via Cron Job PHP we will give you demo and example for implement.

Today I will show a trick to send SMS via your Linux server in the background by executing a PHP script file at a specific time using Cron Job. The primary requirement for this tutorial is a Linux server, an SMS package, and an ssh client software like putty.
I am assuming that you know how to config putty and know basic Linux commands for navigation etc.  before you proceed this tutorial. A Cron job is a Linux command for scheduling a task to be executed sometime in the future. This is normally used to schedule a job that is executed periodically - for example, to send out a notice every morning.
You can execute a PHP script file via crontab.
crontab -e
The above command will open a file in nano which contains all your Cron jobs.  To add a cronjob to execute your PHP script file add the following command at end of file.
45 07 * * * /usr/bin/php /var/www/html/sendsms.php
Save and exit nano.  The above command will execute your PHP script file by using PHP installed in usr/bin every day at 7:45 am.
Each line of a crontab file represents a job, and looks like this:
 ┌───────────── minute (0 - 59)
 │ ┌───────────── hour (0 - 23)
 │ │ ┌───────────── day of month (1 - 31)
 │ │ │ ┌───────────── month (1 - 12)
 │ │ │ │ ┌───────────── day of week (0 - 6) (Sunday to Saturday;
 │ │ │ │ │                                       7 is also Sunday)
 │ │ │ │ │
 │ │ │ │ │
 * * * * *  command to execute
To view all your  cronjob entries type
crontab -l
Now for sending SMS part. As you guys know you need a Bulk SMS subscription and their PHP API. One of the free SMS providers is way2sms. But they don't provide any official PHP library but still, you can use a library at your own risk by downloading from here. Note you are not legally permitted to use above library but since it is for learning purpose you can try it. 
I use ealerts4you.in SMS gateway. It is paid but fast and cheap. The code for sendsms.php file using ealerts API is given below.
<?php 
 
function httpGet($url)
{
    $ch = curl_init();  
         
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
         
    $output=curl_exec($ch);
         
    curl_close($ch);
    return $output;
}
  
$mobileNumber = ["mobile no 1","mobile no 2"];
$smstxt="Good Morning Have a Nice Day.";

foreach ($mobileNumber as $mobile)
{
 $data = array('uname' => 'sjcet',
      'password' => 'as provided by ealert',
      'sender' => 'as provided by ealert',
      'sms' => $smstxt,
      'receiver' => $mobile,
      'msgtype' => '1',
      'route' => 'TA',
      'routetype' => '1');
  
 $url="http://ealerts4you.in/httpapi/smsapi?" .http_build_query($data);

 httpGet($url);
}
If you need any help or have any suggestions then please comment below.
Hope this code and post will helped you for implement Sending SMS via Cron Job PHP. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve onlincode. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs us

Comments

  1. Get ready SMS PHP API or Bulk SMS API PHP Gateway Integration Source Code/Script to Integrate into your website, software or web application. MsgClub PHP APIs helps to send verification code or OTP, Transactional & Promotional SMS from your PHP based application automatically.

    ReplyDelete

Post a Comment