shiros/luna-curl 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

shiros/luna-curl

Composer 安装命令:

composer require shiros/luna-curl

包简介

Luna Module - Curl

README 文档

README

pipeline status coverage report

# Luna Module - Curl A PHP **Curl Module** designed for enhancing HTTP request management in the **Luna Framework**. **Lightweight**, **Flexible**, and **OOP-focused** - Simplifying your curl operations in PHP.

[[TOC]]

ℹ️ About the Project

This project is developed in PHP 8.2 and is part of the Luna Framework ecosystem.

The Luna Curl Module facilitates Curl integration by encapsulating PHP's native curl methods in an Object-Oriented Programming (OOP) approach. This enables structured and reusable Curl workflows.

Detailed documentation is available in the Wiki: Luna Wiki.

Key Features

  • OOP Curl Management: Encapsulates PHP Curl functions into an ease-of-use wrapper.
  • Error Handling: Custom exception classes streamline error detection and debugging.
  • Reusable Design: Define and reuse Curl configurations easily.
  • PSR-4 Autoloading: Clean and autoloaded code structure.
  • PHP 8.2 Typed Syntax: Leverages the latest PHP features, promoting clean and maintainable code.

🔧 Dependencies

It uses PHP 8.2+, ensuring support for the latest features, and requires minimal setup to get started.

This module depends on the following:

Refer to the composer.json file for additional details.

⚙️ Setup and Installation

To use the Luna Curl module in your project, follow these steps:

Step 1: Install via Composer

First, ensure you have Composer installed in your system.
Run the following command in your project's root directory:

composer require shiros/luna-curl    

Step 2: Ensure PHP Dependencies Are Met

Ensure your installation has Curl support enabled in PHP. Check by running:

php -m | grep curl

If not installed, refer to the PHP Manual to enable it based on your OS.

Step 3: Autoload the Module

The module supports PSR-4 autoloading. If you're using the Luna Framework, it's automatically available. Otherwise, make sure to include Composer's autoloader:

require 'vendor/autoload.php';

🚀 Usage Example

Refer to the official documentation for advanced examples and further details.

Handling Curl request

Here's a quick example of how to use the Curl class to perform a Curl request :

// Create the curl instance
$curl = new Curl();

// Create the request
$request = new \Luna\Curl\Entity\CurlRequest(
    url    : 'http://api.example.com',
    method : \Luna\Curl\Entity\CurlMethod::GET,
    headers: [ 'Authorization' => 'Bearer <TOKEN>' ],
    options: [ 
        CURLOPT_TIMEOUT => 30 * 60 // Timeout of 30 min
    ]
);

/**
 * Execute the request and get a response.
 * 
 * To avoid the request's creation, you could use shortcut and call these methods instead.
 * - get(url, headers, options)
 * - post(url, data, headers, options)
 * - put(url, data, headers, options)
 * - patch(url, data, headers, options)
 * - delete(url, data, headers, options)
 */
$response = $curl->request(request: $request);

Handling JSON request

Here's a quick example of how to use the JSONCurl class perform a Curl request :

// Create the curl instance
$curl = new Curl();

// Create the request
$request = new \Luna\Curl\Entity\CurlRequest(
    url    : 'http://api.example.com',
    method : \Luna\Curl\Entity\CurlMethod::POST,
    data   : [
        'firstname' => 'Alexandre',
        'lastname'  => 'Caillot'
    ],
    headers: [ 'Authorization' => 'Bearer <TOKEN>' ],
    options: [ 
        CURLOPT_TIMEOUT => 30 * 60 // Timeout of 30 min
    ]
);

/**
 * Execute the request and get a response.
 * 
 * To avoid the request's creation, you could use shortcut and call these methods instead.
 * - get(url, headers, options)
 * - post(url, data, headers, options)
 * - put(url, data, headers, options)
 * - patch(url, data, headers, options)
 * - delete(url, data, headers, options)
 */
$response = $curl->request(request: $request);

Curl as OOP

Here's a quick example of how to use the CurlWrapper class to initiate, execute, and close a curl request :

// Create the wrapper
$wrapper = new CurlWrapper(url: $url);

try {
    // Initialize & execute curl request
    $response = $wrapper
     ->init()
     ->execute()
    ;
    // You can access the handle later using '$wrapper->getHandle()'.
    
    // Get response's information
    $information = $wrapper->getInformation();
    $error       = $wrapper->getError();

   // Logs data
   var_dump($response);
   var_dump($information);
   var_dump($error);
} catch (Throwable $throwable) {
    echo "Error: " . $throwable->getMessage() . PHP_EOL;
} finally {
    // Close the curl instance
    $wrapper->close();
}

📄 Testing

This project uses PHPUnit for testing, you can run the test suite as follows.

Step 1: Install development dependencies

Before running the test suite, ensure all project dependencies, including development dependencies, are installed. Use Composer** to handle this:

composer install

This command will fetch all the required libraries and ensure your project setup is complete.

Step 2: Execute the Test Suite

Once dependencies are installed, you can execute the test suite using PHPUnit.
This ensures all the functionality of the framework is working as expected:

vendor/bin/phpunit --configuration phpunit.xml --colors=always

The test results will be displayed in your console. Colored output simplifies understanding the testing status:

  • Green: Tests passed successfully.
  • Red: Tests failed.
  • Yellow: Warnings or skipped tests.

For more details on the tests, explore the /tests directory. It contains comprehensive unit tests covering various parts of the framework.

📃 License

This project is licensed under the MIT License, allowing you to use and modify this project freely.
See the LICENSE file for more details.

👨‍💻 Authors and Contributors

This project was created and is maintained by Alexandre Caillot (Shiroe_sama), with contributions from the community.

Authors

Contributors

We thank the following contributors for making this project better:

shiros/luna-curl 适用场景与选型建议

shiros/luna-curl 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 11 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「framework」 「php」 「curl」 「module」 「php framework」 「ShirOS」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 shiros/luna-curl 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 shiros/luna-curl 我们能提供哪些服务?
定制开发 / 二次开发

基于 shiros/luna-curl 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-11-09