首页 > 后端开发 > php教程 > PHP API打包的一个实例,来自EtherPad

PHP API打包的一个实例,来自EtherPad

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
发布: 2016-06-13 13:11:49
原创
1299 人浏览过

PHP API封装的一个实例,来自EtherPad

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

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

<?php

class EtherpadLiteClient {

 

  const API_VERSION             = 1;

 

  const CODE_OK                 = 0;

  const CODE_INVALID_PARAMETERS = 1;

  const CODE_INTERNAL_ERROR     = 2;

  const CODE_INVALID_FUNCTION   = 3;

  const CODE_INVALID_API_KEY    = 4;

 

  protected $apiKey = "";

  protected $baseUrl = "http://localhost:9001/api";

   

  public function __construct($apiKey, $baseUrl = null){

    $this->apiKey  = $apiKey;

    if (isset($baseUrl)){

      $this->baseUrl = $baseUrl;

    }

    if (!filter_var($this->baseUrl, FILTER_VALIDATE_URL)){

      throw new InvalidArgumentException("[{$this->baseUrl}] is not a valid URL");

    }

  }

 

  protected function call($function, array $arguments = array()){

    $query = array_merge(

      array('apikey' => $this->apiKey),

      $arguments

    );

    $url = $this->baseUrl."/".self::API_VERSION."/".$function."?".http_build_query($query);

    // not all PHP installs have access to curl

    if (function_exists('curl_init')){

      $c = curl_init($url);

      curl_setopt($c, CURLOPT_RETURNTRANSFER, true);

      curl_setopt($c, CURLOPT_TIMEOUT, 20);

      $result = curl_exec($c);

      curl_close($c);

    } else {

      $result = file_get_contents($url);

    }

     

    if($result == ""){

      throw new UnexpectedValueException("Empty or No Response from the server");

    }

     

    $result = json_decode($result);

    if ($result === null){

      throw new UnexpectedValueException("JSON response could not be decoded");

    }

    return $this->handleResult($result);

  }

 

  protected function handleResult($result){

    if (!isset($result->code)){

      throw new RuntimeException("API response has no code");

    }

    if (!isset($result->message)){

      throw new RuntimeException("API response has no message");

    }

    if (!isset($result->data)){

      $result->data = null;

    }

 

    switch ($result->code){

      case self::CODE_OK:

        return $result->data;

      case self::CODE_INVALID_PARAMETERS:

      case self::CODE_INVALID_API_KEY:

        throw new InvalidArgumentException($result->message);

      case self::CODE_INTERNAL_ERROR:

        throw new RuntimeException($result->message);

      case self::CODE_INVALID_FUNCTION:

        throw new BadFunctionCallException($result->message);

      default:

        throw new RuntimeException("An unexpected error occurred whilst handling the response");

    }

  }

 

  // GROUPS

  // Pads can belong to a group. There will always be public pads that doesnt belong to a group (or we give this group the id 0)

   

  // creates a new group

  public function createGroup(){

    return $this->call("createGroup");

  }

 

  // this functions helps you to map your application group ids to etherpad lite group ids

  public function createGroupIfNotExistsFor($groupMapper){

    return $this->call("createGroupIfNotExistsFor", array(

      "groupMapper" => $groupMapper

    ));

  }

 

  // deletes a group

  public function deleteGroup($groupID){

    return $this->call("deleteGroup", array(

      "groupID" => $groupID

    ));

  }

 

  // returns all pads of this group

  public function listPads($groupID){

    return $this->call("listPads", array(

      "groupID" => $groupID

    ));

  }

 

  // creates a new pad in this group

  public function createGroupPad($groupID, $padName, $text){

    return $this->call("createGroupPad", array(

      "groupID" => $groupID,

      "padName" => $padName,

      "text"    => $text

    ));

  }

 

  // AUTHORS

  // Theses authors are bind to the attributes the users choose (color and name).

 

  // creates a new author

