michel/httpclient
Composer 安装命令:
composer require michel/httpclient
包简介
A lightweight PHP HTTP client library without external dependencies. No need curl extension.
README 文档
README
This PHP HTTP Client provides a minimalistic way to perform GET and POST HTTP requests with customizable headers. It allows you to handle JSON data, form-encoded requests, and more, without using cURL.
Installation
You can install this library via Composer. Ensure your project meets the minimum PHP version requirement of 7.4.
composer require michel/httpclient
Requirements
- PHP version 7.4 or higher
Features
- Supports
GETandPOSTrequests. - Customize headers for each request.
- Automatically handles JSON or form-encoded data.
- Easily configurable base URL for all requests.
- Includes error handling for invalid URLs and timeouts.
Usage
Basic GET Request
use Michel\HttpClient\HttpClient;
$client = new HttpClient(['base_url' => 'http://example.com']);
// Perform a GET request
$response = $client->get('/api/data');
if ($response->getStatusCode() === 200) {
echo $response->getBody(); // Raw response body
print_r($response->bodyToArray()); // JSON decoded response
}
GET Request with Query Parameters
$response = $client->get('/api/search', ['query' => 'test']);
POST Request (Form-Encoded)
$data = [
'username' => 'testuser',
'password' => 'secret'
];
$response = $client->post('/api/login', $data);
POST Request (JSON)
$data = [
'title' => 'Hello World',
'content' => 'This is a post content'
];
$response = $client->post('/api/posts', $data, true); // `true` specifies JSON content type
Custom Headers
$client = new HttpClient([
'base_url' => 'http://example.com',
'headers' => ['Authorization' => 'Bearer your_token']
]);
$response = $client->get('/api/protected');
Helper Functions
To make the HTTP client easier to use, we provide a set of helper functions that allow you to quickly send GET and POST requests without needing to manually instantiate the HttpClient class every time.
Available Helper Functions
1. http_client()
This function creates and returns a new HttpClient instance with the provided configuration options.
$client = http_client([
'base_url' => 'http://example.com',
'headers' => ['Authorization' => 'Bearer your_token']
]);
2. http_post()
Use this function to make a POST request with form-encoded data. It sends a request to the given URL with optional data and headers.
$response = http_post('http://example.com/api/login', [
'username' => 'user123',
'password' => 'secret'
]);
3. http_post_json()
This function sends a POST request with JSON-encoded data. Useful for APIs expecting JSON input.
$response = http_post_json('http://example.com/api/create', [
'title' => 'New Post',
'body' => 'This is the content of the new post.'
]);
4. http_get()
Make a GET request using this function. You can include query parameters and headers as needed.
$response = http_get('http://example.com/api/users', [
'page' => 1,
'limit' => 10
]);
Example Usage of Helpers
// Make a GET request
$response = http_get('http://api.example.com/items', ['category' => 'books']);
$data = $response->bodyToArray();
// Make a POST request with form data
$response = http_post('http://api.example.com/login', [
'username' => 'user123',
'password' => 'secret'
]);
// Make a POST request with JSON data
$response = http_post_json('http://api.example.com/posts', [
'title' => 'Hello World',
'content' => 'This is my first post!'
]);
These helper functions simplify making HTTP requests by reducing the need to manually create and configure the HttpClient for each request.
michel/httpclient 适用场景与选型建议
michel/httpclient 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 12 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 michel/httpclient 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 michel/httpclient 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-16