CLSms.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace app\common\service;
  3. class CLSms
  4. {
  5. private $chuanglan_config=[];
  6. public function __construct($api_account,$api_password)
  7. {
  8. //创蓝发送短信接口URL, 请求地址请参考253云通讯自助通平台查看或者询问您的商务负责人获取
  9. $this->chuanglan_config['api_send_url'] = 'http://smssh1.253.com/msg/send/json';
  10. //创蓝变量短信接口URL, 请求地址请参考253云通讯自助通平台查看或者询问您的商务负责人获取
  11. $this->chuanglan_config['API_VARIABLE_URL'] = 'http://smssh1.253.com/msg/variable/json';
  12. //创蓝短信余额查询接口URL, 请求地址请参考253云通讯自助通平台查看或者询问您的商务负责人获取
  13. $this->chuanglan_config['api_balance_query_url'] = 'http://smssh1.253.com/msg/balance/json';
  14. //创蓝账号 替换成你自己的账号
  15. $this->chuanglan_config['api_account'] = $api_account;
  16. //创蓝密码 替换成你自己的密码
  17. $this->chuanglan_config['api_password'] = $api_password;
  18. }
  19. /**
  20. * 发送短信
  21. * @param $mobile
  22. * @param $msg
  23. * @param string $needstatus
  24. * @return mixed
  25. */
  26. public function sendSMS($mobile,$msg, $needstatus = 'true') {
  27. //创蓝接口参数
  28. $postArr = array (
  29. 'account' => $this->chuanglan_config['api_account'],
  30. 'password' => $this->chuanglan_config['api_password'],
  31. 'msg' => urlencode($msg),
  32. 'phone' => $mobile,
  33. 'report' => $needstatus
  34. );
  35. $result = $this->curlPost($this->chuanglan_config['api_send_url'] , $postArr);
  36. return $result;
  37. }
  38. /**
  39. *
  40. * 发送变量短信
  41. * @param $msg
  42. * @param $params
  43. * @return mixed
  44. */
  45. private function sendVariableSMS( $msg, $params) {
  46. //创蓝接口参数
  47. $postArr = array (
  48. 'account' => $this->chuanglan_config['api_account'],
  49. 'password' => $this->chuanglan_config['api_password'],
  50. 'msg' => $msg,
  51. 'params' => $params,
  52. 'report' => 'true'
  53. );
  54. $result = $this->curlPost( $this->chuanglan_config['API_VARIABLE_URL'], $postArr);
  55. return $result;
  56. }
  57. /**
  58. * 查询额度
  59. *
  60. * 查询地址
  61. */
  62. private function queryBalance() {
  63. //查询参数
  64. $postArr = array (
  65. 'account' => $this->chuanglan_config['api_account'],
  66. 'password' => $this->chuanglan_config['api_password'],
  67. );
  68. $result = $this->curlPost($this->chuanglan_config['api_balance_query_url'], $postArr);
  69. return $result;
  70. }
  71. /**
  72. * 通过CURL发送HTTP请求
  73. * @param string $url //请求URL
  74. * @param array $postFields //请求参数
  75. * @return mixed
  76. */
  77. private function curlPost($url,$postFields){
  78. $postFields = json_encode($postFields);
  79. $ch = curl_init ();
  80. curl_setopt( $ch, CURLOPT_URL, $url );
  81. curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
  82. 'Content-Type: application/json; charset=utf-8'
  83. )
  84. );
  85. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
  86. curl_setopt( $ch, CURLOPT_POST, 1 );
  87. curl_setopt( $ch, CURLOPT_POSTFIELDS, $postFields);
  88. curl_setopt( $ch, CURLOPT_TIMEOUT,1);
  89. curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0);
  90. curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0);
  91. $ret = curl_exec($ch);
  92. if (false == $ret) {
  93. $result = curl_error($ch);
  94. } else {
  95. $rsp = curl_getinfo( $ch, CURLINFO_HTTP_CODE);
  96. if (200 != $rsp) {
  97. $result = "请求状态 ". $rsp . " " . curl_error($ch);
  98. } else {
  99. $result = $ret;
  100. }
  101. }
  102. curl_close($ch);
  103. return $result;
  104. }
  105. }