  public function createAuthor($name){

    return $this->call("createAuthor", array(

      "name" => $name

    ));

  }

 

  // this functions helps you to map your application author ids to etherpad lite author ids

  public function createAuthorIfNotExistsFor($authorMapper, $name){

    return $this->call("createAuthorIfNotExistsFor", array(

      "authorMapper" => $authorMapper,

      "name"         => $name

    ));

  }

 

  // SESSIONS

  // Sessions can be created between a group and a author. This allows

  // an author to access more than one group. The sessionID will be set as

  // a cookie to the client and is valid until a certian date.

 

  // creates a new session

  public function createSession($groupID, $authorID, $validUntil){

    return $this->call("createSession", array(

      "groupID"    => $groupID,

      "authorID"   => $authorID,

      "validUntil" => $validUntil

    ));

  }

 

  // deletes a session

  public function deleteSession($sessionID){

    return $this->call("deleteSession", array(

      "sessionID" => $sessionID

    ));

  }

 

  // returns informations about a session

  public function getSessionInfo($sessionID){

    return $this->call("getSessionInfo", array(

      "sessionID" => $sessionID

    ));

  }

 

  // returns all sessions of a group

  public function listSessionsOfGroup($groupID){

    return $this->call("listSessionsOfGroup", array(

      "groupID" => $groupID

    ));

  }

 

  // returns all sessions of an author

  public function listSessionsOfAuthor($authorID){

    return $this->call("listSessionsOfAuthor", array(

      "authorID" => $authorID

    ));

  }

 

  // PAD CONTENT

  // Pad content can be updated and retrieved through the API

 

  // returns the text of a pad

  // should take optional $rev

  public function getText($padID){

    return $this->call("getText", array(

      "padID" => $padID

    ));

  }

 

  // sets the text of a pad

  public function setText($padID, $text){

    return $this->call("setText", array(

      "padID" => $padID,

      "text"  => $text

    ));

  }

 

  // PAD

  // Group pads are normal pads, but with the name schema

  // GROUPID$PADNAME. A security manager controls access of them and its

  // forbidden for normal pads to include a $ in the name.

 

  // creates a new pad

  public function createPad($padID, $text){

    return $this->call("createPad", array(

      "padID" => $padID,

      "text"  => $text

    ));

  }

 

  // returns the number of revisions of this pad

  public function getRevisionsCount($padID){

    return $this->call("getRevisionsCount", array(

      "padID" => $padID

    ));

  }

 

  // deletes a pad

  public function deletePad($padID){

    return $this->call("deletePad", array(

      "padID" => $padID

    ));

  }

 

  // returns the read only link of a pad

  public function getReadOnlyID($padID){

    return $this->call("getReadOnlyID", array(

      "padID" => $padID

    ));

  }

 

  // sets a boolean for the public status of a pad

  public function setPublicStatus($padID, $publicStatus){

    return $this->call("setPublicStatus", array(

      "padID"        => $padID,

      "publicStatus" => $publicStatus

    ));

  }

 

  // return true of false

  public function getPublicStatus($padID){

    return $this->call("getPublicStatus", array(

      "padID" => $padID

    ));

  }

 

  // returns ok or a error message

  public function setPassword($padID, $password){

    return $this->call("setPassword", array(

      "padID"    => $padID,

      "password" => $password

    ));

  }

 

  // returns true or false

  public function isPasswordProtected($padID){

    return $this->call("isPasswordProtected", array(

      "padID" => $padID

    ));

  }

}

登录后复制

相关标签:
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
function mysqli() 错误
来自于 1970-01-01 08:00:00
0
0
0
javascript - JS中的Function()函数对象
来自于 1970-01-01 08:00:00
0
0
0
javascript - !function()的问题
来自于 1970-01-01 08:00:00
0
0
0
javascript - 关于jquery中$(function(){})的使用。
来自于 1970-01-01 08:00:00
0
0
0
angular.js - Angularjs如何建立 公共function
来自于 1970-01-01 08:00:00
0
0
0
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板