/SMS/PHP/SendSMS/index.php

https://github.com/tanand/bf_staging · PHP · 140 lines · 96 code · 20 blank · 24 comment · 11 complexity · b67de942f1ed5d0419ba57a7cbc076f5 MD5 · raw file

  1. <?php
  2. session_start();
  3. /* Extract the session variables */
  4. $accessToken = $_SESSION["sms_access_token"];
  5. $sendSMSAction = $_REQUEST[sendSms];
  6. $getSMSDelStatusAction = $_REQUEST[getSmsDeliveryStatus];
  7. ?>
  8. <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
  9. <head>
  10. <title>Sending SMS</title>
  11. </head>
  12. <body>
  13. <img src="http://developer.att.com/developer/images/att.gif" />
  14. <?php
  15. /*
  16. Check if access token is available in the session,
  17. else prompt user to fetch access token.
  18. */
  19. if($accessToken == null || $accessToken == '') {
  20. print '<br/><a href="oauth.php">Fetch Access Token</a><br/>';
  21. }
  22. ?>
  23. <?php//form for send SMS ?>
  24. <form name="sendSms" method="post">
  25. Access Token <input type="text" name="access_token" value="<?php echo $accessToken ?>" size=40/><br/>
  26. MSISDN <input type="text" name="address" value="tel:" /><br/>
  27. Message <input type="text" name="message" value="Test." size=40/><br/>
  28. <input type="submit" name="sendSms" value="Send SMS"/></form><br>
  29. </form>
  30. <?php
  31. /* Extract POST parmeters from send SMS form
  32. and invoke he URL to send SMS along with access token
  33. */
  34. if ($sendSMSAction != null) {
  35. $smsMsg = $_POST['message'];
  36. $address = $_POST['address'];
  37. // Form the URL to send SMS
  38. $sendSMS_RequestBody = '{"address":"tel:+14252337701","message":"'.$smsMsg.'"}';//post data
  39. $sendSMS_Url = "https://beta-api.att.com/1/messages/outbox/sms?access_token=".$accessToken;
  40. $sendSMS_headers = array(
  41. 'Content-Type: application/json'
  42. );
  43. //Invoke the URL
  44. $sendSMS = curl_init();
  45. curl_setopt($sendSMS, CURLOPT_URL, $sendSMS_Url);
  46. curl_setopt($sendSMS, CURLOPT_POST, 1);
  47. curl_setopt($sendSMS, CURLOPT_HEADER, 0);
  48. curl_setopt($sendSMS, CURLINFO_HEADER_OUT, 0);
  49. curl_setopt($sendSMS, CURLOPT_HTTPHEADER, $sendSMS_headers);
  50. curl_setopt($sendSMS, CURLOPT_POSTFIELDS, $sendSMS_RequestBody);
  51. curl_setopt($sendSMS, CURLOPT_RETURNTRANSFER, 1);
  52. curl_setopt($sendSMS, CURLOPT_SSL_VERIFYPEER, false);
  53. $sendSMS_response = curl_exec($sendSMS);
  54. $responseCode=curl_getinfo($sendSMS,CURLINFO_HTTP_CODE);
  55. /*
  56. If URL invocation is successful print success msg along with sms ID,
  57. else print the error msg
  58. */
  59. if($responseCode==200)
  60. {
  61. $jsonObj = json_decode($sendSMS_response);
  62. $smsID = $jsonObj->{'id'};//if the SMS send successfully ,then will get a SMS id.
  63. echo "Successfully Submitted <br> SMS ID value is : ".$smsID;
  64. }
  65. else{
  66. echo curl_error($sendSMS);
  67. }
  68. curl_close ($sendSMS);
  69. }
  70. ?>
  71. <hr>
  72. <?php//form for getting delivery status ?>
  73. <form name="getSmsDeliveryStatus" method="get">
  74. SMS ID <input type="text" name="id" value="" size=40/><br />
  75. Access Token <input type="text" name="access_token" value="<?php echo $accessToken ?>" size=40/><br>
  76. <input type="submit" name="getSmsDeliveryStatus" value="Get SMS Delivery Status" />
  77. </form>
  78. <?php
  79. /* Extract sms ID from getSmsDeliveryStatus form
  80. and invoke the URL to get Sms Delivery Status along with access token
  81. */
  82. if ($getSMSDelStatusAction != null) {
  83. $smsID = $_GET['id'];
  84. // Form the URL to get Sms Delivery Status
  85. $getSMSDelStatus_Url = "https://beta-api.att.com/1/messages/outbox/sms/";
  86. $getSMSDelStatus_Url .= $smsID;
  87. $getSMSDelStatus_Url .= "?access_token=".$accessToken;
  88. $getSMSDelStatus_headers = array(
  89. 'Content-Type: application/x-www-form-urlencoded'
  90. );
  91. //Invoke the URL
  92. $getSMSDelStatus = curl_init();
  93. curl_setopt($getSMSDelStatus, CURLOPT_URL, $getSMSDelStatus_Url);
  94. curl_setopt($getSMSDelStatus, CURLOPT_HTTPGET, 1);
  95. curl_setopt($getSMSDelStatus, CURLOPT_HEADER, 0);
  96. curl_setopt($getSMSDelStatus, CURLINFO_HEADER_OUT, 0);
  97. curl_setopt($getSMSDelStatus, CURLOPT_HTTPHEADER, $getSMSDelStatus_headers);
  98. curl_setopt($getSMSDelStatus, CURLOPT_RETURNTRANSFER, 1);
  99. curl_setopt($getSMSDelStatus, CURLOPT_SSL_VERIFYPEER, false);
  100. $getSMSDelStatus_response = curl_exec($getSMSDelStatus);
  101. $responseCode=curl_getinfo($getSMSDelStatus,CURLINFO_HTTP_CODE);
  102. /*
  103. If URL invocation is successful print success msg along with sms delivery status,
  104. else print the error msg
  105. */
  106. if($responseCode==200)
  107. {
  108. //decode the response and display it.
  109. $jsonObj = json_decode($getSMSDelStatus_response);
  110. $deliveryStatus = $jsonObj->{'deliveryInfoList'}->{'deliveryInfo'}->{'deliveryStatus'};
  111. echo "Delivery Status for SMS ID - ".$smsID." : ".$deliveryStatus;
  112. }
  113. else{
  114. echo curl_error($getSMSDelStatus);
  115. }
  116. curl_close ($getSMSDelStatus);
  117. }
  118. ?>
  119. </body>
  120. </html>