ServiceSmsV1.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: vowkin
  5. * Date: 2017/5/22
  6. * Time: 21:49
  7. */
  8. namespace app\common\service;
  9. use app\common\model\OrderModel;
  10. use think\Session;
  11. class ServiceSmsV1 extends BaseService
  12. {
  13. private $chuanglan_config='';
  14. public function __construct()
  15. {
  16. //创蓝发送短信接口URL, 请求地址请参考253云通讯自助通平台查看或者询问您的商务负责人获取
  17. $this->chuanglan_config['api_send_url'] = 'http://smssh1.253.com/msg/send/json';
  18. //创蓝变量短信接口URL, 请求地址请参考253云通讯自助通平台查看或者询问您的商务负责人获取
  19. $this->chuanglan_config['API_VARIABLE_URL'] = 'http://xxx/msg/variable/json';
  20. //创蓝短信余额查询接口URL, 请求地址请参考253云通讯自助通平台查看或者询问您的商务负责人获取
  21. $this->chuanglan_config['api_balance_query_url'] = 'http://xxx/msg/balance/json';
  22. //创蓝账号 替换成你自己的账号
  23. $this->chuanglan_config['api_account'] = 'N5737774';
  24. //创蓝密码 替换成你自己的密码
  25. $this->chuanglan_config['api_password'] = 'we7da5QAZ0a3ec';
  26. }
  27. /**南哥,下面的方法就是你需要发送短信的dome 可直接调用发送短信***************************************************************************/
  28. //短信验证码发送
  29. public static function sendCode($mobile){
  30. $clapi = new ServiceSmsV1();
  31. $code = mt_rand(1000,9999);//这是短信验证码。发送成功后返回验证码,需要验证时使用,存session
  32. // $mobile='18355173015';//这是接收者手机号码
  33. $message='您的动态验证码为'.$code.'请在页面输入完成验证。如非本人操作请忽略。';//这里是需要发送的短信模板,参数传递拼接在字符串中
  34. $result = $clapi->sendSMS($mobile, $message);
  35. // print_r($result);exit;
  36. if(!is_null(json_decode($result))){
  37. $output=json_decode($result,true);
  38. if(isset($output['code']) && $output['code']=='0'){
  39. Session::set('code',$code);
  40. return helperService::returnJson(['code'=>200,'msg'=>'发送成功','data'=>['code'=>$code]]);
  41. }else{
  42. echo $output['errorMsg'];
  43. }
  44. }else{
  45. echo $result;
  46. }
  47. }
  48. //短信模板信息发送
  49. public static function sendMessage(){
  50. $clapi = new ServiceSmsV1();
  51. $mobile='15329884885';//这是接收者手机号码
  52. $name='南哥';
  53. $time=date("Y-m-d H:i:s");
  54. $message='你好'.$name.',我正在测试,时间是'.$time;//这里是需要发送的短信模板,参数传递拼接在字符串中
  55. $result = $clapi->sendSMS($mobile, $message);
  56. if(!is_null(json_decode($result))){
  57. $output=json_decode($result,true);
  58. if(isset($output['code']) && $output['code']=='0'){
  59. return helperService::returnJson(['code'=>200,'msg'=>'发送成功','data'=>[]]);
  60. }else{
  61. echo $output['errorMsg'];
  62. }
  63. }else{
  64. echo $result;
  65. }
  66. }
  67. //订单付款成功发送短信
  68. public static function sendPayMessage($orderNo){
  69. $clapi = new ServiceSmsV1();
  70. $orderModel = new OrderModel();
  71. $userInfo = $orderModel->getOrderOne(['order_no'=>$orderNo]);
  72. $mobile=$userInfo['mobile'];//这是接收者手机号码
  73. $message='您好,您的订单'.$orderNo.'已付款成功,请等待发货,如有疑问,请咨询客服';//这里是需要发送的短信模板,参数传递拼接在字符串中
  74. $result = $clapi->sendSMS($mobile, $message);
  75. if(!is_null(json_decode($result))){
  76. $output=json_decode($result,true);
  77. if(isset($output['code']) && $output['code']=='0'){
  78. return helperService::returnJson(['code'=>200,'msg'=>'发送成功','data'=>[]]);
  79. }else{
  80. echo $output['errorMsg'];
  81. }
  82. }else{
  83. echo $result;
  84. }
  85. }
  86. /**南哥,下面的方法不用管,是短信发送的内部方法***************************************************************************/
  87. /**
  88. * 发送短信
  89. * @param $mobile
  90. * @param $msg
  91. * @param string $needstatus
  92. * @return mixed
  93. */
  94. public function sendSMS( $mobile, $msg, $needstatus = 'true') {
  95. //创蓝接口参数
  96. $postArr = array (
  97. 'account' => $this->chuanglan_config['api_account'],
  98. 'password' => $this->chuanglan_config['api_password'],
  99. 'msg' => urlencode($msg),
  100. 'phone' => $mobile,
  101. 'report' => $needstatus
  102. );
  103. $result = $this->curlPost( $this->chuanglan_config['api_send_url'] , $postArr);
  104. return $result;
  105. }
  106. /**
  107. *
  108. * 发送变量短信
  109. * @param $msg
  110. * @param $params
  111. * @return mixed
  112. */
  113. public function sendVariableSMS( $msg, $params) {
  114. //创蓝接口参数
  115. $postArr = array (
  116. 'account' => $this->chuanglan_config['api_account'],
  117. 'password' => $this->chuanglan_config['api_password'],
  118. 'msg' => $msg,
  119. 'params' => $params,
  120. 'report' => 'true'
  121. );
  122. $result = $this->curlPost( $this->chuanglan_config['API_VARIABLE_URL'], $postArr);
  123. return $result;
  124. }
  125. /**
  126. * 查询额度
  127. *
  128. * 查询地址
  129. */
  130. public function queryBalance() {
  131. //查询参数
  132. $postArr = array (
  133. 'account' => $this->chuanglan_config['api_account'],
  134. 'password' => $this->chuanglan_config['api_password'],
  135. );
  136. $result = $this->curlPost($this->chuanglan_config['api_balance_query_url'], $postArr);
  137. return $result;
  138. }
  139. /**
  140. * 通过CURL发送HTTP请求
  141. * @param string $url //请求URL
  142. * @param array $postFields //请求参数
  143. * @return mixed
  144. */
  145. private function curlPost($url,$postFields){
  146. $postFields = json_encode($postFields);
  147. $ch = curl_init ();
  148. curl_setopt( $ch, CURLOPT_URL, $url );
  149. curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
  150. 'Content-Type: application/json; charset=utf-8'
  151. )
  152. );
  153. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
  154. curl_setopt( $ch, CURLOPT_POST, 1 );
  155. curl_setopt( $ch, CURLOPT_POSTFIELDS, $postFields);
  156. curl_setopt( $ch, CURLOPT_TIMEOUT,1);
  157. curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0);
  158. curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0);
  159. $ret = curl_exec ( $ch );
  160. if (false == $ret) {
  161. $result = curl_error( $ch);
  162. } else {
  163. $rsp = curl_getinfo( $ch, CURLINFO_HTTP_CODE);
  164. if (200 != $rsp) {
  165. $result = "请求状态 ". $rsp . " " . curl_error($ch);
  166. } else {
  167. $result = $ret;
  168. }
  169. }
  170. curl_close ( $ch );
  171. return $result;
  172. }
  173. }