ssms_su.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * Sends request to API
  4. * @param $request - associative array to pass to API, "format"
  5. * key will be overridden
  6. * @param $cookie - cookies string to be passed
  7. * @return
  8. * * NULL - communication to API failed
  9. * * ($err_code, $data) if response was OK, $data is an associative
  10. * array, $err_code is an error numeric code
  11. */
  12. function _smsapi_communicate($request, $cookie=NULL){
  13. $request['format'] = "json";
  14. $curl = curl_init();
  15. curl_setopt($curl, CURLOPT_URL, "http://api2.ssms.su/");
  16. curl_setopt($curl, CURLOPT_POST, True);
  17. curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
  18. curl_setopt($curl, CURLOPT_RETURNTRANSFER, True);
  19. if(!is_null($cookie)){
  20. curl_setopt($curl, CURLOPT_COOKIE, $cookie);
  21. }
  22. $data = curl_exec($curl);
  23. curl_close($curl);
  24. if($data === False){
  25. return NULL;
  26. }
  27. $js = json_decode($data, $assoc=True);
  28. if(!isset($js['response'])) return NULL;
  29. $rs = &$js['response'];
  30. if(!isset($rs['msg'])) return NULL;
  31. $msg = &$rs['msg'];
  32. if(!isset($msg['err_code'])) return NULL;
  33. $ec = intval($msg['err_code']);
  34. if(!isset($rs['data'])){ $data = NULL; }else{ $data = $rs['data']; }
  35. return array($ec, $data);
  36. }
  37. /**
  38. * Sends a message via ssms_su api, combining authenticating and sending
  39. * message in one request.
  40. * @param $email, $passwrod - login info
  41. * @param $phone - recipient phone number in international format (like 7xxxyyyzzzz)
  42. * @param $text - message text, ASCII or UTF-8.
  43. * @param $params - additional parameters as key => value array, see API doc.
  44. * @return
  45. * * NULL if API communication went a wrong way
  46. * * array(>0) - if an error has occurred (see API error codes)
  47. * * array(0, n_raw_sms, credits) - number of SMS parts in message and
  48. * price for a single part
  49. */
  50. function smsapi_push_msg_nologin($email, $password, $phone, $text, $params = NULL){
  51. $req = array(
  52. "method" => "push_msg",
  53. "api_v"=>"1.1",
  54. "email"=>$email,
  55. "password"=>$password,
  56. "phone"=>$phone,
  57. "text"=>$text);
  58. if(!is_null($params)){
  59. $req = array_merge($req, $params);
  60. }
  61. $resp = _smsapi_communicate($req);
  62. if(is_null($resp)){
  63. // Broken API request
  64. return NULL;
  65. return "";
  66. }
  67. $ec = $resp[0];
  68. if($ec != 0){
  69. return array($ec);
  70. return "";
  71. }
  72. if(!isset($resp[1]['n_raw_sms']) OR !isset($resp[1]['credits'])){
  73. return NULL; // No such fields in response while expected
  74. return "";
  75. }
  76. $n_raw_sms = $resp[1]['n_raw_sms'];
  77. $credits = $resp[1]['credits'];
  78. return array(0, $n_raw_sms, $credits);
  79. return "";
  80. }
  81. /**
  82. * Logs in API, producing a session ID to be sent back in session
  83. * cookie.
  84. * @param $email - user's email
  85. * @param $password - user's password
  86. * @return
  87. * * array(0,$cookie) - if OK, where $cookie is a string
  88. * "sid=${session_id}" to be passed to cURL
  89. * * array(>0) - if API returned an error code (included)
  90. * * NULL is API communication failed
  91. */
  92. function smsapi_login($email, $password){
  93. $req = array("method" => "login",
  94. "format" => "json",
  95. "email" => $email,
  96. "password" => $password);
  97. $ret=_smsapi_communicate($req);
  98. if(is_null($ret)){
  99. return NULL;
  100. }
  101. $ec = $ret[0];
  102. if($ec != 0){
  103. return array($ec);
  104. }
  105. if( ! isset($ret[1]['sid'])){
  106. return NULL;
  107. }
  108. $usid = urlencode($ret[1]['sid']);
  109. $cookie = "sid=$usid";
  110. return array(0,$cookie);
  111. }
  112. /**
  113. * Sends message via API, using previously obtained cookie to
  114. * authenticate.
  115. * @param $cookie - string, returned by smsapi_login, "sid=..."
  116. * @param $phone - target phone
  117. * @param $text - message text, ASCII or UTF-8
  118. * @param params - dictionary of optional parameters, see API
  119. * documentation of push_msg method
  120. * @return
  121. * * NULL if communication to API failed
  122. * * array(>0) if an error occured (see API error codes)
  123. * * array(0, n_raw_sms, credits) - number of SMS parts in message and
  124. * price for a single part
  125. */
  126. function smsapi_push_msg($cookie, $phone, $text, $params = NULL){
  127. $req = array(
  128. "method" => "push_msg",
  129. "api_v"=>"1.1",
  130. "phone"=>$phone,
  131. "text"=>$text);
  132. if(!is_null($params)){
  133. $req = array_merge($req, $params);
  134. }
  135. $resp = _smsapi_communicate($req, $cookie);
  136. if(is_null($resp)){
  137. // Broken API request
  138. return NULL;
  139. }
  140. $ec = $resp[0];
  141. if($ec != 0){
  142. return array($ec);
  143. }
  144. if(!isset($resp[1]['n_raw_sms']) OR !isset($resp[1]['credits'])){
  145. return NULL; // No such fields in response while expected
  146. }
  147. $n_raw_sms = $resp[1]['n_raw_sms'];
  148. $credits = $resp[1]['credits'];
  149. return array(0, $n_raw_sms, $credits);
  150. }
  151. ?>