iam-decoder / rest_server
Requires
- iam-decoder/rest_server: dev-master
This package is not auto-updated.
Last update: 2022-01-08 03:49:52 UTC
README
→ 作者:Travis Neal
→ 许可证:GNU GPL v3
→ 网站:https://github.com/iam-decoder
关于
这是一个即插即用的REST控制器,支持CORS,旨在适用于大多数使用PHP作为服务器端脚本语言的实现的服务器。
这是一个即插即用的库,旨在适用于大多数使用PHP作为服务器端脚本语言的Web应用程序。然而,为了获得最佳效果,建议使用基于Apache的服务器。如果您计划在非Apache服务器上使用此库,请用您的服务器的版本变量替换从_init
方法分叉的_request
属性返回的值(或者使用您自己的聪明方法来获取所需的结果)。
用法
- 将文件
/src/REST_Server.php
复制到您的应用程序中 - 在调用REST_Server类之前包含该文件(使用
require_once
) - 扩展一个您希望REST_Server执行的类/控制器
→ 示例
/* * For servers using full classes as endpoints */ require_once('REST_Server.php'); class api_endpoint extends REST_Server { public function __construct(){ parent::__constructor(); $this->callMethod('methodPrefix_'); } public function methodPrefix_get(){ //this method is only called in a get request $this->response("You've reached the methodPrefix_get method"); } public function methodPrefix_post(){ //this method is only called in a post request $this->response("You've reached the methodPrefix_post method"); } }
或者
/* * For servers using class methods as endpoints */ require_once('REST_Server.php'); class api_endpoint extends REST_Server { public function __construct(){ parent::__constructor(); } public function calledMethod(){ //reached by this method being called from somewhere $this->callMethod('methodPrefix_'); } public function methodPrefix_get(){ //this method is only called in a get request $this->response("You've reached the methodPrefix_get method"); } public function methodPrefix_post(){ //this method is only called in a post request $this->response("You've reached the methodPrefix_post method"); } }
方法
可以从扩展的类中调用的可用方法
__construct($config = NULL
)
对象构造函数接受无参数或配置变量的数组。如果没有传递任何参数,则将使用默认选项(默认选项可以通过覆盖以两个下划线(__
)为前缀的类中的属性值来更改。如果您想通过构造函数发送配置设置,可以传递一个关联数组,其键是可更改属性的名称,值是用于而不是默认值的新值。可更改的属性是
属性名称 | 默认值 | 描述 |
---|---|---|
default_response_type | "json" |
如果请求使用非认可的数据格式,或者格式是编码的字符串形式,将使用默认响应类型 |
charset | "utf-8" |
定义字符集头值 |
default_xml_wrapper | "ApplicationResponse" |
当以XML格式响应时,数据需要包裹在根节点中,如果没有提供,将使用默认的包装器。 |
ajax_only | true |
如果这是true,则只允许带有x-requested-with头和值为xmlhttprequest的请求 |
allowed_http_origins | array('*') |
指定请求可以来自哪些http源 |
allowed_http_methods | array('get', 'delete', 'post', 'put', 'options', 'head') |
指定允许哪些http请求方法 |
allowed_http_headers | array('origin', 'x-requested-with', 'content-type') |
指定请求中允许哪些头 |
allow_http_credentials | true |
指定服务器接受请求中的cookie |
→ 示例
public function __construct(){ parent::__constructor( array( "ajax_only" => false , "default_response_type" => "form" ) ); }
requestData($method, $element = NULL
)
根据提供的请求方法类型检索数据。如果指定了元素,它将在解析的数据中查找该元素,如果不存在,则返回false
。如果没有指定元素,则将返回与该方法相关联的所有数据的数组。
→ 示例
public function someMethod(){ $this->requestData('get'); //returns all get variables as an array $this->requestData('post', 'name'); /* if the 'name' element exists in the post * data, the previous line will return its * value, otherwise it will return a * boolean false. */ }
requestInfo()
返回有关请求的信息,例如是否使用AJAX发起请求,使用的请求方法(如get、post等),请求是否访问了安全套接字层(SSL)等。
→ 示例
public function someMethod(){ $this->requestInfo(); }
callMethod($method_prefix = NULL
, $parameters = NULL
)
调用当前类继承中以$method_prefix
开头并以当前请求方法结尾的方法。如果调用中包含参数,它将它们附加到方法调用中。例如,如果您想将服务器上计算出的值传递给方法,可以将它添加到语句中,如下所示
→ 示例
public function foo(){ $this->callMethod('bar_', 'baz'); } public function bar_get($singleParameter){ echo $singleParameter; //outputs: baz }
如果您想传递多个参数,可以将这些参数的索引数组传递给callMethod方法
→ 示例
public function foo(){ $this->callMethod('bar_', array('baz', 'qux')); } public function bar_get($firstParameter, $secondParameter){ echo $firstParameter." -> ".$secondParameter; //outputs: baz -> qux }
setResponseData($new_data = NULL
)
设置用于生成响应的数据。如果您希望将数据与最终的response()
语句分开,请使用此方法。
→ 示例
public function someMethod(){ $data = array("name" => "Travis Neal"); $this->setResponseData($data); $this->response(); }
setData($new_data = NULL
)
setResponseData
方法的别名。
response($data = NULL
, $http_code = NULL
, $return = FALSE
)
输出格式化的数据,以遵循请求的生成方式。例如,如果请求是用XML
内容发起的,则返回XML
内容。如果无法确定正确的格式,则使用__default_response_type
属性的值。
接受0到3个参数
- 第一个参数应该是
NULL
或数据数组。如果传递了NULL
,则将检查是否以前使用setResponseData
方法设置了数据,如果两者都是NULL
,则不会为请求输出任何内容,但状态码将被设置。 - 第二个参数可以是
NULL
或对应于请求的HTTP状态码的数字值(参见状态码定义)。如果设置为NULL
,则将其转换为状态200 OK
。 - 第三个参数应该是一个布尔值,可以是
TRUE
或FALSE
。如果设置为TRUE
,则将返回格式化后的数据(如果有),并将 HTTP 状态头设置,但请求会继续。如果设置为FALSE
,则请求将结束,并将格式化后的数据输出供读取,并设置状态码。
→ 示例
public function someMethod(){ $data = array("name" => "Travis Neal"); $this->response($data, 100); //outputs the formatted data and terminates the request with an HTTP status of '100 Continue' }
更新日志
v1.0.1
- 常规
- 将许可证从 GNU LGPL v2.1 更改为 GNU GPL v3
- 向所有文件添加了许可证头
- 可在 composer 中使用
- 错误修复
- 修复了 CSV 响应
v1.0.0
初始上传