您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Skebby.class.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. define("MESSAGE_HIGH_QUALITY", "GP");
  3. define("MESSAGE_MEDIUM_QUALITY", "TI");
  4. define("MESSAGE_LOW_QUALITY", "SI");
  5. class Skebby {
  6. public $username = null;
  7. public $password = null;
  8. public $endpoint = null;
  9. public $messageType = null;
  10. public $returnCredits = null;
  11. public $sender = null;
  12. private $auth = null;
  13. function __construct($username, $password, $endpoint, $sender='') {
  14. $this->endpoint = $endpoint;
  15. $this->username = $username;
  16. $this->password = $password;
  17. $this->messageType = MESSAGE_HIGH_QUALITY;
  18. $this->returnCredits = true;
  19. $this->sender = $sender;
  20. }
  21. /**
  22. * Authenticates the user given it's username and password.
  23. * Returns the pair user_key, Session_key
  24. */
  25. public function login() {
  26. $ch = curl_init();
  27. curl_setopt($ch, CURLOPT_URL, $this->endpoint .
  28. 'login?username=' . $this->username .
  29. '&password=' . $this->password);
  30. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  31. $response = curl_exec($ch);
  32. $info = curl_getinfo($ch);
  33. curl_close($ch);
  34. if ($info['http_code'] != 200) {
  35. return null;
  36. }
  37. $auth = explode(";", $response);
  38. $this->auth = $auth;
  39. //file_put_contents('/home/aosga/skebby.log', $this->auth);
  40. return $auth;
  41. }
  42. /**
  43. * Sends an SMS message
  44. */
  45. public function sendSMS($message='', $recipients=[], $auth=[]) {
  46. $auth = empty($auth) ? $this->auth : $auth;
  47. if (empty($recipients)) {
  48. return ['result'=>'ERR', 'httpCode'=>0, 'bodyMsg'=>'No recipients provided'];
  49. } else {
  50. $recipients = !is_array($recipients) ? [$recipients] : $recipients;
  51. }
  52. foreach($recipients as $index => $recipient) {
  53. $recipients[$index] = $this->prepareNumber($recipient);
  54. }
  55. $ch = curl_init();
  56. curl_setopt($ch, CURLOPT_URL, $this->endpoint . 'sms');
  57. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  58. 'Content-type: application/json',
  59. 'user_key: ' . $auth[0],
  60. 'Session_key: ' . $auth[1]
  61. ));
  62. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  63. curl_setopt($ch, CURLOPT_POST, 1);
  64. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($this->prepareStructure($message, $recipients)));
  65. $response = curl_exec($ch);
  66. $info = curl_getinfo($ch);
  67. curl_close($ch);
  68. if ($info['http_code'] != 201) {
  69. file_put_contents('/home/aosga/skebby.log', $response);
  70. return ['result'=>'ERR', 'httpCode'=>$info['http_code'], 'bodyMsg'=>$response];
  71. } else {
  72. file_put_contents('/home/aosga/skebby.log', $response);
  73. return json_decode($response, true);
  74. }
  75. }
  76. /**
  77. * Receive new SMS messages
  78. */
  79. public function receiveSMS($idSim='', $limit=100, $auth=[]) {
  80. $auth = empty($auth) ? $this->auth : $auth;
  81. $ch = curl_init();
  82. curl_setopt($ch, CURLOPT_URL, $this->endpoint . 'newsrsmsmessage/' . $idSim . '?limit=' . $limit);
  83. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  84. 'Content-type: application/json',
  85. 'user_key: ' . $auth[0],
  86. 'Session_key: ' . $auth[1]
  87. ));
  88. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  89. $response = curl_exec($ch);
  90. $info = curl_getinfo($ch);
  91. curl_close($ch);
  92. if ($info['http_code'] != 200) {
  93. //return null;
  94. return ['result'=>'ERR', 'httpCode'=>$info['http_code'], 'bodyMsg'=>$response];
  95. }
  96. return json_decode($response, true);
  97. }
  98. private function prepareStructure($message='', $recipients=[]) {
  99. return [
  100. 'message' => $message,
  101. 'message_type' => $this->messageType,
  102. 'returnCredits' => $this->returnCredits,
  103. 'recipient' => $recipients,
  104. 'sender' => $this->sender
  105. ];
  106. }
  107. private function prepareNumber($number='') {
  108. $number = trim(preg_replace('/[^0-9]/', '', $number));
  109. $number = strpos('+', $number) === false ? '+'.$number : $number;
  110. return $number;
  111. }
  112. }