How to Send SMS using PHP Program (full code sample included)

How to Integrate a SMS API in PHP?

SMS or short messaging service is an integral part of any business’s communication with its customers. Be it promotional or transactional, SMS creates direct contact with your customers and gives them critical and real-time information, such as an OTP for login, or a coupon code that expires in a week. Automating these messages makes sense as the marketing team can focus on curating messages rather than sending them. A host of SMS automation solutions is available, but the most versatile and robust is using a PHP software. 

What is PHP?

PHP was released back in 1994 by Rasmus Lerdorf, a Danish Canadian programmer. PHP, an acronym for Hypertext Preprocessor, is a general-purpose scripting language especially suited to web development. PHP is so versatile and robust that companies like Facebook and WordPress use it as their website’s backbone! It is also intuitive and easy enough for beginners to start with backend development. Thus, you can surely use it to automate your SMS deliveries. Let’s get started.

Request a Demo for More Information

How to Integrate a SMS API in PHP?

Step 1: Download PHP

Head over to and download the latest PHP stable software package for your device. Click on the download page and then click windows download. Once there you have a few options, click on a zip download and extract the zip on a folder. The unzipped folder will contain a windows installer Exe file, run that as an administrator, and click install.  Note: It is imperative to match computer architecture to the software architecture. For example, x86 software of PHP will run only on x86 windows machines. Note: For other systems and troubleshooting, please click on this link

Step 2: Select an SMS API provider

SMS API providers are organisations that have created a development tool on their servers to make use of PHP and automate message deliveries. There are a host of options for you ranging from, which provides free SMS API services to mTalkz that has a more sophisticated API, more features and unparalleled customer support. Head over to SMS API for developer for more information. Once you have narrowed down and selected your SMS API provider, it’s now time to code and automate message deliveries. 

Step 3: PHP Code to Integrate SMS API

$postData = ‘apikey=XXXXXXXXXXXXXXXX&senderid=XXXXXX&number= XXXXXXXXXXX,XXXXXXXXXXX&message=Test Post API&format=json’; $response = httpPOST($postData); print_r($response); function httpPost($post){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,”http://URL/V2/http-api.php?”); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $output = curl_exec($ch); curl_close ($ch); return $output; } There is no one right way to code for such a scenario. You might find variations of the same code or a different code entirely, and all are correct. This is just one way to send SMS through SMS API using PHP.  Here in this code, we are going to do three main things.

  1. Create a message on a server and send it to an SMS Gateway via Email.
  2. The SMS Gateway converts the text in the Email to SMS 
  3. This message is relayed to a short message service centre, whose servers send the message to individual numbers

Email To SMS

Sending an SMS via email using PHP is very simple. Just write, where the phone number is the target number, and the domain name is the network for the SMS Gateway.  For example, To send an SMS through PHP to XYZ, you could add to any email client, type a message, and hit send. This will send a text message to phone number +1 (547) 354-3765 on the Verizon Wireless Network.

Request a Demo for More Information

Here is a full PHP code for the same:

include_once “class.curl.php”; include_once “class.sms.php”;

$smsapp=new sms(); $smsapp->setGateway(‘mTalkz’); account; // mTalkz code has been tested on different accounts to be stable. echo “Logging in … “; $smsapp->login(‘username for mTalkz’,’password’); echo “Sending SMS … “; $result=$smsapp->send(’10 digit mobile number’,’Your text message’); if($result) { echo “Message sent”;    //If you see this your message has been sent successfully!! } else { echo “Error encountered : “.$smsapp->getLastError(); }

How to Send SMS using PHP Program – FAQs

How to send message to mobile using php code

Step wise Step are mentioned in the above blog. Please go step to step and if in case you need any kind of help, please reach out to us.

How to send sms from website using php?

Follow the Blog. You may integrate the PHP code along with a subscription from Mtalkz to send messges.

How to send sms from localhost to mobile?

One can use a PHP script to integrate the local application and with help of a SMS Gateway subscription from Mtalkz and proper registration with local Telecom authorities one an send messages to a mobile number anywhere in the world.

How can I send SMS using PHP?

One can use the code mentioned in the blog along with any application to send SMS.

How to send otp sms using codeigniter?

Much like any other framework of development one can use a PHP script to integrate an SMS API provided by Mtalkz and send SMS messages via an API. Some of the popular frameworks are Angular, Laravel, Symfony, CakePHP, Yii, Zend, Phalcon, and Slim Framework.


Powered by WhatsApp Chat

× Message Us