Perl
We offer code samples to make it easy for you to plug in SMS functionality into your applications.
Download our SDK that contains sample projects in many languages or go straight to the language you need with our code samples.
Code Sample
#!/usr/bin/perl -w
$file=”cgi-lib”;
do $file || die “Fatal Error: Can’t load cgi library – $file\n”;
&ReadParse;
use CGI qw(:all);
use LWP::UserAgent;
$ua = new LWP::UserAgent;
$ua->agent(“AgentName/0.1 ” . $ua->agent); # Create a user agent object
#setup a few things first
my $user = “user\@company.com”; #username
my $pass = “MyPassword”; #password
#my $text=$in{text}; #text passed from previous form
#my $mobnum=$in{mobnum}; #mobile number passed from previous form as well
$text = substr($text,0,160); #just to make sure we dont get anything else in there
$myOutMsg = ‘<?xml version=”1.0″ encoding=”UTF-8″ ?>’;
$myOutMsg .= ‘<Request xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” ‘;
$myOutMsg .= ‘xsi:noNamespaceSchemaLocation=”http://schema.2sms.com/1.0/0410_RequestSendMessage.xsd” ‘;
$myOutMsg .= ‘Version=”1.0″>’;
$myOutMsg .= ‘<Identification>’;
$myOutMsg .= ‘<UserID>’ . $user .'</UserID>’;
$myOutMsg .= ‘<Password>’.$pass .'</Password>’;
$myOutMsg .= ‘</Identification>’;
$myOutMsg .= ‘<Service>’;
$myOutMsg .= ‘<ServiceName>SendMessage</ServiceName>’;
$myOutMsg .= ‘<ServiceDetail>’;
$myOutMsg .= ‘<SingleMessage>’;
$myOutMsg .= ‘<Destination>’.$mobnum.'</Destination>’;
$myOutMsg .= ‘<Text>’.$text.'</Text>’;
$myOutMsg .= ‘</SingleMessage>’;
$myOutMsg .= ‘</ServiceDetail>’;
$myOutMsg .= ‘</Service>’;
$myOutMsg .= ‘</Request>’;
print header, start_html(“title of page”); #start the html of the page
my $req = new HTTP::Request POST => ‘http://www.2sms.com/xml/xml.jsp’; #Create a request
$req->content_type(‘text/xml’); #form our header
$req->content($myOutMsg); #create the content
my $res = $ua->request($req); #Pass request to the user agent and get a response back
if ($res->is_success) { #Check the outcome of the response
print $res->content; #you can comment this bit out if you want, it basically just prints the response
from the server
print”<center><font face=\”arial, verdana, helvetica\” size=\”2\”>Your message has been sent</center </font>\n”;
} else {
print “<center><font face=\”arial, verdana, helvetica\” size=\”2\”>There seems to be an error please hit your back button and try again</font>\n”;
}
print end_html; #close of the html
exit; #exit.