SmsService.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace app\index\service;
  3. use think\Config;
  4. class SmsService extends BaseService
  5. {
  6. const code_url = 'https://api.sms.jpush.cn/v1';
  7. private $appKey = '7ec8cf35b83d1f5fa23b9e4e';
  8. private $masterSecret = '237e321c3b7067e2a634470c';
  9. private $options;
  10. public function __construct(array $options = array()) {
  11. $this->options = array_merge(array(
  12. 'ssl_verify'=> false
  13. ), $options);
  14. $this->appKey = Config::get('SmsConfig')['user'];
  15. $this->masterSecret = Config::get('SmsConfig')['password'];
  16. }
  17. public function sendCode($mobile, $temp_id = 1) {
  18. $url = self::code_url . '/codes';
  19. $body = array('mobile' => $mobile, 'temp_id' => $temp_id);
  20. $res = $this->sendPost($url, $body);
  21. if(!empty($res['http_code']) && $res['http_code'] == 200){
  22. $body = json_decode($res['body'], true);
  23. if(isset($body['error'])){
  24. HelperService::returnJson(['code'=>400,'msg'=>'send fail','data'=>null]);
  25. }
  26. //1.添加一个短信记录 $mobile手机号 $body['msg_id']返回得内容 $temp_id传得数据
  27. $res['msg_id']=$body['msg_id'];
  28. $res['status']=true;
  29. return $res;
  30. }
  31. $res['status'] =false;
  32. return $res;
  33. }
  34. public function sendBatchCode($recipients, $temp_id = 1) {
  35. $url = self::code_url . '/messages/batch';
  36. $body = array('recipients' => $recipients, 'temp_id' => $temp_id);
  37. $res = $this->sendPost($url, $body);
  38. file_put_contents('sendmobile.jg',json_encode($res)."\n",FILE_APPEND);
  39. return $res;
  40. }
  41. public function sendMessage($mobile, $temp_id = 1,$temp_para=[]) {
  42. $url = self::code_url . '/messages';
  43. $body = array('mobile' => $mobile, 'temp_id' => $temp_id,'temp_para'=>$temp_para);
  44. $res = $this->sendPost($url, $body);
  45. if(!empty($res['http_code']) && $res['http_code'] == 200){
  46. $body = json_decode($res['body'], true);
  47. if(isset($body['error'])){
  48. HelperService::returnJson(['code'=>400,'msg'=>'send fail','data'=>null]);
  49. }
  50. //添加一个短信记录
  51. return $res;
  52. }
  53. HelperService::returnJson(['code'=>400,'msg'=>'send fail','data'=>$res]);
  54. }
  55. public function checkCode($code,$msg_id) {
  56. if(empty($msg_id)){
  57. return false;
  58. }
  59. $url = self::code_url . '/codes/' . $msg_id . "/valid";
  60. $body = array('code' => $code);
  61. $res = $this->sendPost($url, $body);
  62. $arrRes = json_decode($res['body'], true);
  63. return $arrRes['is_valid'];
  64. }
  65. private function sendPost($url, $body) {
  66. $ch = curl_init();
  67. $options = array(
  68. CURLOPT_RETURNTRANSFER => true,
  69. CURLOPT_HEADER => true,
  70. CURLOPT_HTTPHEADER => array(
  71. 'Content-Type: application/json',
  72. 'Connection: Keep-Alive'
  73. ),
  74. CURLOPT_USERAGENT => 'JSMS-API-PHP-CLIENT',
  75. CURLOPT_CONNECTTIMEOUT => 20,
  76. CURLOPT_TIMEOUT => 120,
  77. CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
  78. CURLOPT_USERPWD => $this->appKey . ":" . $this->masterSecret,
  79. CURLOPT_URL => $url,
  80. CURLOPT_POST => true,
  81. CURLOPT_POSTFIELDS => json_encode($body)
  82. );
  83. if (!$this->options['ssl_verify']) {
  84. $options[CURLOPT_SSL_VERIFYPEER] = false;
  85. $options[CURLOPT_SSL_VERIFYHOST] = 0;
  86. }
  87. curl_setopt_array($ch, $options);
  88. $output = curl_exec($ch);
  89. if($output === false) {
  90. return "Error Code:" . curl_errno($ch) . ", Error Message:".curl_error($ch);
  91. } else {
  92. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  93. $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
  94. $header_text = substr($output, 0, $header_size);
  95. $body = substr($output, $header_size);
  96. $response['body'] = $body;
  97. $response['http_code'] = $httpCode;
  98. }
  99. curl_close($ch);
  100. return $response;
  101. }
  102. }