Friday, July 19, 2013

How to Send an SMS with a PHP Script


Acquire an SMS Gateway Service
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. Sign up for the service and get the necessary log-in credentials.
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.
Send Your Message
4. 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.
5. 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.
6. 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.

No comments:

Post a Comment