How to Delete an SMS in PHP

The most simple way to Delete SMS from PHP is to use the built in HTTP/Rest SMS api of Ozeki SMS Gateway. When you use this API, you will delete SMS messages by issuing a HTTP Post request to the SMS gateway. The SMS gateway will return a HTTP 200 OK response to your request.

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

PHP code to delete sms from mobile

The PHP sms code sample below demonstrates how you can delete 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.

Delete_message.php

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

	$configuration = new Configuration();
		
	$configuration -> Username = "http_user";
	$configuration -> Password = "qwe123";
	$configuration -> ApiUrl = "http://192.168.0.113:9509/api";
				
	$api = new MessageApi($configuration);

	$msg = new Message();
	
	$msg -> ID = "5b163bd9-fc45-46b6-a1fd-78a2b30a153e";
	
	$result = $api -> DeleteSingle($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 DeleteSMS.php

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

What is in the DeleteSMS.zip file?

The SendSMS.zip file contains the Ozeki.Libs.Rest.Php library, which gives you all the tools neccessary to send and receive SMS messages. You will also find the DeleteSms 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 DeleteSMS.zip

How to Delete SMS from PHP (Quick steps)

To Delete 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 delete SMS Request
  8. Use the Delete method to delete 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 delete sms from PHP

To delete 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 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 requst contains a HTTP header part.

POST /api?action=deletemsg HTTP/1.1
Content-Length: 73
Content-Type: application/json
Accept: */*
Authorization: Basic aHR0cF91c2VyOnF3ZTEyMw==
Host: 192.168.0.113:9509

{
  "folder": "inbox",
  "message_ids": [
    "5b163bd9-fc45-46b6-a1fd-78a2b30a153e"
  ]
}

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.120 (www.myozeki.com)
Content-Type: application/json; charset=utf8
Last-Modified: Mon, 21 Jun 2021 07:29:45 GMT
Server: 10/10.3.120
Transfer-Encoding: chunked

{
  "http_code": 200,
  "response_code": "SUCCESS",
  "response_msg": "",
  "data": {
    "folder": "inbox",
    "message_ids": [
      "5b163bd9-fc45-46b6-a1fd-78a2b30a153e"
    ]
  }
}

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

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

Figure 3 - Delete_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 - Deleting SMS with the PHP code above

Final thoughts

This article explained how to delete an SMS in PHP using the HTTP SMS API user of the Ozeki SMS Gateway. If you have completed this tutorial you know the most simple way to delete SMS from PHP. The main advantage of this solution is that the given PHP SMS example can be used in any PHP application.

You can find more articles that contain as valuable information as this one, the most offer simplicity, and better performance. If you want to build a reliable, high-performance system you should not stop reading here, check out the GitHub: PHP SMS API.

First of all, download the Ozeki SMS Gateway and start working on the development now!

More information