Qxysms.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. $response = curl_post($this->host . '/sms/batch/v1', json_encode($data),['Content-type:application/json']);
  35. //dump($response);exit;
  36. $response = json_decode($response,true);
  37. if(is_array($response) && isset($response['code']) && $response['code'] == '00000'){
  38. return true;
  39. }
  40. return false;
  41. }
  42. protected function ms_time()
  43. {
  44. list($ms, $sec) = explode(' ', microtime());
  45. return intval((floatval($ms) + floatval($sec)) * 1000);
  46. }
  47. /**
  48. * @param $url
  49. * @param array $params
  50. * @param array $header
  51. * @return PromiseInterface|Response
  52. */
  53. protected function postJson($url, array $params = [], array $header = [])
  54. {
  55. return Http::withHeaders($header)->acceptJson()->post($this->host . $url, $params);
  56. }
  57. }