fgsl / rest
Fgsl RESTful 请求器
1.4.1
2024-08-20 13:56 UTC
Requires
- php: >=8.2
- ext-curl: *
- fgsl/http: ^1.4
- phpunit/phpunit: ^11.1
This package is auto-updated.
Last update: 2024-09-20 14:16:00 UTC
README
用于执行 HTTP 请求和测试 RESTful API 的组件
如何安装
composer require fgsl/rest
如何实例化
use Fgsl\Rest\Rest; $rest = new Rest();
可用方法
- doGet
/** * Method to make a HTTP GET request * @param $headers array * @param $url string * @param $expectedCode string | integer | array */ public function doGet($headers,$url,$expectedCode, $data = [],$verbose=false)
- doPost
/** * Method to make a HTTP POST request * @param $data array * @param $headers array * @param $url string * @param $expectedCode string | integer | array */ public function doPost($data,$headers,$url,$expectedCode, $verbose=false)
- doPut
/** * Method to make a HTTP PUT request * @param $data array * @param $headers array * @param $url string * @param $expectedCode string | integer | array */ public function doPut($data,$headers,$url,$expectedCode, $verbose=false) {
- doDelete
/** * Method to make a HTTP DELETE request * @param $headers array * @param $url string * @param $expectedCode string | integer | array * @param $data array * @param $verbose boolean */ public function doDelete($headers,$url,$expectedCode, $data = [],$verbose=false)
- doPatch
/** * Method to make a HTTP PATCH request * @param $data array * @param $headers array * @param $url string * @param $expectedCode string | integer | array */ public function doPatch($data,$headers,$url,$expectedCode, $verbose=false)
- doDelete
/** * Method to make a HTTP PATCH request * @param $data array * @param $headers array * @param $url string * @param $expectedCode string | integer | array */ public function doPatch($data,$headers,$url,$expectedCode, $verbose=false)
示例
以下是一些关于如何使用 Rest
类的示例。
我们使用了巴西国家观察站的页面进行 GET 方法。
我们使用了 Reqres 进行 POST、PATCH 和 DELETE 方法。
测试 HTTP GET 请求
public function testGet() { $rest = new Rest(); $this->assertTrue(is_object($rest)); @$response = $rest->doGet([],'http://www.horalegalbrasil.mct.on.br/SincronismoPublico.html',200); $this->assertStringContainsString('Sincronismo', $response); @$response = $rest->doGet([],'http://www.horalegalbrasil.mct.on.br/SincronismoPublico.html',500); $this->assertEquals(1,count($rest->requestErrors)); $this->assertEquals(2,$rest->requestCounter); }
测试 HTTP POST 请求
public function testPost() { $rest = new Rest(); $data = [ 'name' => 'morpheus', 'job' => 'leader' ]; @$response = $rest->doPost($data, [],'https://reqres.in/api/users',201); $this->assertStringContainsString('createdAt', $response); }
测试 HTTP PUT 请求
public function testPut() { $rest = new Rest(); $data = [ 'name' => 'morpheus', 'job' => 'general' ]; @$response = $rest->doPut($data, [],'https://reqres.in/api/users/2',201); $this->assertStringContainsString('updatedAt', $response); }
测试 HTTP PATCH 请求
public function testPatch() { $rest = new Rest(); $data = [ 'name' => 'morpheus', 'job' => 'zion resident' ]; @$response = $rest->doPatch($data, [], 'https://reqres.in/api/users/2', 200); $this->assertStringContainsString('updatedAt', $response); }
测试 HTTP DELETE 请求
public function testDelete() { $rest = new Rest(); @$response = $rest->doDelete([],'https://reqres.in/api/users/2',204); $this->assertEquals(0,count($rest->requestErrors)); } }
您可以允许多个 HTTP 状态码作为有效的响应。下面的代码片段显示了允许 200 和 201 状态码作为有效响应的请求。
@$response = $rest->doGet([],'http://www.horalegalbrasil.mct.on.br/SincronismoPublico.html',[200,201]); $this->assertStringContainsString('Sincronismo', $response);