设为首页 收藏本站
查看: 718|回复: 0

[经验分享] PHP API封装的一个实例,来自EtherPad

[复制链接]

尚未签到

发表于 2017-4-10 10:07:33 | 显示全部楼层 |阅读模式
<?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
));
}
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-362764-1-1.html 上篇帖子: php socket模拟POST GET请求 fsockopen版 下篇帖子: PHP实现的某SNS手机游戏的简单外挂
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表