ChuanglanSmsApi.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace app\common\api;
  3. class ChuanglanSmsApi {
  4. //参数的配置请登录zz.253.com获取以下API信息↓↓↓↓↓↓↓
  5. const API_SEND_URL = 'http://smssh1.253.com/msg/send/json'; //创蓝发送短信接口URL
  6. const API_ACCOUNT = 'N7450217'; //创蓝API账号
  7. const API_PASSWORD = 'wS5gE4GQszb46f'; //创蓝API密码
  8. private $verification;
  9. /**
  10. * 发送短信
  11. *
  12. * @param string $mobile 手机号码
  13. * @param string $msg 短信内容
  14. * @param string $needstatus 是否需要状态报告
  15. */
  16. public function sendSMS($mobile, $msg, $needstatus = 'true') {
  17. //创蓝接口参数
  18. $postArr = array(
  19. 'account' => self::API_ACCOUNT,
  20. 'password' => self::API_PASSWORD,
  21. 'msg' => urlencode($msg),
  22. 'phone' => $mobile,
  23. 'report' => $needstatus,
  24. );
  25. $deny_list = ["jjrcw.test", "jjrcw.test:8080","report.com","report.jinjianghc.com"]; //拒绝短信名单
  26. if (in_array($_SERVER["HTTP_HOST"], $deny_list))
  27. return false; //测试不发送信息
  28. $redis = \app\common\Redis::instance(\think\facade\Config::get("cache.stores.redis.select"));
  29. $unique = sha1($mobile.$msg);
  30. $time = $redis->get($unique);
  31. $now = time();
  32. if(!empty($time) && $now-$time < 10800){
  33. return false;//3小时内同一个手机号码同一条信息不重复发送
  34. }
  35. $result = $this->curlPost(self::API_SEND_URL, $postArr);
  36. return $result;
  37. }
  38. /**
  39. * 通过CURL发送HTTP请求
  40. * @param string $url //请求URL
  41. * @param array $postFields //请求参数
  42. * @return mixed
  43. *
  44. */
  45. private function curlPost($url, $postFields) {
  46. $postFields = json_encode($postFields);
  47. $ch = curl_init();
  48. curl_setopt($ch, CURLOPT_URL, $url);
  49. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  50. 'Content-Type: application/json; charset=utf-8' //json版本需要填写 Content-Type: application/json;
  51. )
  52. );
  53. curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); //若果报错 name lookup timed out 报错时添加这一行代码
  54. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  55. curl_setopt($ch, CURLOPT_POST, 1);
  56. curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
  57. curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  58. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  59. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  60. $ret = curl_exec($ch);
  61. if (false == $ret) {
  62. $result = curl_error($ch);
  63. } else {
  64. $rsp = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  65. if (200 != $rsp) {
  66. $result = "请求状态 " . $rsp . " " . curl_error($ch);
  67. } else {
  68. $result = $ret;
  69. }
  70. }
  71. curl_close($ch);
  72. return $result;
  73. }
  74. }