abollinger/http
Composer 安装命令:
composer require abollinger/http
包简介
A library to perform HTTP requests using curl.
README 文档
README
Abollinger\Http\Client is a lightweight, static PHP HTTP client for making GET, POST, PUT, and DELETE requests. It simplifies HTTP interactions with support for custom headers, data payloads, and response parsing (including headers and body).
Features
- Static Methods: Easy-to-use static methods for common HTTP verbs (
get,post,put,delete). - Header Support: Custom headers for requests and parsed response headers.
- JSON Parsing: Automatic JSON decoding for response bodies, with fallback to raw output.
- Error Handling: Clear error messages and HTTP status code validation.
- cURL-Based: Uses PHP’s cURL for reliable and flexible HTTP communication.
Requirements
- PHP 7.4 or higher
- cURL extension enabled
- Composer autoloading
Installation
Include the class via Composer autoloading. Install the Abollinger\Http package using composer. Run the following command:
composer require abollinger/http
Then, load the class in your code:
require_once 'vendor/autoload.php'; use Abollinger\Http\Client;
Usage
Making a GET Request
$response = Client::get([ 'url' => 'https://api.example.com/data', 'headers' => ['Authorization' => 'Bearer token123'] ]);
Making a POST Request
$response = Client::post([ 'url' => 'https://api.example.com/data', 'data' => ['key' => 'value'], 'headers' => ['Content-Type' => 'application/json'] ]);
Retrieving Headers Only
$headers = Client::getHeaders([ 'url' => 'https://api.example.com/data' ]);
You can use this method to obtain normalized headers (example: "content type" becomes "Content-Type"):
$headers = Client::getNormalizedHeaders([ 'url' => 'https://api.example.com/data' ]);
Handling the Response
The response is always an associative array with the following structure:
[
'status' => 200, // HTTP status code
'headers' => ['Content-Type' => 'application/json', ...], // Parsed headers
'body' => ['data' => '...'] // Parsed body (or ['raw' => '...'] if not JSON)
]
Error Handling
If the request fails, the response will include an error key:
if (isset($response['error'])) { echo "Error: " . $response['error']; }
API Reference
Static Methods
get(array $params = []): array
Performs a GET request. Returns the parsed response body.
getHeaders(array $params = []): array
Performs a GET request. Returns the parsed response headers.
getNormalizedHeaders(array $params = []): array
Performs a GET request. Normalize and returns the parsed response headers.
post(array $params = []): array
Performs a POST request. Returns the parsed response body.
put(array $params = []): array
Performs a PUT request. Returns the parsed response body.
delete(array $params = []): array
Performs a DELETE request. Returns the parsed response body.
Parameters
All methods accept an associative array of parameters:
| Key | Type | Description |
|---|---|---|
url |
string | Required. The target URL. |
method |
string | HTTP method (GET, POST, PUT, DELETE). Default: GET. |
data |
array | Request payload. Automatically URL-encoded for GET, or sent as-is for POST/PUT. |
headers |
array | Custom headers. Default: ['Content-Type: application/x-www-form-urlencoded']. |
Example Workflow
use Abollinger\Http\Client; // GET request $data = Client::get(['url' => 'https://api.example.com/users']); // POST request with custom headers $result = Client::post([ 'url' => 'https://api.example.com/users', 'data' => ['name' => 'John Doe'], 'headers' => ['Authorization' => 'Bearer token123'] ]);
License
This library is licensed under the MIT License. For full license details, see the LICENSE file distributed with this source code.
Author
Antoine Bollinger Email: abollinger@partez.net
For contributions, issues, or feedback, feel free to reach out or open an issue on the relevant repository.
abollinger/http 适用场景与选型建议
abollinger/http 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 129 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 12 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 abollinger/http 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 abollinger/http 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 129
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-12-02