How to receive SMS from 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 receive SMS messages by issuing a HTTP Get request to the SMS gateway. The SMS gateway will return a HTTP 200 OK response to your request.

how to receive sms from php
Figure 1 - How to receive SMS from PHP

PHP code to receive sms from 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";
					
		$api = new MessageApi($configuration);
		
		$result = $api -> DownloadIncoming();	
			
		echo "Folder: " . $result -> Folder;
		echo "
"; echo "Limit: " . $result -> Limit; echo "
"; echo strval($result); echo "
"; echo "Messages:"; echo "
"; foreach($result->Messages as $msg) { echo "From: ". $msg->FromAddress . " Text: " . $msg->Text; echo "
"; } }

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 ReceiveSMS.php

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

What is in the ReceiveSMS.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 ReceiveSms 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 ReceiveSMS.zip

How to receive 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. receive 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 receive SMS Request
  8. Use the Receive method to receive 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 receive 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 receive 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 receive 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 receive 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.

GET /api?action=receivemsg&folder=inbox HTTP/1.1
Content-Type: application/json
Accept: */*
Authorization: Basic aHR0cF91c2VyOnF3ZTEyMw==
Host: 127.0.0.1:9509

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.120 (www.myozeki.com)
Content-Type: application/json; charset=utf8
Last-Modified: Thu, 17 Jun 2021 09:18:12 GMT
Server: 10/10.3.120
Transfer-Encoding: chunked

{
  "http_code": 200,
  "response_code": "SUCCESS",
  "response_msg": "",
  "data": {
    "folder": "inbox",
    "limit": "1000",
    "data": [
      {
        "message_id": "16673c81-0568-49af-a0fa-1e4b3d6bb500",
        "from_connection": "SMPP_client_1@localhost",
        "from_address": "+36301111111",
        "from_station": "%",
        "to_connection": "http_user@localhost",
        "to_address": "+36308888888",
        "to_station": "%",
        "text": "Hello World  1!",
        "create_date": "2021-06-17 10:01:55",
        "valid_until": "2021-06-24 10:01:55",
        "time_to_send": "2021-06-17 10:01:55",
        "submit_report_requested": true,
        "delivery_report_requested": false,
        "view_report_requested": false,
        "tags": [
          {
            "name": "PduCount",
            "value": "1"
          }
        ]
      },
      {
        "message_id": "8ed7f65b-d3b5-4b68-8b4d-25b63dad0cc8",
        "from_connection": "SMPP_client_1@localhost",
        "from_address": "+36302222222",
        "from_station": "%",
        "to_connection": "http_user@localhost",
        "to_address": "+36308888888",
        "to_station": "%",
        "text": "Hello World  2!",
        "create_date": "2021-06-17 10:02:03",
        "valid_until": "2021-06-24 10:02:03",
        "time_to_send": "2021-06-17 10:02:03",
        "submit_report_requested": true,
        "delivery_report_requested": false,
        "view_report_requested": false,
        "tags": [
          {
            "name": "PduCount",
            "value": "1"
          }
        ]
      },
      {
        "message_id": "56ad323c-b19b-4406-af2d-cf2eb365e725",
        "from_connection": "SMPP_client_1@localhost",
        "from_address": "+36303333333",
        "from_station": "%",
        "to_connection": "http_user@localhost",
        "to_address": "+36308888888",
        "to_station": "%",
        "text": "Hello World  3!",
        "create_date": "2021-06-17 10:02:27",
        "valid_until": "2021-06-24 10:02:27",
        "time_to_send": "2021-06-17 10:02:27",
        "submit_report_requested": true,
        "delivery_report_requested": false,
        "view_report_requested": false,
        "tags": [
          {
            "name": "PduCount",
            "value": "1"
          }
        ]
      }
    ]
  }
}

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

This video shows you how to download and use the ReceiveSMS 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: Receive_Message.php

Figure 3 - Receive_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 - Receiving SMS with the PHP code above (Video tutorial)

To sum it up

This article explained how to receive SMS messages with the HTTP REST SMS API of the Ozeki SMS Gateway in PHP. If you have followed the steps above, you are now able to create an HTTP SMS API user in the Ozeki SMS Gateway to receive SMS with PHP. This service can be put into use anytime, so your existing PHP system could be expanded with SMS capabilities.

You find more information about this technology and the Ozeki SMS Gateway here, make sure that you continue reading. Ozeki has other articles on this topic, such as How to send SMS from PHP, check it out now!

Your next thing to do is to download the Ozeki SMS Gateway and enjoy the benefits.

More information