JGSmsService.php 3.8 KB

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