PHP API Kit

The following code is a PHP v5 example of sending a message using XML.

  1. <?php
  2. /**
  3. * Sample SMS API XML Interface example in PHP
  4. * Copyright Mediaburst 2009-2010
  5. *
  6. * IMPORTANT NOTE:
  7. * This script is a brief example and should not be used in
  8. * a live environment.
  9. * This script is provided without warranty and Mediaburst
  10. * do not offer any support in the use of this example.
  11. */
  12. // Turning on track_errors allows us to see details on HTTP errors
  13. ini_set("track_errors", 1);
  14. // Set Debug to 1 to output request and response XML
  15. $debug = 1;
  16. // Account Parameters
  17. $server = 'http://sms.message-platform.com/xml/send.aspx';
  18. $username = 'My_User';
  19. $password = 'My_Pass';
  20. $content = 'Mediaburst XML API Test - PHP Example';
  21. /**
  22. * Numbers to send to
  23. * Normally this would come from your database/application
  24. */
  25. $numbers = array('447000000001','447000000002','447000000abc');
  26. // Build the XML request to send
  27. $req_doc = new DOMDocument('1.0', 'UTF-8');
  28. $root = $req_doc->createElement('Message');
  29. $req_doc->appendChild($root);
  30. $root->appendChild($req_doc->createElement('Username', $username));
  31. $root->appendChild($req_doc->createElement('Password', $password));
  32. // Create an SMS Element for each recipient
  33. foreach($numbers as $number)
  34. {
  35. $sms_node = $req_doc->createElement('SMS');
  36. $sms_node->appendChild($req_doc->createElement('To', $number));
  37. $sms_node->appendChild($req_doc->createElement('Content',
    $content));
  38. $root->appendChild($sms_node);
  39. }
  40. $req_xml = $req_doc->saveXML();
  41. if($debug) print("Request:\n$req_xml\n");
  42. $resp_xml = sendToHost($server,$req_xml);
  43. if($debug) print("Response:\n$resp_xml\n");
  44. $resp_doc = DOMDocument::loadXML($resp_xml);
  45. $err_no = null;
  46. $err_desc = null;
  47. foreach($resp_doc->documentElement->childNodes AS $doc_child)
  48. {
  49. switch($doc_child->nodeName)
  50. {
  51. case "SMS_Resp":
  52. $to = null;
  53. $msg_id = null;
  54. $sms_err_no = null;
  55. $sms_err_desc = null;
  56. foreach($doc_child->childNodes AS $resp_node)
  57. {
  58. switch($resp_node->nodeName)
  59. {
  60. case "To":
  61. $to = $resp_node->nodeValue;
  62. break;
  63. case "MessageID":
  64. $msg_id = $resp_node->nodeValue;
  65. break;
  66. case "ErrNo":
  67. $sms_err_no = $resp_node->nodeValue;
  68. break;
  69. case "ErrDesc":
  70. $sms_err_desc = $resp_node->nodeValue;
  71. break;
  72. }
  73. }
  74. if(isset($sms_err_no))
  75. print "To: $to Error $sms_err_no: $sms_err_desc\n";
  76. else
  77. print "To: $to ID: $msg_id\n";
  78. break;
  79. case "ErrNo":
  80. $err_no = $doc_child->nodeValue;
  81. break;
  82. case "ErrDesc":
  83. $err_desc = $doc_child->nodeValue;
  84. break;
  85. }
  86. }
  87. if(isset($err_no))
  88. print "Error in request.\nError Number: $err_no Description:
    $err_desc\n";
  89. /*
  90. * Function to perform the HTTP POST to Mediaburst API Server
  91. * PHP Doesn't have convenient HTTP functions in many default installs
  92. */
  93. function sendToHost($url,$data)
  94. {
  95. $params = array('http' => array(
  96. 'method' => 'POST',
  97. 'header' => 'Content-Type: text/xml\r\n',
  98. 'content' => $data
  99. ));
  100. $ctx = stream_context_create($params);
  101. $fp = @fopen($url, 'rb', false, $ctx);
  102. if (!$fp) {
  103. throw new Exception("Problem with $url,
    $php_errormsg");
  104. }
  105. $response = @stream_get_contents($fp);
  106. if ($response === false) {
  107. throw new Exception("Problem reading data from $url,
    $php_errormsg");
  108. }
  109. return $response;
  110. }
  111. ?>
<?php
/**
 * Sample SMS API XML Interface example in PHP
 * Copyright Mediaburst 2009-2010
 *
 * IMPORTANT NOTE:
 * This script is a brief example and should not be used in
 * a live environment.
 * This script is provided without warranty and Mediaburst
 * do not offer any support in the use of this example.
 */
// Turning on track_errors allows us to see details on HTTP errors
ini_set("track_errors", 1);

// Set Debug to 1 to output request and response XML $debug = 1;

// Account Parameters $server = 'http://sms.message-platform.com/xml/send.aspx'; $username = 'My_User'; $password = 'My_Pass'; $content = 'Mediaburst XML API Test - PHP Example'; /** * Numbers to send to * Normally this would come from your database/application */ $numbers = array('447000000001','447000000002','447000000abc');

// Build the XML request to send $req_doc = new DOMDocument('1.0', 'UTF-8'); $root = $req_doc->createElement('Message'); $req_doc->appendChild($root); $root->appendChild($req_doc->createElement('Username', $username)); $root->appendChild($req_doc->createElement('Password', $password));

// Create an SMS Element for each recipient foreach($numbers as $number) { $sms_node = $req_doc->createElement('SMS'); $sms_node->appendChild($req_doc->createElement('To', $number)); $sms_node->appendChild($req_doc->createElement('Content', $content)); $root->appendChild($sms_node); }

$req_xml = $req_doc->saveXML(); if($debug) print("Request:\n$req_xml\n");

$resp_xml = sendToHost($server,$req_xml); if($debug) print("Response:\n$resp_xml\n");

$resp_doc = DOMDocument::loadXML($resp_xml);

$err_no = null; $err_desc = null;

foreach($resp_doc->documentElement->childNodes AS $doc_child) { switch($doc_child->nodeName) { case "SMS_Resp": $to = null; $msg_id = null; $sms_err_no = null; $sms_err_desc = null; foreach($doc_child->childNodes AS $resp_node) { switch($resp_node->nodeName) { case "To": $to = $resp_node->nodeValue; break; case "MessageID": $msg_id = $resp_node->nodeValue; break; case "ErrNo": $sms_err_no = $resp_node->nodeValue; break; case "ErrDesc": $sms_err_desc = $resp_node->nodeValue; break; } } if(isset($sms_err_no)) print "To: $to Error $sms_err_no: $sms_err_desc\n"; else print "To: $to ID: $msg_id\n";

break; case "ErrNo": $err_no = $doc_child->nodeValue; break; case "ErrDesc": $err_desc = $doc_child->nodeValue; break; } } if(isset($err_no)) print "Error in request.\nError Number: $err_no Description: $err_desc\n";

/* * Function to perform the HTTP POST to Mediaburst API Server * PHP Doesn't have convenient HTTP functions in many default installs */

function sendToHost($url,$data) { $params = array('http' => array( 'method' => 'POST', 'header' => 'Content-Type: text/xml\r\n', 'content' => $data )); $ctx = stream_context_create($params); $fp = @fopen($url, 'rb', false, $ctx); if (!$fp) { throw new Exception("Problem with $url, $php_errormsg"); } $response = @stream_get_contents($fp); if ($response === false) { throw new Exception("Problem reading data from $url, $php_errormsg"); } return $response;

} ?>