Perl API Kit

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.

  1. #!/usr/bin/perl
  2. use strict;
  3. use LWP;
  4. use XML::LibXML;
  5. # Set this to 1 to output the XML Sent and Received
  6. my $debug = 0;
  7. # Parameters for the API call
  8. my $server = 'http://sms.message-platform.com/xml/send.aspx';
  9. my $username = 'My_User';
  10. my $password = 'My_Pass';
  11. my $content = 'Mediaburst XML API Test - Perl Example';
  12. # Array of numbers to send message to
  13. # Normally this would come from your application/database
  14. my @numbers = ('447000000001','447000000002','447000000abc');
  15. # Build the XML request to send
  16. my $req_doc = XML::LibXML::Document->new('1.0','UTF-8');
  17. my $root = $req_doc->createElement("Message");
  18. $req_doc->setDocumentElement($root);
  19. $root->appendTextChild('Username', $username);
  20. $root->appendTextChild('Password', $password);
  21. # Create an SMS Element for each recipient
  22. foreach my $number (@numbers) {
  23. my $sms_node = $req_doc->createElement("SMS");
  24. $root->appendChild($sms_node);
  25. $sms_node->appendTextChild('To', $number);
  26. $sms_node->appendTextChild('Content', $content);
  27. }
  28. print $req_doc->toString(1)."\n" if ($debug);
  29. # Send XML to Mediaburst API Server
  30. my $ua=LWP::UserAgent->new();
  31. my $req=HTTP::Request->new('POST',$server);
  32. $req->content($req_doc->toString());
  33. $req->content_type('text/xml');
  34. my $resp=$ua->request($req);
  35. # Parse the Response
  36. if($resp->is_success) {
  37. my $parser = XML::LibXML->new();
  38. my $resp_doc = $parser->parse_string($resp->content);
  39. print $resp_doc->toString(1)."\n" if ($debug);
  40. $root = $resp_doc->getDocumentElement;
  41. if($root->hasChildNodes()) {
  42. my $err_no;
  43. my $err_desc;
  44. foreach my $a_node ($root->childNodes) {
  45. if($a_node->nodeName eq 'SMS_Resp') {
  46. my $to;
  47. my $msg_id;
  48. my $sms_err_no;
  49. my $sms_err_desc;
  50. foreach my $resp_node
    ($a_node->childNodes) {
  51. if($resp_node->nodeName eq
    'To') {
  52. $to =
    $resp_node->firstChild->nodeValue;
  53. } elsif ($resp_node->nodeName
    eq 'MessageID') {
  54. $msg_id =
    $resp_node->firstChild->nodeValue;
  55. } elsif ($resp_node->nodeName
    eq 'ErrNo') {
  56. $sms_err_no =
    $resp_node->firstChild->nodeValue;
  57. } elsif ($resp_node->nodeName
    eq 'ErrDesc') {
  58. $sms_err_desc =
    $resp_node->firstChild->nodeValue;
  59. }
  60. }
  61. if(defined($sms_err_no)) {
  62. print "To: $to Error
    $sms_err_no: $sms_err_desc\n";
  63. } else {
  64. print "To: $to ID:
    $msg_id\n";
  65. }
  66. } elsif($a_node->nodeName eq 'ErrNo') {
  67. $err_no =
    $a_node->firstChild->nodeValue;
  68. } elsif($a_node->nodeName eq 'ErrDesc') {
  69. $err_desc =
    $a_node->firstChild->nodeValue;
  70. }
  71. }
  72. if(defined($err_no)) {
  73. print "Error in request.\nError Number:
    $err_no Description: $err_desc\n";
  74. }
  75. } else {
  76. print "Error in response. No XML child
    nodes.\n";
  77. }
  78. } else {
  79. print "API Server returned Non 200 status
    code:".$resp->code."\n";
  80. }
#!/usr/bin/perl

use 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"; }