How to schedule an SMS in PHP

The simplest way to send SMS from PHP is to use the built in HTTP/Rest SMS api of Ozeki SMS Gateway. When you use this API, you will send SMS messages by issuing a HTTP Post request to the SMS gateway. The HTTP Post request will contain a message formatted in json format. The SMS gateway will send this SMS to the recipient phone, and it will return a HTTP 200 OK response to your request.

how to schedule an sms in php
Figure 1 - How to schedule an SMS in PHP

PHP code to send sms to mobile

The PHP sms code sample below demonstrates how you can send SMS using the http rest sms api of Ozeki SMS Gateway using the PHP Ozeki.Libs.Rest library. This library is provided to you free of charge, and you may use it and modify it in any of your projects.

SendSMS.cs
namespace Ozeki_PHP_Rest
{
require 'MessageApi/MessageApi.php';

		$configuration = new Configuration();
		
		$configuration -> Username = "http_user";
		$configuration -> Password = "qwe123";
		$configuration -> ApiUrl = "http://127.0.0.1:9509/api";
		
		$msg = new Message();
		
		$msg -> ToAddress = "+36201111111";
		$msg -> Text = "Hello, World!";
		$msg -> TimeToSend = "2021-06-23 16:18:00";
			
		$api = new MessageApi($configuration);
		
		$result = $api -> SendSingle($msg);	
		
		echo strval($result);
}		

How to use the PHP sms example:

This PHP sms example can be used in any PHP application. To use it, you must add the MessageApi folder to your project. You can use the Message class to create the SMS and you can use the MessageApi class to send the SMS to the SMS gateway. The SMS gateway will forward your message to the mobile network either through a wireless connection or through the Internet.

Download Send-schedule-SMS.php

The source code explained in this article can be downloaded and used and modified free of charge.
Download: Send-schedule-SMS.zip (5.9Kb)

What is in the Send-schedule-SMS.zip file?

The SendSMS.zip file contains the Ozeki.Libs.Rest.Php library, which gives you all the tools necessary to send and receive SMS messages. You will also find the SendScheduleSms project in the zip, which contains the example code to show you how to send an SMS. This example code is listed below.

Figure 2 - What is inside Send-schedule-SMS.zip

How to send SMS from PHP (Quick steps)

To send sms from PHP:

  1. Install Ozeki SMS Gateway
  2. Connect Ozeki SMS Gateway to the mobile network
  3. Send a test sms from Ozeki GUI
  4. Create a HTTP sms api user
  5. Start Wamp server
  6. Download the example above
  7. Create the SMS by creating a new Message object
  8. Use the SendSingle method to send your message
  9. Read the HTTP response
  10. Check the logs in the SMS gateway

Install Ozeki SMS Gateway and create an HTTP API user

To be able to send SMS from PHP, first you need to install Ozeki SMS Gateway. The SMS gateway can be installed on the same computer, where you develop your PHP code. After installation, the next step is to connect Ozeki SMS Gateway to the mobile network. You can send a test sms from the Ozeki GUI to verify, that your mobile network connection works. The final step to prepare your environment is to create a HTTP sms api user. Create a user with a username of "http_user", and with a password of "qwe123" to make the example work without modification.

After the environment is setup, you can run your PHP code.

HTTP API url to use send sms from PHP

To send SMS from PHP, your PHP will have to issue an HTTP request to the SMS gateway. The API url is shown below. Note that the IP address (127.0.0.1) should be replaced to the IP address of your SMS gateway. If Ozeki SMS Gateway is installed on the same computer where the C# sms application is running, this can be 127.0.0.1. If it is installed on a different computer, it should be the IP address of that computer.

http://127.0.0.1:9509/api?action=rest

HTTP request header to send SMS from PHP

To send the SMS messages, you need to include the following lines as headers in the HTTP request. Note that we include a content type and an Authorization header.

Content-Type: application/json
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==

HTTP request to send SMS from PHP

