Using curl functions in php to send sms for smaple sms gateway

Saturday, November 8, 2008 11:42
Posted in category PHP, Programming

This is a sample code assuming we have a sms gateway with name smsgateway.co.cc. We have take a sample

$curlPost = 'Mobile_No=9000099119&Type=SMS&Message="This is a test message"';

//we have to pass the mobile number and message as argument to the url. 

$LOGINURL = "http://smsgateway.co.cc/UserMiscellaneousMT.asp"; 

//This url will be provided by our gateway 

$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";

//setting the browser agent as Firefox 

$ch = curl_init(); 

// initiate a curl function c

url_setopt($ch, CURLOPT_URL,$LOGINURL); 

//set the url to be used 

curl_setopt($ch, CURLOPT_USERAGENT, $agent);

//set the browser 

curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,2); 

//set the maxmium time to execute 

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

// accept the responce after the execution 

curl_setopt($ch, CURLOPT_POST, 1); 

// set the post method for passing variables to the server 

curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);

//assigning the post values 

$result = curl_exec ($ch);

//executing with the above set options and catching the values in a variable 

curl_close ($ch); 

// closing the connection 

echo $result;

// printing the result.
CURL_INIT():-

The curl_init() function returns a Curl instance for us to use in later functions, and you should always store it for later. It has just one optional parameter: if you pass a string into curl_init(), it will automatically use that string as the URL to work with. In the script above, we use curl_setopt() to do that for clarity, but it is all the same.

Curl_setopt():-

Curl_setopt() takes three parameters, which are the Curl instance to use, a constant value for the setting you want to change, and the value you want to use for that setting. There are a huge number of constants you can use for settings, and many of these are listed shortly. In the example we use CURLOPT_URL:- which is used to set the URL for Curl. CURLOPT_USERAGENT:- which is used to set browseragent for Curl. CURLOPT_CONNECTTIMEOUT:- which is used to set the MAX ececution tiem for Curl. CURLOPT_RETURNTRANSFER:- which is used to set the Returntransfer enable for Curl. CURLOPT_POST:- which is used to set the method of posting the variables for Curl. CURLOPT_POSTFIELDS:- which is used to set the variables for Curl.

curl_exec():-

Calling curl_exec() means, We finished setting our options and ready do it, and you need to pass precisely one parameter: the Curl resource to use. The return value of curl_exec() is true/false by default.

curl_close():-

The final function, curl_close(), takes a Curl resource as its only parameter, closes the Curl session, then frees up the associated memory.

Bookmark and Share

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

You can leave a response, or trackback from your own site.

5 Responses to “Using curl functions in php to send sms for smaple sms gateway”

  1. Using curl functions in php to send SMS via SMS gateway | CLD Tutorials says:

    December 14th, 2008 at 8:10 am

    [...] View tutorial Tutorial Stats : 1 views Rate Tutorial : (No Ratings Yet)  Loading … DiggStumbleDel.icio.usFloatBumpMixxRedditzaBoxother [...]

  2. edward says:

    February 2nd, 2009 at 7:20 pm

    does dis work.. can i put any mobile num.. will i receive the message?.. :D

  3. PHP_Starter says:

    February 2nd, 2009 at 11:51 pm

    This works if you use any SMS Gateway.

  4. jesse says:

    May 11th, 2009 at 1:52 am

    please suggest any free sms gateway

  5. james g says:

    July 1st, 2009 at 12:10 am

    Thank you so much for this! Now I need to get Adobe Photoshop.. :p

Leave a Reply