ChuanglanSmsApi.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace plugins\eyunzhu_mobile_code\ChuanglanSmsHelper;
  3. header("Content-type:text/html; charset=UTF-8");
  4. /* *
  5. * 类名:ChuanglanSmsApi
  6. * 功能:创蓝接口请求类
  7. * 详细:构造创蓝短信接口请求,获取远程HTTP数据
  8. * 版本:1.3
  9. * 日期:2017-04-12
  10. * 说明:
  11. * 以下代码只是为了方便客户测试而提供的样例代码,客户可以根据自己网站的需要,按照技术文档自行编写,并非一定要使用该代码。
  12. * 该代码仅供学习和研究创蓝接口使用,只是提供一个参考。
  13. */
  14. class ChuanglanSmsApi
  15. {
  16. //参数的配置 请登录zz.253.com 获取以下API信息 ↓↓↓↓↓↓↓
  17. const API_SEND_URL = 'http://smssh1.253.com/msg/send/json'; //创蓝发送短信接口URL
  18. const API_VARIABLE_URL = 'http://smssh1.253.com/msg/variable/json';//创蓝变量短信接口URL
  19. const API_BALANCE_QUERY_URL = 'http://smssh1.253.com/msg/balance/json';//创蓝短信余额查询接口URL
  20. const API_ACCOUNT = ''; // 创蓝API账号
  21. const API_PASSWORD = '';// 创蓝API密码
  22. //参数的配置 请登录zz.253.com 获取以上API信息 ↑↑↑↑↑↑↑
  23. /**
  24. * 发送短信
  25. * @param string $mobile 手机号码
  26. * @param string $msg 短信内容
  27. * @param string $needstatus 是否需要状态报告
  28. */
  29. public function sendSMS($config, $mobile, $msg, $needstatus = 'true')
  30. {
  31. //创蓝接口参数
  32. $postArr = [
  33. 'account' => $config["API_ACCOUNT"],
  34. 'password' => $config["API_PASSWORD"],
  35. 'msg' => urlencode($msg),
  36. 'phone' => $mobile,
  37. 'report' => $needstatus,
  38. ];
  39. $result = $this->curlPost($config["API_SEND_URL"], $postArr);
  40. return $result;
  41. }
  42. /**
  43. * 发送变量短信
  44. *
  45. * @param string $msg 短信内容
  46. * @param string $params 最多不能超过1000个参数组
  47. */
  48. public function sendVariableSMS($config, $msg, $params)
  49. {
  50. //创蓝接口参数
  51. $postArr = [
  52. 'account' => $config["API_ACCOUNT"],
  53. 'password' => $config["API_PASSWORD"],
  54. 'msg' => $msg,
  55. 'params' => $params,
  56. 'report' => 'true',
  57. ];
  58. $result = $this->curlPost($config["API_VARIABLE_URL"], $postArr);
  59. return $result;
  60. }
  61. /**
  62. * 查询额度
  63. *
  64. * 查询地址
  65. */
  66. public function queryBalance($config)
  67. {
  68. //查询参数
  69. $postArr = [
  70. 'account' => $config["API_ACCOUNT"],
  71. 'password' => $config["API_PASSWORD"],
  72. ];
  73. $result = $this->curlPost($config["API_BALANCE_QUERY_URL"], $postArr);
  74. return $result;
  75. }
  76. /**
  77. * 通过CURL发送HTTP请求
  78. * @param string $url //请求URL
  79. * @param array $postFields //请求参数
  80. * @return mixed
  81. *
  82. */
  83. private function curlPost($url, $postFields)
  84. {
  85. $postFields = json_encode($postFields);
  86. $ch = curl_init();
  87. curl_setopt($ch, CURLOPT_URL, $url);
  88. curl_setopt($ch, CURLOPT_HTTPHEADER, [
  89. 'Content-Type: application/json; charset=utf-8' //json版本需要填写 Content-Type: application/json;
  90. ]
  91. );
  92. curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); //若果报错 name lookup timed out 报错时添加这一行代码
  93. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  94. curl_setopt($ch, CURLOPT_POST, 1);
  95. curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
  96. curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  97. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  98. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  99. $ret = curl_exec($ch);
  100. if (false == $ret) {
  101. $result = curl_error($ch);
  102. } else {
  103. $rsp = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  104. if (200 != $rsp) {
  105. $result = "请求状态 " . $rsp . " " . curl_error($ch);
  106. } else {
  107. $result = $ret;
  108. }
  109. }
  110. curl_close($ch);
  111. return $result;
  112. }
  113. }
  114. ?>