Qxysms.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace app\common\library;
  3. class Qxysms
  4. {
  5. private $host = '';
  6. private $appKey = '';
  7. private $appSecret = '';
  8. private $appCode = '';
  9. private $signName = '';
  10. public function __construct($config){
  11. $this->host = $config['host'];
  12. $this->appKey = $config['appKey'];
  13. $this->appSecret = $config['appSecret'];
  14. $this->appCode = $config['appCode'];
  15. $this->signName = $config['signName'];
  16. }
  17. /**
  18. * 发送验证码
  19. * @param string $mobile
  20. * @param $code
  21. * @return bool
  22. */
  23. public function send(string $mobile, $code)
  24. {
  25. $timestamp = $this->ms_time();
  26. $data = [
  27. 'sign' => md5("{$this->appKey}{$this->appSecret}{$timestamp}"),
  28. 'timestamp' => $timestamp,
  29. 'phone' => $mobile,
  30. 'appcode' => $this->appCode,
  31. 'appkey' => $this->appKey,
  32. 'msg' => "【{$this->signName}】您的验证码是{$code},请妥善保管"
  33. ];
  34. $header = ['Content-type:application/json'];
  35. $response = curl_post($this->host . '/sms/batch/v1', json_encode($data),$header);
  36. //dump($response);exit;
  37. $response = json_decode($response,true);
  38. if(is_array($response) && isset($response['code']) && $response['code'] == '00000'){
  39. return true;
  40. }
  41. return false;
  42. }
  43. protected function ms_time()
  44. {
  45. list($ms, $sec) = explode(' ', microtime());
  46. return intval((floatval($ms) + floatval($sec)) * 1000);
  47. }
  48. }