`
buluzhai
  • 浏览: 108322 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

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

    博客分类:
  • php
阅读更多
<?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
    ));
  }
}
分享到:
评论

相关推荐

    etherpad-lite-php-client:用于 Etherpad Lite API 的 PHP 客户端库

    Etherpad-Lite API PHP 客户端它是什么? Etherpad-Lite API 的 PHP 客户端。 目前不完整且不稳定。先决条件您必须使用 Etherpad-Lite &gt;= 1.2.1。安装作曲家在根目录中,运行: $ curl -s ...

    Etherpad在线协同文档

    Etherpad协同文档包,windows版,bin启动installOnWindows.bat,主包启动start.bat,可以进行局域网在线协同文件编辑。

    Etherpad安装手册

    Etherpad安装手册

    etherpad-latex:一组用于将 etherpad-lite 与 LaTeX 结合的 php 和 javascript 文件

    etherpad乳胶一组用于将 etherpad-lite 与 LaTeX 结合的 php 和 javascript 文件。设置安装并从 APIKEY.txt 中获取 api 密钥。 在服务器上安装 TeX 发行版(例如 )。服务器端配置应在后端文件夹中编辑两个文件: ...

    Etherpad真正实时协作文档编辑

    Etherpad 是一个线上共制平台,三、四个人可以坐在自己电脑前,同时对一份文件修改,也同时看到其他人的修改,不必锁文件或什么的。 Etherpad 号称是「Really Real-time Collaboration」

    Etherpad Lite

    Etherpad 用于多人环境下富文本协作的软件,十分适用于限定范围内的知识共创。 新功能包括: 相比原始版本,显著降低硬件资源消耗。 支持 Win, OSX 和 Linux 三种系统下的部署。 界面得到了美化。 无限制的颜色选择...

    etherpad-lite

    还有一个功能齐全的插件框架,可让您轻松添加自己的功能。 默认情况下,您的Etherpad相当稀疏,并且因为Etherpad从Wordpress插件中汲取了很多灵感,所以它们的安装和更新确实非常容易。 一旦安装了Etherpad,您应该...

    EtherpadLiteDotNet:用 C# 编写的用于 .Net 的 Etherpad Lite API 的实现

    用于 Etherpad Lite API 的 .Net 库 它实现了 Etherpad Lite API,API 的。 它在结构上尽可能地匹配 API。 它将返回的 JSON 解析为强类型对象,以允许使用智能感知并减少魔术字符串的使用。 该库是用 C# 编写的,...

    实时在线文档协作工具Etherpad源码.003

    实时在线文档协作工具Etherpad源码 下载001、002、003文件以后,用7-zip解压。

    etherpad-new:我最近尝试推出自定义 etherpad

    还有一个可以帮助您将 Pads 嵌入您的网站。 还有一个功能齐全的插件框架,允许您轻松添加自己的功能。 默认情况下,您的 Etherpad 相当稀疏,因为 Etherpad 从 Wordpress 插件中汲取了很多灵感,因此非常易于安装和...

    ep_kaput:一个有意破坏Etherpad的插件

    该插件被设计为恶意软件,如果已安装,则其目的是使您的Etherpad实例崩溃并使其不可用。 安装 npm install ep_kaput 或使用Etherpad /admin界面。 测验 前端 访问来运行前端测试。 后端 键入cd src && npm run ...

    docker-etherpad:高度可配置的Etherpad Docker容器

    基本配置选项: ETHERPAD_TITLE (默认: Etherpad ) 此Etherpad实例的标题。 ETHERPAD_FAVICON (默认值: favicon.ico ) 此实例要使用的Favicon。 ETHERPAD_IP (默认: 0.0.0.0 ) 要监听的IP地址。 ETHERPAD_...

    etherpad-lite-mongoDB

    还有一个可以帮助您将 Pads 嵌入您的网站。 还有一个功能齐全的插件框架,允许您轻松添加自己的功能。 默认情况下,您的 Etherpad 相当稀疏,因为 Etherpad 从 Wordpress 插件中汲取了很多灵感,因此非常易于安装...

    etherpad-docker:用于Etherpad Lite的Dockerfile

    etherpad-泊坞窗 这或多或少是当前版本(截至编写此文件时),其中包括大量有用的插件。 要从Docker索引下载映像,请运行: docker pull toeirei/etherpad-docker 环境变量: ETHERPAD_TITLE =您的记事本标题 ...

    etherpad-lite-control:用于 Etherpad-Lite 管理的 htmljavascriptphp 接口

    在可能的情况下使用 Etherpad-Lite 的 API,并辅以直接数据库调用以在需要时扩展功能 特征 列出作者、垫组、垫和会话* 添加和删​​除作者、垫组、垫和会话* 显示作者、pad 和会话信息* 查看板内容 修改作者地图...

    etherpad-plugins:Etherpad Lite 插件

    Etherpad-Lite 插件Linkify:添加内部链接(ep_linkify) 标题:标题支持(ep_headings)安装 cd [your etherpad installation]; npm install [path-to-etherpad-plugins]/[plugin-name]其中plugin-name是 ep_...

    etherpad-proxy:Etherpad SocketIO消息的代理的非常基本的示例

    这是一个在端口9000上运行的反向代理,它将基于padId(在查询中(当前为Etherpad核心的一个分支))基于padId的填充路由到后端池(当前为硬编码(在app.js中))。 要添加或删除后端,请修改app.js-将来将由API驱动...

    etherpad-alfresco:Alfresco到Etherpad的集成

    这将在HTML文档的文档库中创建一个新的Etherpad中的Edit in Etherpad操作。 这使多个用户可以实时协作并将这些更改保存回Alfresco。 经过Enterprise 4.2。*,5.0。*和Community 4.2。*,5.0。*的测试 编译中 你会...

    ep_draw:Etherpad的Etherpad插件

    在Etherpad中绘画 您必须正在运行实例 。 在您的settings.json ,添加: 更改主机 设置可从用户浏览器访问的主机。 " ep_draw " : { " host " : " your.etherdrawhost.com " } # Example " ep_draw " :{ " ...

    etherpad-lite-heroku

    Etherpad-Lite Heroku 在Heroku上部署Etherpad-Lite。 可以在Heroku配置变量(ENV)中指定整个Etherpad-Lite配置,包括插件。跑步克隆由于该项目将Etherpad作为子模块,因此您需要使用--recursive标志将其克隆: git...

Global site tag (gtag.js) - Google Analytics