redgoose / restapi
RestAPI接口
1.0.2
2020-08-18 15:32 UTC
Requires
- php: >=7.2.0
- redgoose/console: ^1.0
README
PHP上的RestAPI接口
这是一个PHP环境下帮助执行API请求的接口类。
由于此包使用cURL PHP模块,因此必须确保已经安装了curl模块。
# php7.4
sudo apt-get install php7.4-curl
安装
以下是通过composer安装包的方法。
composer require redgoose/restapi
如果不使用composer,可以从github上下载源代码,然后使用/src/RestAPI.php。
使用
使用composer
require 'vendor/autoload.php'; use redgoose\RestAPI; $restapi = new RestAPI();
不使用composer
下载源代码后直接连接的使用方法。
require 'src/RestAPI.php'; use redgoose\RestAPI; $restapi = new RestAPI();
创建实例
可以通过使用new关键字创建实例对象来使用。
use redgoose\RestAPI; // example $restapi = new RestAPI((object)[]);
选项
use redgoose\RestAPI; $restapi = new RestAPI((object)[ 'url' => 'https://api.address.com', 'outputType' => 'text', 'headers' => [], 'timeout' => 10, 'debug' => false, ]);
创建实例对象时使用的选项。
创建对象的示例代码中,对象类型的值如下。
方法
request
以函数形式访问时使用。
use redgoose\RestAPI; $response = RestAPI::request($method, $path, $data, $files, $options); // example $response = RestAPI::request('get', 'https://api.domain.com', null, null, (object)[]);
此方法使用以下参数值。
call
请求时使用的函数。
由于内部使用RestAPI::request()函数,因此使用方法与Methods/request部分类似。
use redgoose\RestAPI; $restapi = new RestAPI((object)[]); $response = $restapi->call($method, $path, $data, $files); // example $response = $restapi->call('get', 'https://api.domain.com', null, null);
此方法使用以下参数值。
update
可以使用它来更新实例的值。
use redgoose\RestAPI; $restapi = new RestAPI(); $restapi->update($options);
此方法使用以下参数值。
示例
可以在/tests中查看为开发而创建的痕迹。
文件上传
为开发创建了关于文件上传功能的示例源代码。
page/upload.php文件可以用来示例上传表单和请求附件的部分。
并且可以在api/upload.php文件中上传并接收输出结果。
由于curl不能向同一域名发送请求,因此启动了一个额外的测试API服务器。
这是通过script.sh脚本来启动不同的本地服务器端口,以便开发文件上传并进行测试。
API服务器上,当以$_FILES形式传递时,实际上可以上传文件,因此需要更多的处理来传递值。