承接 xenoxyt/lruchttp 相关项目开发

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

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

xenoxyt/lruchttp

Composer 安装命令:

composer require xenoxyt/lruchttp

包简介

A php http client!

README 文档

README

- Lightweight resource-utilizing client for HTTP.

A simple, fluent PHP HTTP client library for making API requests with minimal configuration.

Features

  • Fluent, chainable API
  • Support for all standard HTTP methods
  • JSON and form data handling
  • Cookie management
  • Proxy support
  • Automatic retry functionality
  • Automatic response parsing (JSON, XML, Text)
  • SSL verification options
  • Redirect handling
  • Timeout configuration
  • Comprehensive error handling
  • Download functionality
  • Logging support

Installation

composer require xenoxyt/lruchttp

Basic Usage

<?php
require_once 'vendor/autoload.php';

$client = new LrucHTTPClient();

// Simple GET request
$response = $client->get('https://api.example.com/data');
echo $response['parsed']; // Automatically parsed based on Content-Type

// POST JSON data
$response = $client->postJson('https://api.example.com/users', [
    'name' => 'John Doe',
    'email' => 'john@example.com'
]);

// Download a file
$client->download('https://example.com/large-file.zip', '/path/to/save/file.zip');

Advanced Usage

Chaining Methods

$response = $client->prepareReq('POST', 'https://api.example.com/data')
    ->setHeader([
        'X-API-Key' => 'your-api-key',
        'Accept' => 'application/json'
    ])
    ->setJsonBody(['key' => 'value'])
    ->timeout(5000) // 5 seconds
    ->retry(3, 1000) // retry 3 times with 1 second between attempts
    ->sendReq();

Using Configuration Array

$response = $client->req([
    'Method' => 'POST',
    'URL' => 'https://api.example.com/data',
    'Header' => [
        'X-API-Key' => 'your-api-key'
    ],
    'JsonBody' => [
        'name' => 'John Doe',
        'email' => 'john@example.com'
    ],
    'Timeout' => 5000,
    'Retry' => 3
]);

Authentication

// Basic Auth
$response = $client->prepareReq('GET', 'https://api.example.com/protected')
    ->setBasicAuth('username', 'password')
    ->sendReq();

// Bearer Token
$response = $client->prepareReq('GET', 'https://api.example.com/protected')
    ->setAuthorization('Bearer', 'your-token-here')
    ->sendReq();

Working with Cookies

$client->prepareReq('GET', 'https://api.example.com')
    ->addCookie('session_id', 'abc123')
    ->sendReq();

// After response, get cookies that were set
$nextRequest = $client->prepareReq('POST', 'https://api.example.com/next')
    ->setCookie($response['headers']['cookies'])
    ->sendReq();

Custom Logging

$client->setLogger(function($message, $level) {
    echo "[$level] $message\n";
});

Handling Different Response Formats

// Force JSON parsing regardless of Content-Type
$response = $client->prepareReq('GET', 'https://api.example.com/data')
    ->expectFormat('json')
    ->sendReq();

Response Structure

The response from any request is an associative array with the following keys:

  • success: Boolean indicating if the request completed without errors
  • status_code: HTTP status code (e.g., 200, 404, 500)
  • body: Raw response body
  • parsed: Response body parsed according to content type
  • headers: Associative array of response headers
  • error: Error information (if request failed)
  • attempts: Number of attempts made

Error Handling

$response = $client->get('https://api.example.com/data');

if (!$response['success']) {
    echo "Request failed: " . $response['error']['message'];
} else if ($response['status_code'] >= 400) {
    echo "Server error: " . $response['status_code'];
} else {
    // Process successful response
    $data = $response['parsed'];
}

License

MIT License - Copyright (c) 2025 XenoXYT

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-08

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固