How to Send an SMS with a PHP Script

Written by roy huggins
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Send an SMS with a PHP Script
Your PHP application can reach your customers' phones as well as their browsers. (cellphone image by Philip Date from

With nearly everyone carrying a mobile phone, Short Message Service (SMS) is a powerful way to reach people wherever they are. Your PHP application can leverage this service, too. However, SMS is not like e-mail, which is free and open to anyone with an Internet connection. SMS messages are delivered through the telecoms' cellular networks, and so you must use an SMS gateway server to access the network.

Skill level:

Things you need

  • PHP installation with CURL support

Show MoreHide


  1. 1

    Select an SMS gateway service to use. This gateway will forward your messages into the telecoms' cellular network. The gateway service will cost money, because the telecoms charge for SMS message delivery. Take a look at Ozeki or TM4B for examples. There are other services, too, such as the Google SMS gateway.

  2. 2

    Sign up for the service and get the necessary login credentials.

  3. 3

    Acquire and read the Application Programming Interface (API) for your chosen gateway. The manner in which you send your SMS message will vary according to the API. But there is a general pattern it follows regardless of which gateway you choose.

  1. 1

    Set up your request variables. Create an array called "$sms". This array will contain all the request variables your SMS gateway needs to send a message. You will likely need to set "$sms['to']" for the message receiver, "$sms['from']" for the message sender and "$sms['message']" for the contents of the message. Check the API for other data that needs to be sent.

  2. 2

    Use CURL to send your request. See the CURL documentation on the PHP website for details on how to use CURL. You will use "curl_setopt()" to set the URL of your SMS gateway and to encode your "$sms" variable. You will use "curl_exec()" to send your SMS message to the gateway.

  3. 3

    Examine the response that "curl_exec()" returns. If the response is "false," then your SMS was not sent. There may be a network problem on your server. If there isn't, check the SMS gateway API again to make sure you're sending all the necessary information correctly. Some SMS gateways offer debugging tools. If yours does, take advantage of them at this point. If the response was "true," then your message was sent successfully.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.