How to send SMS from PHP

This page provides a simple PHP example code that allows you to send SMS messages using HTTP requests via the SMS Gateway. The PHP code embedded into an HTML document, so to run the code, you just need to create an HTML file, copy-paste the example code and open the file using your browser. The browser runs the whole HTML file with the PHP code as well, and you will be able to see the results of the SMS sending in the opened HTML page.

What is a PHP SMS API?

The PHP SMS API ensures you to send SMS messages from a web application of a website. The API uses HTTP requests to communicate with the SMS Gateway that delivers the message and sends a response back.


Send SMS from PHP

  • Open XAMPP Control Panel
  • Start Apache service
  • Open Sublime Text or other text editor application
  • Copy-Paste the PHP source code below
  • Save the file to the xampp/htmldocs folder
  • Name the file as index.php
  • Open a new tab in your browser
  • Type 'localhost' and hit Enter to run the PHP code

PHP SMS source code example

The following example PHP source code below is free to use, you can simply implement it into your project or you can modify any segments of the source code to use it for other projects or applications. If you would like to run this example code itself, you just have to follow the instructions from the steps and the video above.

<!DOCTYPE html>
	<title>Http Request sending</title>
		$username = "john";
		$password = "Xc3ffs";
		$messagetype = "SMS:TEXT";
		$httpUrl = "";
		$recipient = urlencode("+36201324567");
		$messagedata = urlencode("TestMessage");

		$sendString = $httpUrl."api?action=sendmessage"."&username=" 

		echo '<p><b> Sending html request:</b> '.$sendString.'</p>';
		$aContext = array(
		    'http' => array(
		    	'method'  => 'GET',
		    'ssl' => array(
		    	'verify_peer' => false,
       			'verify_peer_name' => false,
		$cxContext = stream_context_create($aContext);
		$response = file_get_contents($sendString, true, $cxContext);

		echo '<p><b> Http response received :</b> </p>';
		echo '<xmp>' . $response. '</xmp>';

Step 1 - Open XAMPP Control Panel

The first step to run the PHP example code on your computer is to open XAMPP Control Panel. XAMPP is a free and open-source cross-platform web server solution stack, which contains a Apache HTTP Server, MariaDB database, and interpreters for script written in PHP and Perl programming languages. If you haven't downloaded this application yet, you can download it easily by following the link in the Prerequisites section. If you installed XAMPP already, you just have to open it by clicking on its icon on the desktop as you can see it in Figure 1.

start xampp control panel
Figure 1 - Start XAMPP Control Panel

Step 2 - Start Apache Server

After you opened XAMPP Control Panel, the main window of XAMPP shows up. Here, you will be able to see all the services that XAMPP provides for you. At this point, you need to start the Apache Server as Figure 2 demonstrates it. This service ensures you to run a local webserver where you can build and develop your web application or website. Now, this service provides the environment to run the PHP SMS source code.

starting apache server
Figure 2 - Starting Apache Server

Step 3 - Copy the PHP SMS example source code

The next step after starting the Apache server is to get the example source code. Luckily, this is the right place to grab that PHP SMS source code. All you have to do here, is to scroll up to the 'PHP SMS source code example' section, and as you can see it in Figure 3, just mark out every line of the source code and press Ctrl+C on your keyboard to copy the source code to the clipboard.

copy code from website
Figure 3 - Copy code from website

Step 4 - Paste the source code to a text file

You need to place the copied source code into a simple text file. To do that, first, you have to open a text editor on your computer. That can be the simple Notepad application or you can use a more developed text editor. After you opened your text editor and created a new, empty text file, just press Ctrl+V on your keyboard. As Figure 4 shows that, the source code will be pasted into the text document.

open text editor and paste php sms code into
Figure 4 - Open Text Editor and Paste PHP SMS Code into

Step 5 - Save the document as PHP file

At this point, you have got the document that you need, but it is not in the correct format. To correct that, you just need to save it with .php file extension. So, as Figure 5 shows that, save the text file into the xampp/htdocs folder. It is important to save the file into that folder, since the Apache server will be able to read the file from that folder. You also need to name the file as 'index.php'. That naming ensures that the file will be read automatically in case you load the local web service.

save the document as php file
Figure 5 - Save the document as PHP file

Step 6 - Open 'localhost' to run the example code

The last step of the guide is to open your localhost service. For that, just open your browser, type 'localhost' like in Figure 6, and hit Enter. By performing this action, the local web service loads the saved 'index.php' with the PHP SMS source code. The page that the service loaded prints the HTTP request and also prints the response message from the SMS Gateway that shows if the delivery of the SMS was successful.

open localhost address in browser
Figure 6 - Open localhost address in browser

Step 7 - Check the send result in the Ozeki log

In Ozeki 10 SMS Gateway, you can check easily what messages sent by your application, since the HTTP API service logs every event that occurred during the time it is enabled. So, if you open the SMS Gateway, and select details of the HTTP API service, you will be able to see the events. As you can see it in Figure 7, the service logged an event, when your web application sent the HTTP request to the service.

check the logs of the http api service
Figure 7 - Check the logs of the HTTP API Service

The process of the message sending can be also viewed back by the events. To be able to see that, open the HTTP API User connection, that you had to configure before. Figure 8 shows that how the connection handles the HTTP request and send the message to the recipient that you specified in your PHP SMS example code.

check the logs of the http api user
Figure 8 - Check the logs of the HTTP API User


This tutorial explains to you how to use the provided PHP example code. The code will allow you to send SMS messages using the Ozeki SMS Gateway. By using this code, you will send an HTTP request to the Ozeki SMS Gateway, which will forward your message to any of the selected addresses with ease.

To know more about the flexible SMS sending system that you can build using the Ozeki SMS Gateway, visit the Ozeki website. There are articles about all the programming languages you can use to manage the SMS traffic, like Java or Perl.

To create your first high performance SMS gateway system, now is the best time to download Ozeki SMS Gateway!

More information