PHP implements RESTful style API example (i)

Source: Internet
Author: User

(For a restful tutorial on Yii2, see: YII2 Framework RESTful API tutorial)

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 */class request{//allowed request mode private static $method _type = Array (' Get ', ' post ', ' Put ', ' Patch ', ' d    Elete '); Test data private static $test _class = Array (1 = = Array (' name ' = ' Tofoban ', ' count ' = +), 2 = a    Rray (' name ' = ' + ' IELTS class ', ' count ' = 20),);        public static function Getrequest () {//Request mode $method = Strtolower ($_server[' Request_method ')); if (In_array ($method, Self:: $method _type)) {//Invoke the method corresponding to the request $data _name = $method.            ' Data ';        Return self:: $data _name ($_request);    } return false;        }//get gets the information private static function GetData ($request _data) {$class _id = (int) $request _data[' class ');        GET/CLASS/ID: Get information on a given class if ($class _id > 0) {return self:: $test _class[$class _id];        } else {//get/class: Lists all classes return self:: $test _class; }}//post/class: Create a new class private static function PostData ($reQuest_data) {if (!empty ($request _data[' name ')) {$data [' name '] = $request _data[' name '];            $data [' count '] = (int) $request _data[' count '];            Self:: $test _class[] = $data;        Return self:: $test _class;//Returns the newly generated resource object} else {return false; }}//PUT/CLASS/ID: Update information for a given class (all information) private static function PutData ($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[' na            Me '];            $data [' count '] = (int) $request _data[' count '];            Self:: $test _class[$class _id] = $data;        Return self:: $test _class;        } else {return false; }}//PATCH/CLASS/ID: Update information for a given class (some information) private static function Patchdata ($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 '];    } return Self:: $test _class; }//delete/class/id: Delete a class private static function DeleteData ($request _data) {$class _id = (int) $request _d        Ata[' class '];        if ($class _id = = 0) {return false;        } unset (self:: $test _class[$class _id]);    Return self:: $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.