ChuanglanSmsApi.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace common\components;
  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. //参数的配置 请登录zz.253.com 获取以下API信息 ↓↓↓↓↓↓↓
  16. const API_SEND_URL='http://smsbj1.253.com/msg/send/json'; //创蓝发送短信接口URL
  17. const API_VARIABLE_URL = 'http://smsbj1.253.com/msg/variable/json';//创蓝变量短信接口URL
  18. const API_BALANCE_QUERY_URL= 'http://XXX/msg/balance/json';//创蓝短信余额查询接口URL
  19. const API_ACCOUNT= 'N2890342'; // 创蓝API账号
  20. const API_PASSWORD= 'abc178c7';// 创蓝API密码
  21. //参数的配置 请登录zz.253.com 获取以上API信息 ↑↑↑↑↑↑↑
  22. /**
  23. * 发送短信
  24. *
  25. * @param string $mobile 手机号码
  26. * @param string $msg 短信内容
  27. * @param string $needstatus 是否需要状态报告
  28. */
  29. public function sendSMS( $mobile, $msg, $needstatus = 'true') {
  30. //创蓝接口参数
  31. $postArr = array (
  32. 'account' => self::API_ACCOUNT,
  33. 'password' => self::API_PASSWORD,
  34. 'msg' => urlencode($msg),
  35. 'phone' => $mobile,
  36. 'report' => $needstatus,
  37. );
  38. $result = $this->curlPost( self::API_SEND_URL, $postArr);
  39. return $result;
  40. }
  41. /**
  42. * 发送变量短信
  43. *
  44. * @param string $msg 短信内容
  45. * @param string $params 最多不能超过1000个参数组
  46. */
  47. public function sendVariableSMS( $msg, $params) {
  48. //创蓝接口参数
  49. $postArr = array (
  50. 'account' => self::API_ACCOUNT,
  51. 'password' => self::API_PASSWORD,
  52. 'msg' => $msg,
  53. 'params' => $params,
  54. 'report' => 'true'
  55. );
  56. $result = $this->curlPost( self::API_VARIABLE_URL, $postArr);
  57. return $result;
  58. }
  59. /**
  60. * 查询额度
  61. *
  62. * 查询地址
  63. */
  64. public function queryBalance() {
  65. //查询参数
  66. $postArr = array (
  67. 'account' => self::API_ACCOUNT,
  68. 'password' => self::API_PASSWORD,
  69. );
  70. $result = $this->curlPost(self::API_BALANCE_QUERY_URL, $postArr);
  71. return $result;
  72. }
  73. /**
  74. * 通过CURL发送HTTP请求
  75. * @param string $url //请求URL
  76. * @param array $postFields //请求参数
  77. * @return mixed
  78. *
  79. */
  80. private function curlPost($url,$postFields){
  81. $postFields = json_encode($postFields);
  82. $ch = curl_init ();
  83. curl_setopt( $ch, CURLOPT_URL, $url );
  84. curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
  85. 'Content-Type: application/json; charset=utf-8' //json版本需要填写 Content-Type: application/json;
  86. )
  87. );
  88. curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); //若果报错 name lookup timed out 报错时添加这一行代码
  89. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
  90. curl_setopt( $ch, CURLOPT_POST, 1 );
  91. curl_setopt( $ch, CURLOPT_POSTFIELDS, $postFields);
  92. curl_setopt( $ch, CURLOPT_TIMEOUT,60);
  93. curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0);
  94. curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0);
  95. $ret = curl_exec ( $ch );
  96. if (false == $ret) {
  97. $result = curl_error( $ch);
  98. } else {
  99. $rsp = curl_getinfo( $ch, CURLINFO_HTTP_CODE);
  100. if (200 != $rsp) {
  101. $result = "请求状态 ". $rsp . " " . curl_error($ch);
  102. } else {
  103. $result = $ret;
  104. }
  105. }
  106. curl_close ( $ch );
  107. return $result;
  108. }
  109. }
  110. ?>