PHP implements RESTful style API example (i)

Source: Internet
Author: User

Recently read some information about the restful, self-made also wrote a restful instance, the following is the source

Catalogue Details:

restful/    request.php Data operation class    response.php output class    index.php Portal file    . htaccess rewrite URL

Request.php: Contains a request class, which is a data manipulation class. After receiving the data for the URL, depending on how the URL is requested (get| Post| Put| patch| DELETE) The corresponding additions and deletions to the data, and return the results of the operation:

<?PHP/** * Data operation class*/classrequest{//allowed request Mode    Private Static $method _type=Array(' Get ', ' post ', ' Put ', ' Patch ', ' delete '); //test Data    Private Static $test _class=Array(        1 =Array(' name ' = ' Tofoban ', ' count ' = +), 2 =Array(' name ' = ' + ' IELTS class ', ' count ' = 20),    );  Public Static functiongetrequest () {//Request Method        $method=Strtolower($_server[' Request_method ']); if(In_array($method, Self::$method _type)) {            //methods that correspond to calling request methods            $data _name=$method. ' Data '; returnSelf::$data _name($_request); }        return false; }    //Get Get information    Private Static functionGetData ($request _data)    {        $class _id= (int)$request _data[' Class ']; //get/class/id: Get information about a given class        if($class _id> 0) {            returnSelf::$test _class[$class _id]; } Else{//Get/class: List all Classes            returnSelf::$test _class; }    }    //Post/class: Create a new class    Private Static functionPostData ($request _data)    {        if(!Empty($request _data[' Name '])) {            $data[' name '] =$request _data[' Name ']; $data[' count '] = (int)$request _data[' Count ']; Self::$test _class[] =$data; returnSelf::$test _class;//returns the newly generated resource object}Else {            return false; }    }    //put/class/id: Update information for a given class (all information)    Private Static functionPutData ($request _data)    {        $class _id= (int)$request _data[' Class ']; if($class _id= = 0) {            return false; }        $data=Array(); if(!Empty($request _data[' Name ']) &&isset($request _data[' Count '])) {            $data[' name '] =$request _data[' Name ']; $data[' count '] = (int)$request _data[' Count ']; Self::$test _class[$class _id] =$data; returnSelf::$test _class; } Else {            return false; }    }    //patch/class/id: Update information for a given class (some information)    Private Static functionPatchdata ($request _data)    {        $class _id= (int)$request _data[' Class ']; if($class _id= = 0) {            return false; }        if(!Empty($request _data[' Name ']) { self::$test _class[$class _id[' name '] =$request _data[' Name ']; }        if(isset($request _data[' Count ']) { self::$test _class[$class _id[' count '] = (int)$request _data[' Count ']; }        returnSelf::$test _class; }    //delete/class/id: Delete a class    Private Static functionDeleteData ($request _data)    {        $class _id= (int)$request _data[' Class ']; if($class _id= = 0) {            return false; }        unset(Self::$test _class[$class _id]); returnSelf::$test _class; }}

Next PHP implementation of RESTful style API example (ii)

PHP implements RESTful style API example (i)

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.