首頁 後端開發 php教程 PHP API打包的一个实例,来自EtherPad

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

Jun 13, 2016 am 10:49 AM
function gt return this

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

<?phpclass 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

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1664
14
CakePHP 教程
1423
52
Laravel 教程
1321
25
PHP教程
1269
29
C# 教程
1249
24
華為GT3 Pro和GT4的差異是什麼? 華為GT3 Pro和GT4的差異是什麼? Dec 29, 2023 pm 02:27 PM

許多用戶在選擇智慧型手錶的時候都會選擇的華為的品牌,其中華為GT3pro和GT4都是非常熱門的選擇,不少用戶都很好奇華為GT3pro和GT4有什麼區別,下面就給大家介紹一下二者。華為GT3pro和GT4有什麼差別一、外觀GT4:46mm和41mm,材質是玻璃鏡板+不鏽鋼機身+高分纖維後殼。 GT3pro:46.6mm和42.9mm,材質是藍寶石玻璃鏡+鈦金屬機身/陶瓷機身+陶瓷後殼二、健康GT4:採用最新的華為Truseen5.5+演算法,結果會更加的精準。 GT3pro:多了ECG心電圖和血管及安

C語言return的用法詳解 C語言return的用法詳解 Oct 07, 2023 am 10:58 AM

C語言return的用法有:1、對於傳回值類型為void的函數,可以使用return語句來提前結束函數的執行;2、對於傳回值型別不為void的函數,return語句的作用是將函數的執行結果傳回給呼叫者;3、提前結束函數的執行,在函數內部,我們可以使用return語句來提前結束函數的執行,即使函數並沒有回傳值。

function是什麼意思 function是什麼意思 Aug 04, 2023 am 10:33 AM

function是函數的意思,是一段具有特定功能的可重複使用的程式碼區塊,是程式的基本組成單元之一,可以接受輸入參數,執行特定的操作,並傳回結果,其目的是封裝一段可重複使用的程式碼,提高程式碼的可重複使用性和可維護性。

修復:截圖工具在 Windows 11 中不起作用 修復:截圖工具在 Windows 11 中不起作用 Aug 24, 2023 am 09:48 AM

為什麼截圖工具在Windows11上不起作用了解問題的根本原因有助於找到正確的解決方案。以下是截圖工具可能無法正常工作的主要原因:對焦助手已開啟:這可以防止截圖工具開啟。應用程式損壞:如果截圖工具在啟動時崩潰,則可能已損壞。過時的圖形驅動程式:不相容的驅動程式可能會幹擾截圖工具。來自其他應用程式的干擾:其他正在運行的應用程式可能與截圖工具衝突。憑證已過期:升級過程中的錯誤可能會導致此issu簡單的解決方案這些適合大多數用戶,不需要任何特殊的技術知識。 1.更新視窗與Microsoft應用程式商店應用程

Java中return和finally語句的執行順序是怎樣的? Java中return和finally語句的執行順序是怎樣的? Apr 25, 2023 pm 07:55 PM

原始碼:publicclassReturnFinallyDemo{publicstaticvoidmain(String[]args){System.out.println(case1());}publicstaticintcase1(){intx;try{x=1;returnx;}finally{x=3;}}#輸出上述程式碼的輸出可以簡單地得出結論:return在finally之前執行,我們來看下字節碼層面上發生了什麼事情。下面截取case1方法的部分字節碼,並且對照源碼,將每個指令的含義註釋在

'enumerate()'函數在Python中的用途是什麼? 'enumerate()'函數在Python中的用途是什麼? Sep 01, 2023 am 11:29 AM

在本文中,我們將了解enumerate()函數以及Python中「enumerate()」函數的用途。什麼是enumerate()函數? Python的enumerate()函數接受資料集合作為參數並傳回一個枚舉物件。枚舉物件以鍵值對的形式傳回。 key是每個item對應的索引,value是items。語法enumerate(iterable,start)參數iterable-傳入的資料集合可以作為枚舉物件傳回,稱為iterablestart-顧名思義,枚舉物件的起始索引由start定義。如果我們忽

MySQL.proc表的作用與功能詳解 MySQL.proc表的作用與功能詳解 Mar 16, 2024 am 09:03 AM

MySQL.proc表的功能與功能詳解MySQL是一種流行的關係型資料庫管理系統,開發者在使用MySQL時常常會涉及到預存程序(StoredProcedure)的建立與管理。而MySQL.proc表則是一個非常重要的系統表,它儲存了資料庫中所有的預存程序的相關信息,包括預存程序的名稱、定義、參數等。在本文中,我們將詳細解釋MySQL.proc表的作用與功能

如何修復無法連線到iPhone上的App Store錯誤 如何修復無法連線到iPhone上的App Store錯誤 Jul 29, 2023 am 08:22 AM

第1部分:初始故障排除步驟檢查蘋果的系統狀態:在深入研究複雜的解決方案之前,讓我們先從基礎知識開始。問題可能不在於您的設備;蘋果的伺服器可能會關閉。造訪Apple的系統狀態頁面,查看AppStore是否正常運作。如果有問題,您所能做的就是等待Apple修復它。檢查您的網路連接:確保您擁有穩定的網路連接,因為「無法連接到AppStore」問題有時可歸因於連接不良。嘗試在Wi-Fi和行動數據之間切換或重置網路設定(「常規」>「重置」>「重置網路設定」>設定)。更新您的iOS版本:

See all articles