php api封装的一个实例,来自etherpad
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 )); }}
