/way2sms-api.php

https://github.com/kingster/Way2SMS-API · PHP · 147 lines · 81 code · 24 blank · 42 comment · 16 complexity · 45439d4043424db3b227682c2160c875 MD5 · raw file

  1. <?php
  2. /**
  3. * WAY2SMSClient
  4. * @author Kingster
  5. * @author SuyashBansal
  6. * @category SMS
  7. * Please use this code on your own risk. The author is no way responsible for the outcome arising out of this
  8. * Good Luck!
  9. **/
  10. class WAY2SMSClient
  11. {
  12. var $curl;
  13. var $timeout = 30;
  14. var $jstoken;
  15. var $way2smsHost;
  16. var $refurl;
  17. /**
  18. * @param $username
  19. * @param $password
  20. * @return bool|string
  21. */
  22. function login($username, $password)
  23. {
  24. $this->curl = curl_init();
  25. $uid = urlencode($username);
  26. $pwd = urlencode($password);
  27. // Go where the server takes you :P
  28. curl_setopt($this->curl, CURLOPT_URL, "http://way2sms.com");
  29. curl_setopt($this->curl, CURLOPT_HEADER, true);
  30. curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, false);
  31. curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, TRUE);
  32. $a = curl_exec($this->curl);
  33. if (preg_match('#Location: (.*)#', $a, $r))
  34. $this->way2smsHost = trim($r[1]);
  35. // Setup for login
  36. curl_setopt($this->curl, CURLOPT_URL, $this->way2smsHost . "Login1.action");
  37. curl_setopt($this->curl, CURLOPT_POST, 1);
  38. curl_setopt($this->curl, CURLOPT_POSTFIELDS, "username=" . $uid . "&password=" . $pwd . "&button=Login");
  39. curl_setopt($this->curl, CURLOPT_COOKIESESSION, 1);
  40. curl_setopt($this->curl, CURLOPT_COOKIEFILE, "cookie_way2sms");
  41. curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, 1);
  42. curl_setopt($this->curl, CURLOPT_MAXREDIRS, 20);
  43. curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
  44. curl_setopt($this->curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
  45. curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, $this->timeout);
  46. curl_setopt($this->curl, CURLOPT_REFERER, $this->way2smsHost);
  47. $text = curl_exec($this->curl);
  48. // Check if any error occured
  49. if (curl_errno($this->curl))
  50. return "access error : " . curl_error($this->curl);
  51. // Check for proper login
  52. $pos = stripos(curl_getinfo($this->curl, CURLINFO_EFFECTIVE_URL), "main.action");
  53. if ($pos === "FALSE" || $pos == 0 || $pos == "")
  54. return "invalid login";
  55. // Set the home page from where we can send message
  56. $this->refurl = curl_getinfo($this->curl, CURLINFO_EFFECTIVE_URL);
  57. /*$newurl = str_replace("ebrdg.action?id=", "main.action?section=s&Token=", $this->refurl);
  58. curl_setopt($this->curl, CURLOPT_URL, $newurl);*/
  59. // Extract the token from the URL
  60. $tokenLocation = strpos($this->refurl, "Token");
  61. $this->jstoken = substr($this->refurl, $tokenLocation + 6, 37);
  62. //Go to the homepage
  63. //$text = curl_exec($this->curl);
  64. return true;
  65. }
  66. /**
  67. * @param $phone
  68. * @param $msg
  69. * @return array
  70. */
  71. function send($phone, $msg)
  72. {
  73. $result = array();
  74. // Check the message
  75. if (trim($msg) == "" || strlen($msg) == 0)
  76. return "invalid message";
  77. // Take only the first 140 characters of the message
  78. $msg = substr($msg, 0, 140);
  79. // Store the numbers from the string to an array
  80. $pharr = explode(",", $phone);
  81. // Send SMS to each number
  82. foreach ($pharr as $p) {
  83. // Check the mobile number
  84. if (strlen($p) != 10 || !is_numeric($p) || strpos($p, ".") != false) {
  85. $result[] = array('phone' => $p, 'msg' => $msg, 'result' => "invalid number");
  86. continue;
  87. }
  88. // Setup to send SMS
  89. curl_setopt($this->curl, CURLOPT_URL, $this->way2smsHost . 'smstoss.action');
  90. curl_setopt($this->curl, CURLOPT_REFERER, curl_getinfo($this->curl, CURLINFO_EFFECTIVE_URL));
  91. curl_setopt($this->curl, CURLOPT_POST, 1);
  92. curl_setopt($this->curl, CURLOPT_POSTFIELDS, "ssaction=ss&Token=" . $this->jstoken . "&mobile=" . $p . "&message=" . $msg . "&button=Login");
  93. $contents = curl_exec($this->curl);
  94. //Check Message Status
  95. $pos = strpos($contents, 'Message has been submitted successfully');
  96. $res = ($pos !== false) ? true : false;
  97. $result[] = array('phone' => $p, 'msg' => $msg, 'result' => $res);
  98. }
  99. return $result;
  100. }
  101. /**
  102. * logout of current session.
  103. */
  104. function logout()
  105. {
  106. curl_setopt($this->curl, CURLOPT_URL, $this->way2smsHost . "LogOut");
  107. curl_setopt($this->curl, CURLOPT_REFERER, $this->refurl);
  108. $text = curl_exec($this->curl);
  109. curl_close($this->curl);
  110. }
  111. }
  112. /**
  113. * Helper Function to send to sms to single/multiple people via way2sms
  114. * @example sendWay2SMS ( '9000012345' , 'password' , '987654321,9876501234' , 'Hello World')
  115. */
  116. function sendWay2SMS($uid, $pwd, $phone, $msg)
  117. {
  118. $client = new WAY2SMSClient();
  119. $client->login($uid, $pwd);
  120. $result = $client->send($phone, $msg);
  121. $client->logout();
  122. return $result;
  123. }