Home Backend Development PHP Tutorial 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    ));  }}
Copy after login

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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Detailed explanation of the usage of return in C language Detailed explanation of the usage of return in C language Oct 07, 2023 am 10:58 AM

The usage of return in C language is: 1. For functions whose return value type is void, you can use the return statement to end the execution of the function early; 2. For functions whose return value type is not void, the function of the return statement is to end the execution of the function. The result is returned to the caller; 3. End the execution of the function early. Inside the function, we can use the return statement to end the execution of the function early, even if the function does not return a value.

What does function mean? What does function mean? Aug 04, 2023 am 10:33 AM

Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

What is the execution order of return and finally statements in Java? What is the execution order of return and finally statements in Java? Apr 25, 2023 pm 07:55 PM

Source code: publicclassReturnFinallyDemo{publicstaticvoidmain(String[]args){System.out.println(case1());}publicstaticintcase1(){intx;try{x=1;returnx;}finally{x=3;}}}#Output The output of the above code can simply conclude: return is executed before finally. Let's take a look at what happens at the bytecode level. The following intercepts part of the bytecode of the case1 method, and compares the source code to annotate the meaning of each instruction in

How to Fix Can't Connect to App Store Error on iPhone How to Fix Can't Connect to App Store Error on iPhone Jul 29, 2023 am 08:22 AM

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

What is the purpose of the 'enumerate()' function in Python? What is the purpose of the 'enumerate()' function in Python? Sep 01, 2023 am 11:29 AM

In this article, we will learn about enumerate() function and the purpose of “enumerate()” function in Python. What is the enumerate() function? Python's enumerate() function accepts a data collection as a parameter and returns an enumeration object. Enumeration objects are returned as key-value pairs. The key is the index corresponding to each item, and the value is the items. Syntax enumerate(iterable,start) Parameters iterable - The passed in data collection can be returned as an enumeration object, called iterablestart - As the name suggests, the starting index of the enumeration object is defined by start. if we ignore

See all articles