当前位置:Gxlcms > PHP教程 > php短信接口代码_PHP

php短信接口代码_PHP

时间:2021-07-01 10:21:17 帮助过:50人阅读

本文实例为大家分享了几个常用的php短信接口代码,供大家参考,具体内容如下

1. 短信调用class

  1. <?php
  2. /**
  3. * User: Administrator
  4. * Date: 2016/5/8 0008
  5. * Time: 下午 2:36
  6. */
  7. class Sms{
  8. //Luosimao api key
  9. private $_api_key = '';
  10. private $_last_error = array();
  11. private $_use_ssl = FALSE;
  12. private $_ssl_api_url = array(
  13. 'send' => 'http://www.bitsCN.com/v1/send.json',
  14. 'send_batch' => 'http://www.bitsCN.com/v1/send_batch.json',
  15. 'status' => 'http://www.bitsCN.com/v1/status.json',
  16. );
  17. private $_api_url = array(
  18. 'send' => 'http://www.bitsCN.com/v1/send.json',
  19. 'send_batch' => 'http://www.bitsCN.com/send_batch.json',
  20. 'status' => 'http://www.bitsCN.com/v1/status.json',
  21. );
  22. /**
  23. * @param array $param 配置参数
  24. * api_key api秘钥,在luosimao短信后台短信->触发发送下面可查看
  25. * use_ssl 启用HTTPS地址,HTTPS有一定性能损耗,可选,默认不启用
  26. */
  27. public function __construct( $param = array() ){
  28. if( !isset( $param['api_key'] ) ){
  29. die("api key error.");
  30. }
  31. if( isset( $param['api_key'] ) ){
  32. $this->_api_key = $param['api_key'];
  33. }
  34. if( isset( $param['use_ssl'] ) ){
  35. $this->_use_ssl = $param['use_ssl'];
  36. }
  37. }
  38. //触发,单发,适用于验证码,订单触发提醒类
  39. public function send( $mobile , $message = '' ){
  40. $api_url = !$this->_use_ssl ? $this->_api_url['send'] : $this->_ssl_api_url['send'];
  41. $param = array(
  42. 'mobile' => $mobile ,
  43. 'message' => $message,
  44. );
  45. $res = $this->http_post( $api_url ,$param );
  46. return @json_decode( $res ,TRUE );
  47. }
  48. //批量发送,用于大批量发送
  49. public function send_batch( $mobile_list = array() , $message = array() , $time = '' ){
  50. $api_url = !$this->_use_ssl ? $this->_api_url['send_batch'] : $this->_ssl_api_url['send_batch'];
  51. $mobile_list = is_array( $mobile_list ) ? implode( ',' , $mobile_list ) : $mobile_list;
  52. $param = array(
  53. 'mobile_list' => $mobile_list ,
  54. 'message' => $message,
  55. 'time' => $time,
  56. );
  57. $res = $this->http_post( $api_url ,$param );
  58. return @json_decode( $res ,TRUE );
  59. }
  60. //获取短信账号余额
  61. public function get_deposit(){
  62. $api_url = !$this->_use_ssl ? $this->_api_url['status'] : $this->_ssl_api_url['status'];
  63. $res = $this->http_get( $api_url );
  64. return @json_decode( $res ,TRUE );
  65. }
  66. /**
  67. * @param string $type 接收类型,用于在服务器端接收上行和发送状态,接收地址需要在luosimao后台设置
  68. * @param array $param 传入的参数,从推送的url中获取,官方文档:https://luosimao.com/docs/api/
  69. */
  70. public function recv( $type = 'status' , $param = array() ){
  71. if( $type == 'status' ){
  72. if( $param['batch_id'] && $param['mobile'] && $param['status'] ){ //状态
  73. // do record
  74. }
  75. }elseif( $type == 'incoming' ){ //上行回复
  76. if( $param['mobile'] && $param['message'] ){
  77. // do record
  78. }
  79. }
  80. }
  81. /**
  82. * @param string $api_url 接口地址
  83. * @param array $param post参数
  84. * @param int $timeout 超时时间
  85. * @return bool
  86. */
  87. private function http_post( $api_url = '' , $param = array() , $timeout = 5 ){
  88. if( !$api_url ){
  89. die("error api_url");
  90. }
  91. $ch = curl_init();
  92. curl_setopt( $ch, CURLOPT_URL, $api_url );
  93. curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_0 );
  94. curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
  95. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE);
  96. curl_setopt( $ch, CURLOPT_HEADER, FALSE);
  97. if( parse_url( $api_url )['scheme'] == 'https' ){
  98. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST , FALSE);
  99. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER , FALSE);
  100. }
  101. curl_setopt( $ch, CURLOPT_HTTPAUTH , CURLAUTH_BASIC);
  102. curl_setopt( $ch, CURLOPT_USERPWD , 'api:key-'.$this->_api_key );
  103. curl_setopt( $ch, CURLOPT_POST, TRUE);
  104. curl_setopt( $ch, CURLOPT_POSTFIELDS, $param );
  105. $res = curl_exec( $ch );
  106. $error = curl_error( $ch );
  107. curl_close( $ch );
  108. if( $error ){
  109. $this->_last_error[] = $error;
  110. return FALSE;
  111. }
  112. return $res;
  113. }
  114. /**
  115. * @param string $api_url 接口地址
  116. * @param string $timeout 超时时间
  117. * @return bool
  118. */
  119. private function http_get( $api_url = '' , $timeout = '' ){
  120. if( !$api_url ){
  121. die("error api_url");
  122. }
  123. $ch = curl_init();
  124. curl_setopt( $ch, CURLOPT_URL, $api_url );
  125. curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_0 );
  126. curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
  127. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE);
  128. curl_setopt( $ch, CURLOPT_HEADER, FALSE);
  129. if( parse_url( $api_url )['scheme'] == 'https' ){
  130. curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST , FALSE);
  131. curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER , FALSE);
  132. }
  133. curl_setopt( $ch, CURLOPT_HTTPAUTH , CURLAUTH_BASIC);
  134. curl_setopt( $ch, CURLOPT_USERPWD , 'api:key-'.$this->_api_key );
  135. $res = curl_exec( $ch );
  136. $error = curl_error( $ch );
  137. curl_close( $ch );
  138. if( $error ){
  139. $this->_last_error[] = curl_error( $ch );
  140. return FALSE;
  141. }
  142. return $res;
  143. }
  144. public function last_error(){
  145. return $this->_last_error;
  146. }
  147. }

