adaiasmagdiel/cururu
Composer 安装命令:
composer require adaiasmagdiel/cururu
包简介
A lightweight PHP library that simplifies making HTTP requests with cURL, offering a minimalistic approach for handling web APIs with an easy-to-use interface and essential features, ideal for smaller projects.
README 文档
README
Cururu is a lightweight PHP library designed to simplify making HTTP requests with cURL. It offers a minimalistic approach to handling web APIs, providing an easy-to-use interface with essential features. This makes it ideal for smaller projects that need efficient HTTP request handling without the overhead of larger libraries.
Features
- Simple and intuitive interface for making HTTP requests.
- Optional base URL configuration, or you can use full URLs directly in requests.
- Configurable headers and timeouts.
- SSL verification toggle for secure or insecure requests.
- cURL error handling and automatic response parsing.
- Support for JSON, form-data, and URL-encoded bodies in POST requests.
- Lightweight and easy to integrate into any PHP project.
Installation
To include Cururu in your project, use Composer. First, ensure you have Composer installed, then run the following command:
composer require adaiasmagdiel/cururu
Usage
Basic Example
<?php require 'vendor/autoload.php'; use AdaiasMagdiel\Cururu\Cururu; // Initialize Cururu with optional base URL and headers $cururu = new Cururu('https://api.example.com', ['Authorization' => 'Bearer your_token']); // Make a GET request $response = $cururu->get('/resource'); // Get response content if ($response->isSuccessful()) { echo $response->getContent(); } else { echo "Request failed with status: " . $response->getStatusCode(); } // Make a POST request with JSON data $response = $cururu->post('/resource', ['name' => 'Cururu'], ['Content-Type' => 'application/json']); if ($response->isSuccessful()) { $json = $response->getJson(); print_r($json); }
Using Full URLs without Base URL
Cururu allows you to skip the base URL entirely and use full URLs directly in the request methods.
$cururu = new Cururu(); // Make a GET request using the full URL $response = $cururu->get('https://api.example.com/resource');
Setting Custom Headers
You can set additional headers for each request or globally for the instance.
$cururu = new Cururu('https://api.example.com', ['Authorization' => 'Bearer your_token']); $customHeaders = ['Custom-Header' => 'CustomValue']; $response = $cururu->get('/resource', $customHeaders);
Handling POST Requests
Cururu supports different content types for POST requests, such as application/json and application/x-www-form-urlencoded.
$data = ['name' => 'Cururu', 'description' => 'Simple HTTP client']; $response = $cururu->post('/resource', $data, ['Content-Type' => 'application/json']); if ($response->isSuccessful()) { echo "Resource created successfully!"; }
Error Handling
Cururu provides error handling for cURL operations. In case of a request failure, an exception will be thrown with a descriptive error message.
try { $response = $cururu->get('/invalid-endpoint'); } catch (Exception $e) { echo "Error: " . $e->getMessage(); }
Response Handling
Cururu provides a Response object that encapsulates the HTTP response, offering methods to work with the response content, status code, and headers.
$response = $cururu->get('/resource'); // Get status code $status = $response->getStatusCode(); // Get response content as JSON $data = $response->getJson(); // Get specific header $contentType = $response->getHeader('Content-Type');
License
Cururu is licensed under the MIT License.
Contributing
Contributions are welcome! Please fork this repository and submit a pull request with your changes.
adaiasmagdiel/cururu 适用场景与选型建议
adaiasmagdiel/cururu 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 09 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 adaiasmagdiel/cururu 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 adaiasmagdiel/cururu 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 4
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-09-14