To submit the SMS, your PHP application will send an HTTP request similar to the one below. Note, that this request contains a HTTP header part and a http body part. The HTTP body is a JSON encoded data string. It contains the recipient's number and the message's text.

HTTP request received from 192.168.0.113:57045
POST /api?action=sendmsg HTTP/1.1
Content-Length: 433
Content-Type: application/json
Accept: */*
Authorization: Basic aHR0cF91c2VyOnF3ZTEyMw==
Host: 192.168.0.113:9509

{
  "messages": [
    {
      "message_id": "75ce6646-0a84-48d6-996a-02b632eef83a",
      "from_connection": null,
      "from_address": null,
      "from_station": null,
      "to_connection": null,
      "to_address": "+36201111111",
      "to_station": null,
      "text": "Hello, World!",
      "create_date": "2021-06-16 15:48:03",
      "valid_until": "2021-06-23 15:48:03",
      "time_to_send": "2021-06-23 16:18:00",
      "submit_report_requested": true,
      "delivery_report_requested": true,
      "view_report_requested": true,
      "tags": []
    }
  ]
}

HTTP response received by the PHP sms example

Once the SMS gateway receives this request, it will generate a HTTP response. The HTTP response will contain a status code, to indicate whether the SMS submit request was successful or not. It will also return a JSON encoded structure to provide you useful details about the message's submission.

HTTP/1.1 200 OK
User-Agent: OZEKI 10.3.118 (www.myozeki.com)
Content-Type: application/json; charset=utf8
Last-Modified: Mon, 14 Jun 2021 07:43:45 GMT
Server: 10/10.3.118
Transfer-Encoding: chunked

{
  "http_code": 200,
  "response_code": "SUCCESS",
  "response_msg": "Messages queued for delivery.",
  "data": {
    "total_count": 1,
    "success_count": 1,
    "failed_count": 0,
    "messages": [
      {
        "message_id": "38585421-3260-4bf9-9daa-2b39ec16961a",
        "from_station": "%",
        "to_address": "+36201111111",
        "to_station": "%",
        "text": "Hello, World!",
        "create_date": "2021-06-16 15:48:03",
        "valid_until": "2021-06-16 15:48:03",
        "time_to_send": "2021-06-23 16:18:00",
        "submit_report_requested": true,
        "delivery_report_requested": true,
        "view_report_requested": false,
        "tags": [
          {
            "name": "Type",
            "value": "SMS:TEXT"
          }
        ],
        "status": "SUCCESS"
      }
    ]
  }
}

How to send SMS from PHP using the PHP sms api (Video tutorial)

This video shows you how to download and use the SendSMS php project. Once you added the needed files to your project, you might notice that a there is a file called MessageApi.php. This is the file that contains the MessageApi and all the stuffs you need to send an SMS using PHP.

Video 1 - How to download and run the example project (Video tutorial)

PHP SMS example: Send_Message.php

Figure 3 - Send_Message.php

How to check that the SMS has been accepted by the HTTP user

After the SMS has been submitted, it is a good idea to check your SMS gateway, to see what it has received. You can check the log by opening the HTTP user's details from the Ozeki SMS Gateway management console. The following video shows you what to look for.

Video 2 - Sending SMS with the PHP code above (Video tutorial)

How to check that the SMS has been sent to the mobile network

The final step in verifying the procedure is to take a look at the logs of the mobile network connection. You might have to turn on logging in the configuration of the connection before you send the message to see the logs. If logging is enabled, you will see the phone number and the text of the message you have sent.

To sum it up

From this guide, you can get the knowledge to send scheduled SMS messages with the HTTP REST SMS API of the Ozeki SMS Gateway from PHP. Using this PHP SMS API helps you to stick to your schedule and run your business much more professionally. This service is easy to set up, comes with the full source code and you can use it and modify it without any limitations.

The information you have obtained is valuable, let's collect more! Ozeki has many other articles to help you, continue with the GitHub PHP SMS API tutorial.

Now your next thing to do is to download Ozeki SMS Gateway and configure this solution immediately!

More information