Table of Contents
php SMS interface code, php SMS interface
Home Backend Development PHP Tutorial php SMS interface code, php SMS interface_PHP tutorial

php SMS interface code, php SMS interface_PHP tutorial

Jul 12, 2016 am 08:51 AM
php interface Short message

php SMS interface code, php SMS interface

The examples in this article share several commonly used php SMS interface codes for your reference. The specific content is as follows

1. SMS calling 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.bkjia.com/v1/send.json',

    'send_batch' => 'http://www.bkjia.com/v1/send_batch.json',

    'status'   => 'http://www.bkjia.com/v1/status.json',

  );

  

  private $_api_url = array(

    'send'    => 'http://www.bkjia.com/v1/send.json',

    'send_batch' => 'http://www.bkjia.com/send_batch.json',

    'status'   => 'http://www.bkjia.com/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;

  }

}

Copy after login

2. SMS sending example

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;

Copy after login


3. Batch sending example

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;

Copy after login

4. Example of getting balance

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;

Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone’s study.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1127909.htmlTechArticlephp SMS interface code, php SMS interface This article shares several commonly used php SMS interface codes for everyone. For your reference, the specific content is as follows 1. SMS call class php/** * User...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

See all articles