承接 michel/httpclient 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

michel/httpclient

最新稳定版本:1.0.0

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 GET and POST requests.
  • 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.

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-16