承接 aternos/curl-psr 相关项目开发

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

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

aternos/curl-psr

Composer 安装命令:

composer require aternos/curl-psr

包简介

A simple PSR-18 HTTP client based on cURL that supports actual streaming

README 文档

README

A simple PSR-18 client implementation based on cURL that actually supports streaming both requests and responses.

Installation

composer require aternos/curl-psr

In addition to PSR-18 (HTTP Client), this library also provides implementations for PSR-17 (HTTP Factories) and PSR-7 (HTTP Messages), so no other implementations need to be installed.

Usage

Creating a client

$client = new \Aternos\CurlPsr\Psr18\Client();

When creating a client, you can optionally provide PSR-17 ResponseFactoryInterface and UriFactoryInterface instances. By default, the client will use the Aternos\CurlPsr\Psr17\Psr17Factory class included in this library.

Additionally, you can pass an optional UriResolverInterface instance, which is used to resolve redirect targets.

Configuring the client

Since PSR-7 does not offer many request options, you can set client-wide options that are used for all requests. Requests will use the client options as they are at the moment they are sent. Changing client options will therefore not affect already running requests.

$client->setTimeout(10) // Set the timeout to 10 seconds
       ->setMaxRedirects(5) // Set the maximum number of redirects to follow to 5
       ->setCookieFile("/path/to/cookie/file") // Set the path to the cURL cookie file 
       ->setCurlOption(CURLOPT_DNS_SHUFFLE_ADDRESSES, true) // Set a custom cURL option
       ->setDefaultHeaders(["User-Agent" => ["MyClient/1.0"]]) // Set default headers for all requests
       ->addDefaultHeader("Accept", "application/json"); // Add a default header

$client->setProgressCallback(function (
    \Psr\Http\Message\RequestInterface $request, 
    int $downloadTotal, 
    int $downloaded, 
    int $uploadTotal, 
    int $uploaded
) {
    // Progress callback
});

Custom cURL options

You can set custom cURL options using the setCurlOption method. Note that some options cannot be set, since they are used internally by the client.

Redirects

The client will follow redirects by default. You can set the maximum number of redirects to follow using the setMaxRedirects method. It is also possible to disable redirects using setFollowRedirects. The difference between setting the maximum number of redirects to 0 and disabling redirects is that the former will throw an exception if a redirect is received, while the latter will simply return the redirect response.

Only when status 303 See Other is received, the client will automatically change the request method to GET and remove the request body. Historically, this behavior was also sometimes present for 301 and 302, so it is possible to enable it for other status codes using the setRedirectToGetStatusCodes method.

Status 300 Multiple Choices will only be treated as a redirect if the Location header is present. Otherwise, the response will be returned as is.

To manage how redirect targets are resolved, or limit what locations the client can be redirected to, you can pass an instance of UriResolverInterface to the client constructor.

When a redirect response is received that does not prompt the client to change the request method to GET and the body stream cannot be rewound, an exception is thrown. This is because the client cannot resend the request with the same body stream.

Progress callback

The progress callback function works the same way as the CURLOPT_PROGRESSFUNCTION in cURL, except that it receives the PSR-7 request object instead of a cURL handle as the first argument. Please note that the request object passed to the callback is not necessarily same instance that was originally passed to the sendRequest method. This is because PSR-7 request objects are immutable, so the client will create a new request object if changes are necessary (e.g. to add default headers).

Sending a request

$factory = new \Aternos\CurlPsr\Psr17\Psr17Factory();

$request = $factory->createRequest("GET", "https://example.com")
    ->withHeader("X-Some-Header", "Some Value");
    ->withBody($streamFactory->createStream("Some body"));

$response = $client->sendRequest($request);

$headers = $response->getHeaders();
$stream = $response->getBody();

echo $stream->getContents();

CurlPsr can send any PSR-7 request object and return a PSR-7 response object. For more information on how to use PSR-7 objects, see the PSR-7 documentation.

aternos/curl-psr 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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