An example of PHP API packaging, from Etherpad

Source: Internet
Author: User
An example of PHP API encapsulation from Etherpad
 ApiKey = $apiKey;    if (Isset ($BASEURL)) {$this->baseurl = $BASEURL; } if (!filter_var ($this->baseurl, Filter_validate_url)) {throw new InvalidArgumentException ("[{$this->baseur    L}] 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 has 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 respon    SE 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 ($resul      T->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 is always being public pads, 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-to-map your application group IDs to Etherpad Lite Group IDs public Function Creategroupi Fnotexistsfor ($groupMapper) {return $this->call ("Creategroupifnotexistsfor", Array ("groupmapper" + = $groupM  Apper)); }//Deletes a group public function DeleteGroup ($groupID) {return $this->call ("DeleteGroup", Array ("GroupI  D "= $groupID));      }//Returns all pads of the 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->cal   L ("Creategrouppad", Array ("GroupID" = $groupID, "Padname" and "= $padName", "text" = $text));   }//AUTHORS//Theses AUTHORS is 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-to-map your application author IDs to Etherpad Lite author IDs public Function Createauth Orifnotexistsfor ($authorMapper, $name) {return $this->call ("Createauthorifnotexistsfor", Array ("AuthorMapper"  = = $authorMapper, "name" = + $name)); }//SESSIONS//SESSIONS can be created between a group and a author. This allows//a author to access more than one group.  The SessionID would 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 ("Crea   Tesession ", 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 ("getsess  Ioninfo ", Array (" SessionID "= $sessionID)); }//Returns all sessions of a group public function Listsessionsofgroup ($groupID) {return $this->call ("Listsessi  Onsofgroup ", Array (" GroupID "= $groupID)); }//Returns all sessions of a author public Function Listsessionsofauthor ($authorID) {return $this->call ("ListS  Essionsofauthor ", Array (" Authorid "= $authorID)); }//pad content//pad content can be updated and retrieved through the API//returns the text of a PAD//should t    Ake 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 ("p  AdID "= $padID," text "= $text)); }//PAD//Group pads is 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 ("G  Etrevisionscount ", 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 $t  His->call ("Setpublicstatus", Array ("Padid" and "$padID," "publicstatus" and "= $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 ("Setpasswor  D ", Array (" padid "= $padID," password "and" = $password) ");  }//returns TRUE or false public function ispasswordprotected ($padID) {return $this->call ("ispasswordprotected",  Array ("padid" = $padID)); }}
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.