Click here to Skip to main content
15,892,005 members
Articles / Programming Languages / PHP
Tip/Trick

How to Send SMS with Proxy IP in Your PHP or other Applications

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
3 Jul 2023CPOL1 min read 8.5K   27   2  
Consume SMS API from SMS provider in PHP app
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.

PHP
$curl = curl_init();
   $Number = $mailagentnamenumber;
   $countryCode = 'xxx'; // Replace with known country code of user.
   $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);

   //Initiate cURL.
   $ch = curl_init($url);

   //Tell cURL that we want to send a POST request.
   curl_setopt($ch, CURLOPT_POST, 1);
   curl_setopt($ch, CURLOPT_PROXY, $proxy);
   //Attach our encoded JSON string to the POST fields.
   curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // false for https
   curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);

   //Set the content type to application/json
   curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

   //Execute the request
   $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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Business Analyst Personal
Nigeria Nigeria
I have experience in application development in the use of .Net, PHP, MSSQL, JAVA....

Comments and Discussions

 
-- There are no messages in this forum --