激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - PHP教程 - php短信接口代碼

php短信接口代碼

2021-01-18 14:09取個名字想半天 PHP教程

這篇文章主要為大家詳細介紹了php短信接口代碼,php短信發送、php批量發送、php獲取余額等代碼,感興趣的小伙伴們可以參考一下

本文實例為大家分享了幾個常用的php短信接口代碼,供大家參考,具體內容如下

1. 短信調用class    

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/**
 * User: Administrator
 * Date: 2016/5/8 0008
 * Time: 下午 2:36
 */
class Sms{
 
 
  //Luosimao api key
  private $_api_key = '';
 
  private $_last_error = array();
 
 
  private $_use_ssl = FALSE;
 
  private $_ssl_api_url = array(
    'send'    => 'http://www.zmynmublwnt.cn/v1/send.json',
    'send_batch' => 'http://www.zmynmublwnt.cn/v1/send_batch.json',
    'status'   => 'http://www.zmynmublwnt.cn/v1/status.json',
  );
 
  private $_api_url = array(
    'send'    => 'http://www.zmynmublwnt.cn/v1/send.json',
    'send_batch' => 'http://www.zmynmublwnt.cn/send_batch.json',
    'status'   => 'http://www.zmynmublwnt.cn/v1/status.json',
  );
 
  /**
   * @param array $param 配置參數
   * api_key api秘鑰,在luosimao短信后臺短信->觸發發送下面可查看
   * use_ssl 啟用HTTPS地址,HTTPS有一定性能損耗,可選,默認不啟用
   */
  public function __construct( $param = array() ){
 
    if( !isset( $param['api_key'] ) ){
      die("api key error.");
    }
 
    if( isset( $param['api_key'] ) ){
      $this->_api_key = $param['api_key'];
    }
 
    if( isset( $param['use_ssl'] ) ){
      $this->_use_ssl = $param['use_ssl'];
    }
 
  }
 
  //觸發,單發,適用于驗證碼,訂單觸發提醒類
  public function send( $mobile , $message = '' ){
    $api_url = !$this->_use_ssl ? $this->_api_url['send'] : $this->_ssl_api_url['send'];
    $param = array(
      'mobile' => $mobile ,
      'message' => $message,
    );
    $res = $this->http_post( $api_url ,$param );
    return @json_decode( $res ,TRUE );
  }
 
  //批量發送,用于大批量發送
  public function send_batch( $mobile_list = array() , $message = array() , $time = '' ){
    $api_url = !$this->_use_ssl ? $this->_api_url['send_batch'] : $this->_ssl_api_url['send_batch'];
    $mobile_list = is_array( $mobile_list ) ? implode( ',' , $mobile_list ) : $mobile_list;
    $param = array(
      'mobile_list' => $mobile_list ,
      'message' => $message,
      'time'  => $time,
    );
    $res = $this->http_post( $api_url ,$param );
    return @json_decode( $res ,TRUE );
  }
 
  //獲取短信賬號余額
  public function get_deposit(){
    $api_url = !$this->_use_ssl ? $this->_api_url['status'] : $this->_ssl_api_url['status'];
    $res = $this->http_get( $api_url );
    return @json_decode( $res ,TRUE );
  }
 
  /**
   * @param string $type 接收類型,用于在服務器端接收上行和發送狀態,接收地址需要在luosimao后臺設置
   * @param array $param 傳入的參數,從推送的url中獲取,官方文檔:https://luosimao.com/docs/api/
   */
  public function recv( $type = 'status' , $param = array() ){
    if( $type == 'status' ){
      if( $param['batch_id'] && $param['mobile'] && $param['status'] ){ //狀態
        // do record
      }
    }elseif( $type == 'incoming' ){ //上行回復
      if( $param['mobile'] && $param['message'] ){
        // do record
      }
    }
  }
 
  /**
   * @param string $api_url 接口地址
   * @param array $param post參數
   * @param int $timeout 超時時間
   * @return bool
   */
  private function http_post( $api_url = '' , $param = array() , $timeout = 5 ){
 
    if( !$api_url ){
      die("error api_url");
    }
 
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_URL, $api_url );
 
    curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_0 );
    curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt( $ch, CURLOPT_HEADER, FALSE);
 
    if( parse_url( $api_url )['scheme'] == 'https' ){
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST , FALSE);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER , FALSE);
    }
 
    curl_setopt( $ch, CURLOPT_HTTPAUTH , CURLAUTH_BASIC);
    curl_setopt( $ch, CURLOPT_USERPWD , 'api:key-'.$this->_api_key );
    curl_setopt( $ch, CURLOPT_POST, TRUE);
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $param );
 
    $res  = curl_exec( $ch );
    $error = curl_error( $ch );
    curl_close( $ch );
    if( $error ){
      $this->_last_error[] = $error;
      return FALSE;
    }
    return $res;
  }
 
  /**
   * @param string $api_url 接口地址
   * @param string $timeout 超時時間
   * @return bool
   */
  private function http_get( $api_url = '' , $timeout = '' ){
 
    if( !$api_url ){
      die("error api_url");
    }
 
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_URL, $api_url );
 
    curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_0 );
    curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt( $ch, CURLOPT_HEADER, FALSE);
 
    if( parse_url( $api_url )['scheme'] == 'https' ){
      curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST , FALSE);
      curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER , FALSE);
    }
    curl_setopt( $ch, CURLOPT_HTTPAUTH , CURLAUTH_BASIC);
    curl_setopt( $ch, CURLOPT_USERPWD , 'api:key-'.$this->_api_key );
 
    $res  = curl_exec( $ch );
    $error = curl_error( $ch );
    curl_close( $ch );
    if( $error ){
      $this->_last_error[] = curl_error( $ch );
      return FALSE;
    }
    return $res;
  }
 
  public function last_error(){
    return $this->_last_error;
  }
}

2.短信發送示例    

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//send 單發接口
 
require 'sms.php';
$sms = new Sms( array('api_key' => '86f52f3ce0647dc24da53eafe29fadd4' , 'use_ssl' => FALSE ) );
$res = $sms->send_batch( array('13761428268') , '驗證碼:19272【服務器之家】');
if( $res ){
  if( isset( $res['error'] ) && $res['error'] == 0 ){
    echo 'success';
  }else{
    echo 'failed,code:'.$res['error'].',msg:'.$res['msg'];
  }
}else{
  var_dump( $sms->last_error() );
}
exit;


3.批量發送示例    

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
require 'sms.php';
$sms = new Sms( array('api_key' => '86f52f3ce0647dc24da53eafe29fadd4' , 'use_ssl' => FALSE ) );
 
 
//send 單發接口
$res = $sms->send_batch( array('13761428268') , '驗證碼:19272【服務器之家】');
if( $res ){
  if( isset( $res['error'] ) && $res['error'] == 0 ){
    echo 'success';
  }else{
    echo 'failed,code:'.$res['error'].',msg:'.$res['msg'];
  }
}else{
  var_dump( $sms->last_error() );
}
exit;

4.獲取余額示例    

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

以上就是本文的全部內容,希望對大家的學習有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 黄色a级片视频 | 最近日本电影hd免费观看 | 久色成人 | 中文字幕电影免费播放 | 美女一级毛片 | 4p嗯啊巨肉寝室调教男男视频 | 亚洲成人黄色片 | 国产羞羞视频在线免费观看 | 在线免费观看欧美 | 欧美综合在线观看视频 | 国产午夜免费福利 | 国产精品.com | 九一看片. | 美女久久 | 久久久久久久国产视频 | 国产二区三区在线播放 | 中文有码一区二区 | 亚洲人成网站免费播放 | 国产黄色网| 91精品免费观看 | 日本精品视频一区二区三区四区 | 成人免费毛片在线观看 | 久久久婷婷一区二区三区不卡 | 天天鲁在线视频免费观看 | 黄污免费网站 | 广州毛片 | 福利免费在线观看 | 国产精品久久久久久久午夜片 | 黄色试看视频 | 51色视频| 国产视频精品在线 | 成人在线视频免费观看 | 日韩区在线 | 久草视频在线资源 | 做羞羞视频| 在线播放免费播放av片 | 亚洲视频观看 | www.国产免费 | 国产资源在线视频 | 九九午夜视频 | 九色激情网|