# rest4tp **Repository Path**: duanchi/rest4tp ## Basic Information - **Project Name**: rest4tp - **Description**: A restful extension for ThinkPHP - **Primary Language**: PHP - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 1 - **Created**: 2015-09-28 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # A Package of Extensions for ThinkPHP 3.X ---- ## 0. Introduction > This package is a soure of [Arua Studio(07Studio) PHP Library](http://github.com/duanchi/public-library). ## 1. How to ### 1.1 Installation > Cloning use `git clone https://git.oschina.net/duanchi/rest4tp.git` or download and uncompress it into `/Path/Of/ThinkPHP/Project/Library/Org/`. ### 1.2 Configuraiton > Put each config file in your config file path. ### 1.3 Coding! > Code `import('Org.AuraStudio.Util.Constant');` before you use the class below! ## 2. Classes ### 2.1 Class UUID > Make an uuid. #### 2.1.1 \Org\AuraStudio\Util\UUID::make ([$\_type = EX_CORE_UUID_TYPE_DEFAULT]) > Make an uuid use type from a enum. > - EX_CORE_UUID_TYPE_DEFAULT > - EX_CORE_UUID_TYPE_DEFAULT > - EX_CORE_UUID_TYPE_TIME > - EX_CORE_UUID_TYPE_DCE > - EX_CORE_UUID_TYPE_NAME > - EX_CORE_UUID_TYPE_RANDOM > - EX_CORE_UUID_TYPE_NULL > - EX_CORE_UUID_TYPE_INVALID code: ``` \Org\AuraStudio\Util\UUID::make(EX_CORE_UUID_TYPE_DEFAULT); ``` ### 2.2 Class Net\Restful\Server > Handle a Restful Api service with a Http server. #### 2.2.1 \Org\AuraStudio\Net\Restful\Server::__construct (\Org\AuraStudio\Net\Restful\Server\Handle $_handle, array $_config) `Config` ``` return [ 'request' => [ 'properties' => [//Restful Header Properties to set(must set if appear in here) 'user_agent' => [// Header (key): (default-value) set, 'key' => 'User-Agent', 'value' => 'Restful Client 0.1.0, Common defined', ], 'access_token' => 'Access-Token',// Header (key): (*) set, 'client_id' => [ 'key' => 'Client-Id',// Header (key): (*) set, ] ], 'resource' => [ 'placeholder' => '{{RESOURCE_PLACEHOLDER}}'//which to replace the resource, place in the end of request-path if not set, ], 'content_type' => [ 'json' => [//json output style. 'configure' => 192 ] ] ] ]; ``` `Example` ``` import('Org.AuraStudio.Util.Constant'); $__conf = C('RESTFUL'); $__handle = new TestServer(); $__server = new \Org\AuraStudio\Net\Restful\Server($__handle, $__conf); $__server->handle(); ``` `Class TestServer` ``` class TestServer extends \Org\AuraStudio\Net\Restful\Server\Handle { public function GET($_service, $_resource, $_parameters, $_properties) { $this->get_response() ->set('status', 200) ->set('headers', ['Dump: test']) ->set('properties', ['service'=>'test.test']) ->set('content_type', EX_MIMETYPE_MSGPACK) ->set('data', $_SERVER) ; return TRUE; } } ``` #### 2.2.2 \Org\AuraStudio\Net\Restful\Server\Handle::\[method\]($_service, $_resource, $_parameters, $_properties) > - \[method\] can be GET DELETE HEAD TRACE OPTIONS POST PUT PATCH UPDATE > - properties to set > - `status` EX_NET_HTTP_CODE_XXX > - `headers` http_raw_headers array > - `properties` properties key,value array of config file > - `content_type` EX_MIMETYPE_XXX > - `data` return data content ## 2.3 Class Net\Restful\Client > Handle a or a couple of request of Restful. #### 2.3.1 \Org\AuraStudio\Net\Restful\Client\Request::__construct (string $_method = EX_NET_RESTFUL_METHOD_GET, string $_service = '[SERVICE NOT SET]', $_resource = '', $_parameter = '', array $_properties = [], array $_request_headers = [], string $_request_body = '', array $_configurations = []) > How to Use? See below. #### 2.3.2 \Org\AuraStudio\Net\Restful\Client::__construct (array $_config) > configuration is as the same of Restful Server. `Example` ``` import('Org.AuraStudio.Util.Constant'); $__conf = C('RESTFUL'); $__client_handle = new \Org\AuraStudio\Net\Restful\Client($__conf); $__request = new \Org\AuraStudio\Net\Restful\Client\Request( EX_NET_HTTP_METHOD_GET, 'http://rest4tp.devel/server', 'the-request-resource', '{{RESOURCE_PLACEHOLDER}}&haha=foo', [], '', [] ); $__request ->set('Access-token', '8LO2rRDSmwIdbafeicpqAgJC47LXBJ2x5CaOJNpqw32ba6rxwnDNWccQep8HUycW') ->set('Client-id', '51b08861-84cd-3ca1-a507-5d02907d1d80'); $__client_handle->add_request($__request); $this->show($__client_handle->execute($__request), 'utf-8'); ```