How to send multiple sms from PHP

The most simple 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 send multiple sms from php
Figure 1 - How to send multiple sms from 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 Gateawy 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"; 	
		$msg1 = new Message();
		$msg2 = new Message();
		
		$msg1 -> ToAddress = "+36201111111";
		$msg1 -> Text = "Hello, World 1!";
		$msg2 -> ToAddress = "+36202222222";
		$msg2 -> Text = "Hello, World 2!";
			
		$api = new MessageApi($configuration);
		
		$result = $api -> SendMultiple([$msg1,$msg2]);	
		
		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-multiple-SMS.php

The source code explained in this article can be downloaded and used and modified free of charge.
Download: Send_multiple_SMS.zip (3.62Kb)

What is in the Send-multiple-SMS.js.zip file?

The Send-multiple-SMS.zip file contains the Ozeki.Libs.Rest library, which gives you all the tools neccessary to send and receive SMS messages. You will also find a SendMultipleSms file in the project, which contains the example code to show you how to send an SMS. This example code is listed below.

example project to send multiple sms with php
Figure 2 - Send-multiple-SMS directory

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 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 requst contains a HTTP header part and a http body part. The HTTP body is a JSON encoded data string. It contains the recipient number and the messages text.

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

{
  "messages": [
    {
      "message_id": "973c88f2-ae30-4b46-a4cd-e497151d38f3",
      "from_connection": null,
      "from_address": null,
      "from_station": null,
      "to_connection": null,
      "to_address": "+36201111111",
      "to_station": null,
      "text": "Hello, World 1!",
      "create_date": "2021-06-16 12:18:04",
      "valid_until": "2021-06-23 12:18:04",
      "time_to_send": "2021-06-16 12:18:04",
      "submit_report_requested": true,
      "delivery_report_requested": true,
      "view_report_requested": true,
      "tags": []
    },
    {
      "message_id": "dd910a4f-9172-4f35-9231-8dc83a5fc401",
      "from_connection": null,
      "from_address": null,
      "from_station": null,
      "to_connection": null,
      "to_address": "+36202222222",
      "to_station": null,
      "text": "Hello, World 2!",
      "create_date": "2021-06-16 12:18:04",
      "valid_until": "2021-06-23 12:18:04",
      "time_to_send": "2021-06-16 12:18:04",
      "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 messages submission.

HTTP/1.1 200 OK
User-Agent: OZEKI 10.3.116 (www.myozeki.com)
Content-Type: application/json; charset=utf8
Last-Modified: Wed, 16 Jun 2021 10:04:01 GMT
Server: 10/10.3.116
Transfer-Encoding: chunked

{
  "http_code": 200,
  "response_code": "SUCCESS",
  "response_msg": "Messages queued for delivery.",
  "data": {
    "total_count": 2,
    "success_count": 2,
    "failed_count": 0,
	"messages": [
      {
        "message_id": "63d2303d-9add-44d3-afd6-f27d6b6966ba",
        "from_station": "%",
        "to_address": "+36201111111",
        "to_station": "%",
        "text": "Hello, World 1!",
        "create_date": "2021-06-16 12:18:04",
        "valid_until": "2021-06-23 12:18:04",
        "time_to_send": "2021-06-16 12:18:04",
        "submit_report_requested": true,
        "delivery_report_requested": true,
        "view_report_requested": false,
        "tags": [
          {
            "name": "Type",
            "value": "SMS:TEXT"
          }
        ],
        "status": "SUCCESS"
      },
      {
        "message_id": "f393dd6c-6d6c-4b87-b069-78166a25b894",
        "from_station": "%",
        "to_address": "+36202222222",
        "to_station": "%",
        "text": "Hello, World 2!",
        "create_date": "2021-06-16 12:18:04",
        "valid_until": "2021-06-23 12:18:04",
        "time_to_send": "2021-06-16 12:18:04",
        "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

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 managment console. The following video shows you what to look for.

Video 2 - Sending SMS with the PHP code above

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.

Video 3 - How to turn on logging in Ozeki SMS Gateway

To turn on logging in the http_user you have to follow the same instructions.

Summary

This guide contains all the crucial information on how to send multiple SMS messages from PHP with the HTTP REST SMS API of the Ozeki SMS Gateway. You will find using PHP SMS API very worthwhile because it not only works with online SMS service providers; it works with the android SMS gateway to allow you to send SMS wirelessly. This service can make your messaging more efficient because it allows you to deliver valuable information to more than one mobile phone at once.

You can find much more guides like this on the Ozeki website, let's learn more! Find out how you can use PHP for other purposes, start with sending scheduled SMS messages.

Setup Ozeki SMS Gateway now, and put what you have learned into practice!

More information