beycanpress / http
一个简单且实用的HTTP请求、响应和客户端库。
0.1.4
2023-09-29 03:37 UTC
Requires
- php: >=7.4
README
BeycanPress HTTP包是一个小型实用包,专为简单项目设计。使用此包,您可以创建HTTP请求,返回响应,并轻松捕获在HTTP请求中发送的多种类型的参数。
该包包含3个类,它们是
Client
Request
Response
类。以下我们将通过示例尝试解释每个类的作用。
安装
您可以使用以下命令安装此包。
composer require beycanpress/http
用法
客户端
通过客户端类,您可以通过创建HTTP请求与API进行连接。
<?php require_once __DIR__ . '/vendor/autoload.php'; use BeycanPress\Http\Client; $client = new Client(); // Methods // If you are going to operate with specific endpoints on a single API, you can define a base URL. $client->setBaseUrl(/* string API base url */); // The package uses cURL, so if you want to add or delete any cURL option, you can use the following methods. Also, "CURLOPT_RETURNTRANSFER" in the example is "true" by default. If you need to make it "false", you need to use it as follows. $client->addOption(CURLOPT_RETURNTRANSFER, false); // or $client->addOptions([ CURLOPT_RETURNTRANSFER => false, ]); // or if you want to delete an option $client->deleteOption(CURLOPT_RETURNTRANSFER); // or $client->deleteOptions([ CURLOPT_RETURNTRANSFER ]); // For example, many APIs require a bearer token in the header to allow access to the API. If you want to add or delete a header data, you can use the following methods. $client->addHeader('Authorization', 'Bearer ' . $token); // or $client->addHeaders([ 'Authorization' => 'Bearer ' . $token, ]); $client->deleteHeader('Authorization'); // or $client->deleteHeaders([ 'Authorization' ]); // If you wanna get cURL info or error, you can use the following methods. $client->getInfo(); $client->getError(); // Request Methods // You can use the following method to find out which request methods are accepted. $client->getMethods(); // But we can say it directly here. The following methods are supported. `GET` `HEAD` `POST` `PUT` `DELETE` `CONNECT` `OPTIONS` `TRACE` `PATCH` // When creating a request, all you need to do is call the method of the request type, for example POST and GET are given below. $client->post(/* string $url */, /* array $data */); $client->get(/* string $url */, /* array $data */); // if you want send raw data, just add true as third parameter. $client->post(/* string $url */, /* array $data */, true);
请求
通过请求类,您可以轻松捕获HTTP请求中发送的参数。
<?php require_once __DIR__ . '/vendor/autoload.php'; use BeycanPress\Http\Request; $request = new Request(); // Methods // You can know the request method with the following method. $method = $request->getMethod(); // GET, POST, PUT, DELETE, etc. // You can get the request data with the following method. $errors = $request->getErrors(); // Returns an array of errors. // only for post request data $data = $request->post(/* string $key */); // only for get request data $data = $request->get(/* string $key */); // only for get file data $file = $request->files(/* string $key */); // only for get all request data $data = $request->getParam(/* string $key */); // only for get all request data $allParams = $request->getAllParams(); // only for get header data $headerParam = $request->getHeaderParam(/* string $key */); // only for get all header data $headerParams = $request->getAllHeaderParams(); // only for get json type data $json = $request->json(/* string $key */); // only for get xml type data $json = $request->xml(/* string $key */); // get php content - 'php://input' $json = $request->getContent(/* string $key */);
响应
通过响应类,您可以返回HTTP请求的响应。
<?php require_once __DIR__ . '/vendor/autoload.php'; use BeycanPress\Http\Response; // Methods Response::success(/* string $message */, /* mixed $data */); // 200 Response::error(/* string $message */, /* string $errorCode */, /* mixed $data */, /* int $responseCode */); // $responseCode default: 200 Response::badRequest(/* string $message */, /* string $errorCode */, /* mixed $data */); // 400 Response::unAuthorized(/* string $message */, /* string $errorCode */, /* mixed $data */); // 401 Response::forbidden(/* string $message */, /* string $errorCode */, /* mixed $data */); // 403 Response::notFound(/* string $message */, /* string $errorCode */, /* mixed $data */); // 404 Response::notAcceptable(/* string $message */, /* string $errorCode */, /* mixed $data */); // 406 Response::serverInternal(/* string $message */, /* string $errorCode */, /* mixed $data */); // 500