The following code is a perl example of sending a message using XML. It uses the XML::LibXML library to generate the XML and the LWP library to send it.
#!/usr/bin/perluse strict;use LWP;use XML::LibXML;# Set this to 1 to output the XML Sent and Receivedmy $debug = 0;# Parameters for the API callmy $server = 'http://sms.message-platform.com/xml/send.aspx';my $username = 'My_User';my $password = 'My_Pass';my $content = 'Mediaburst XML API Test - Perl Example';# Array of numbers to send message to# Normally this would come from your application/databasemy @numbers = ('447000000001','447000000002','447000000abc');# Build the XML request to sendmy $req_doc = XML::LibXML::Document->new('1.0','UTF-8');my $root = $req_doc->createElement("Message");$req_doc->setDocumentElement($root);$root->appendTextChild('Username', $username);$root->appendTextChild('Password', $password);# Create an SMS Element for each recipientforeach my $number (@numbers) {my $sms_node = $req_doc->createElement("SMS");$root->appendChild($sms_node);$sms_node->appendTextChild('To', $number);$sms_node->appendTextChild('Content', $content);}print $req_doc->toString(1)."\n" if ($debug);# Send XML to Mediaburst API Servermy $ua=LWP::UserAgent->new();my $req=HTTP::Request->new('POST',$server);$req->content($req_doc->toString());$req->content_type('text/xml');my $resp=$ua->request($req);# Parse the Responseif($resp->is_success) {my $parser = XML::LibXML->new();my $resp_doc = $parser->parse_string($resp->content);print $resp_doc->toString(1)."\n" if ($debug);$root = $resp_doc->getDocumentElement;if($root->hasChildNodes()) {my $err_no;my $err_desc;foreach my $a_node ($root->childNodes) {if($a_node->nodeName eq 'SMS_Resp') {my $to;my $msg_id;my $sms_err_no;my $sms_err_desc;foreach my $resp_node
($a_node->childNodes) {if($resp_node->nodeName eq
'To') {$to =
$resp_node->firstChild->nodeValue;} elsif ($resp_node->nodeName
eq 'MessageID') {$msg_id =
$resp_node->firstChild->nodeValue;} elsif ($resp_node->nodeName
eq 'ErrNo') {$sms_err_no =
$resp_node->firstChild->nodeValue;} elsif ($resp_node->nodeName
eq 'ErrDesc') {$sms_err_desc =
$resp_node->firstChild->nodeValue;}}if(defined($sms_err_no)) {print "To: $to Error
$sms_err_no: $sms_err_desc\n";} else {print "To: $to ID:
$msg_id\n";}} elsif($a_node->nodeName eq 'ErrNo') {$err_no =
$a_node->firstChild->nodeValue;} elsif($a_node->nodeName eq 'ErrDesc') {$err_desc =
$a_node->firstChild->nodeValue;}}if(defined($err_no)) {print "Error in request.\nError Number:
$err_no Description: $err_desc\n";}} else {print "Error in response. No XML child
nodes.\n";}} else {print "API Server returned Non 200 status
code:".$resp->code."\n";}
#!/usr/bin/perluse strict; use LWP; use XML::LibXML;
# Set this to 1 to output the XML Sent and Received my $debug = 0;
# Parameters for the API call my $server = 'http://sms.message-platform.com/xml/send.aspx'; my $username = 'My_User'; my $password = 'My_Pass'; my $content = 'Mediaburst XML API Test - Perl Example';
# Array of numbers to send message to # Normally this would come from your application/database my @numbers = ('447000000001','447000000002','447000000abc');
# Build the XML request to send my $req_doc = XML::LibXML::Document->new('1.0','UTF-8'); my $root = $req_doc->createElement("Message"); $req_doc->setDocumentElement($root); $root->appendTextChild('Username', $username); $root->appendTextChild('Password', $password);
# Create an SMS Element for each recipient foreach my $number (@numbers) { my $sms_node = $req_doc->createElement("SMS"); $root->appendChild($sms_node); $sms_node->appendTextChild('To', $number); $sms_node->appendTextChild('Content', $content); } print $req_doc->toString(1)."\n" if ($debug);
# Send XML to Mediaburst API Server my $ua=LWP::UserAgent->new(); my $req=HTTP::Request->new('POST',$server); $req->content($req_doc->toString()); $req->content_type('text/xml'); my $resp=$ua->request($req);
# Parse the Response if($resp->is_success) { my $parser = XML::LibXML->new(); my $resp_doc = $parser->parse_string($resp->content); print $resp_doc->toString(1)."\n" if ($debug);
$root = $resp_doc->getDocumentElement; if($root->hasChildNodes()) { my $err_no; my $err_desc; foreach my $a_node ($root->childNodes) { if($a_node->nodeName eq 'SMS_Resp') { my $to; my $msg_id; my $sms_err_no; my $sms_err_desc; foreach my $resp_node ($a_node->childNodes) { if($resp_node->nodeName eq 'To') { $to = $resp_node->firstChild->nodeValue; } elsif ($resp_node->nodeName eq 'MessageID') { $msg_id = $resp_node->firstChild->nodeValue; } elsif ($resp_node->nodeName eq 'ErrNo') { $sms_err_no = $resp_node->firstChild->nodeValue; } elsif ($resp_node->nodeName eq 'ErrDesc') { $sms_err_desc = $resp_node->firstChild->nodeValue; } } if(defined($sms_err_no)) { print "To: $to Error $sms_err_no: $sms_err_desc\n"; } else { print "To: $to ID: $msg_id\n"; } } elsif($a_node->nodeName eq 'ErrNo') { $err_no = $a_node->firstChild->nodeValue; } elsif($a_node->nodeName eq 'ErrDesc') { $err_desc = $a_node->firstChild->nodeValue; } } if(defined($err_no)) { print "Error in request.\nError Number: $err_no Description: $err_desc\n"; } } else { print "Error in response. No XML child nodes.\n"; } } else { print "API Server returned Non 200 status code:".$resp->code."\n"; }
You might find our Mobile site useful.