ChuanglanSmsApi.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. $redis->set($unique,$now,12000);
  36. $result = $this->curlPost(self::API_SEND_URL, $postArr);
  37. return $result;
  38. }
  39. /**
  40. * 通过CURL发送HTTP请求
  41. * @param string $url //请求URL
  42. * @param array $postFields //请求参数
  43. * @return mixed
  44. *
  45. */
  46. private function curlPost($url, $postFields) {
  47. $postFields = json_encode($postFields);
  48. $ch = curl_init();
  49. curl_setopt($ch, CURLOPT_URL, $url);
  50. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  51. 'Content-Type: application/json; charset=utf-8' //json版本需要填写 Content-Type: application/json;
  52. )
  53. );
  54. curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); //若果报错 name lookup timed out 报错时添加这一行代码
  55. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  56. curl_setopt($ch, CURLOPT_POST, 1);
  57. curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
  58. curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  59. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  60. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  61. $ret = curl_exec($ch);
  62. if (false == $ret) {
  63. $result = curl_error($ch);
  64. } else {
  65. $rsp = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  66. if (200 != $rsp) {
  67. $result = "请求状态 " . $rsp . " " . curl_error($ch);
  68. } else {
  69. $result = $ret;
  70. }
  71. }
  72. curl_close($ch);
  73. return $result;
  74. }
  75. }