2.短信发送示例

  1. //send 单发接口
  2. require 'sms.php';
  3. $sms = new Sms( array('api_key' => '86f52f3ce0647dc24da53eafe29fadd4' , 'use_ssl' => FALSE ) );
  4. $res = $sms->send_batch( array('13761428268') , '验证码:19272【】');
  5. if( $res ){
  6. if( isset( $res['error'] ) && $res['error'] == 0 ){
  7. echo 'success';
  8. }else{
  9. echo 'failed,code:'.$res['error'].',msg:'.$res['msg'];
  10. }
  11. }else{
  12. var_dump( $sms->last_error() );
  13. }
  14. exit;


3.批量发送示例

  1. require 'sms.php';
  2. $sms = new Sms( array('api_key' => '86f52f3ce0647dc24da53eafe29fadd4' , 'use_ssl' => FALSE ) );
  3. //send 单发接口
  4. $res = $sms->send_batch( array('13761428268') , '验证码:19272【】');
  5. if( $res ){
  6. if( isset( $res['error'] ) && $res['error'] == 0 ){
  7. echo 'success';
  8. }else{
  9. echo 'failed,code:'.$res['error'].',msg:'.$res['msg'];
  10. }
  11. }else{
  12. var_dump( $sms->last_error() );
  13. }
  14. exit;

4.获取余额示例

  1. //deposit 余额查询
  2. require 'sms.php';
  3. $sms = new Sms( array('api_key' => '86f52f3ce0647dc24da53eafe29fadd4' , 'use_ssl' => FALSE ) );
  4. $res = $sms->get_deposit();
  5. if( $res ){
  6. if( isset( $res['error'] ) && $res['error'] == 0 ){
  7. echo 'desposit:'.$res['deposit'];
  8. }else{
  9. echo 'failed,code:'.$res['error'].',msg:'.$res['msg'];
  10. }
  11. }else{
  12. var_dump( $sms->last_error() );
  13. }
  14. exit;

以上就是本文的全部内容,希望对大家的学习有所帮助。

人气教程排行