utopia-php/fetch 问题修复 & 功能扩展

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

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

utopia-php/fetch

Composer 安装命令:

composer require utopia-php/fetch

包简介

A simple library that provides an interface for making HTTP Requests.

README 文档

README

Lite & fast micro PHP library that provides a convenient and flexible way to perform HTTP requests in PHP applications.

Usage

Create an instance of the Client class to perform HTTP requests. The instance methods allow for setting request options like headers, timeout, connection timeout, and more.

The fetch() method on a Client instance accepts the following parameters:

  • url - A String containing the URL to which the request is sent.
  • method - A String containing the HTTP method for the request. The default method is GET.
  • body - An array of data to send as the body of the request, which can be an associative array for form data or a JSON string.
  • query - An associative array of query parameters.

The Response object returned by fetch() provides several methods to inspect the response:

  • isOk() - Checks if the response status code is within the 200-299 range.
  • getBody() - Retrieves the response body as a string.
  • getHeaders() - Fetches the response headers as an associative array.
  • getStatusCode() - Gets the response status code.
  • json() - Decodes the response body to an associative array.
  • text() - Alias for getBody(), returns the response body as a string.
  • blob() - Returns the response body as a blob.

Examples

GET Request

require_once __DIR__ . '/vendor/autoload.php';
use Utopia\Fetch\Client;

$client = new Client();
$url = 'https://httpbin.org/get';
$method = 'GET';
$query = ['foo' => 'bar'];

// Optionally set more configurations
$client
  ->setUserAgent('CustomUserAgent/1.0')
  ->setAllowRedirects(true)
  ->setMaxRedirects(1)
  ->setConnectionTimeout(10)
  ->setTimeout(30);

$resp = $client->fetch(
    url: $url,
    method: $method,
    query: $query
);

if ($resp->isOk()) {
    echo "Status Code: " . $resp->getStatusCode() . "\n";
    echo "Response Headers:\n";
    print_r($resp->getHeaders());
    echo "Response Body:\n";
    echo $resp->getBody();
} else {
    echo "Error: " . $resp->getStatusCode() . "\n";
}

POST Request

require_once __DIR__ . '/vendor/autoload.php';
use Utopia\Fetch\Client;

$client = new Client();
$url = 'https://httpbin.org/post';
$method = 'POST';
$headers = ['Content-Type' => 'application/json'];
$body = ['name' => 'John Doe'];
$query = ['foo' => 'bar'];

// Set request headers
$client->addHeader('Content-Type', 'application/json');

$resp = $client->fetch(
    url: $url,
    method: $method,
    body: $body,
    query: $query
);

print_r($resp->json());

Sending a file

require_once __DIR__ . '/vendor/autoload.php';
use Utopia\Fetch\Client;

$client = new Client();
$url = 'http://localhost:8000/upload';
$method = 'POST';

// Ensure you set the appropriate Content-Type for file upload
$client->addHeader('Content-Type', 'multipart/form-data');

$filePath = realpath(__DIR__ . '/tests/resources/logo.png'); // Absolute path to the file
$body = ['file' => new \CURLFile($filePath, 'image/png', 'logo.png')];

$resp = $client->fetch(
    url: $url,
    method: $method,
    body: $body
);

print_r($resp->json());

utopia-php/fetch 适用场景与选型建议

utopia-php/fetch 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 254.55k 次下载、GitHub Stars 达 10, 最近一次更新时间为 2023 年 10 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 utopia-php/fetch 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 10
  • Watchers: 5
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-10-16