ChuanglanSmsApi.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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"]; //拒绝短信名单
  26. if (in_array($_SERVER["HTTP_HOST"], $deny_list))
  27. return false; //测试不发送信息
  28. $result = $this->curlPost(self::API_SEND_URL, $postArr);
  29. return $result;
  30. }
  31. /**
  32. * 通过CURL发送HTTP请求
  33. * @param string $url //请求URL
  34. * @param array $postFields //请求参数
  35. * @return mixed
  36. *
  37. */
  38. private function curlPost($url, $postFields) {
  39. $postFields = json_encode($postFields);
  40. $ch = curl_init();
  41. curl_setopt($ch, CURLOPT_URL, $url);
  42. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  43. 'Content-Type: application/json; charset=utf-8' //json版本需要填写 Content-Type: application/json;
  44. )
  45. );
  46. curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); //若果报错 name lookup timed out 报错时添加这一行代码
  47. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  48. curl_setopt($ch, CURLOPT_POST, 1);
  49. curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
  50. curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  51. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  52. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  53. $ret = curl_exec($ch);
  54. if (false == $ret) {
  55. $result = curl_error($ch);
  56. } else {
  57. $rsp = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  58. if (200 != $rsp) {
  59. $result = "请求状态 " . $rsp . " " . curl_error($ch);
  60. } else {
  61. $result = $ret;
  62. }
  63. }
  64. curl_close($ch);
  65. return $result;
  66. }
  67. }