This tip will show you how to consume an SMS API from SMS provider in your PHP application. Also important here is how I have used proxy IP.
Introduction
Many applications or companies would want to notify users or customers through the sending of SMS and not email only. And because of this and many other reasons, we have found ourselves in a situation whereby we will need to do some integrations of SMS API. This is the main reason for this tip. How can you integrate an SMS API using proxy also.
Background
You would need to get a working SMS API from an SMS provider, and have knowledge of Curl scripting.
Using the Code
The below describes how you can integrate the SMS API. In my code below, I have masked some values. In your case, ensure you replace the xxxx to the correct value. You are expected to have a valid SMS API and also be able to echo the result during execution to see the response.
Also note that depending on your network environment, you may not need the proxy. I am only using proxy here to be able to communicate with the API from my secured network through the proxy IP.
$curl = curl_init();
$Number = $mailagentnamenumber;
$countryCode = 'xxx';
$intNumber = preg_replace('/^0/', $countryCode, $Number);
$msg = 'Good day';
$url = "http://xxxx.com/smsapi/gee.aspx?action=sendmsg&username=xxxx&
password=xxxx&type=1&dlr=1&destination=".$intNumber."
&source=APMT&message=".urlencode($msg)."&URL=";
$proxy = 'x:x:x:x:xxxx';
$jsonDataEncoded = json_encode($jsonData);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
echo $result;
Points of Interest
A proxy IP can help you communicate with external network most especially when you do not want to go through the problems of requesting for a whitelist.
History
- 3rd July, 2023: Initial version