jdriscoll/curl-psr
Composer 安装命令:
composer require jdriscoll/curl-psr
包简介
A utility for using PSR-7 Request and Response objects with Curl
README 文档
README
This lets you run Curl requests with PSR-7 objects. This has a few benefits:
- It keeps you away from Curl syntax, which is complicated when you're doing more than just downloading a file
- If you're proxying, this requires less code as PSR-7 ServerRequest is
compliant with PSR-7 Request - in other words, a modified version of your
$requestwould usually work fine. - Sending the response back is simpler
- Streaming responses is significantly more straightwarward
Usage
$handler = new \CurlPsr\Handler();
$response = $handler->run(
$request->withUri(
$request->getUri()
->withPath("/")
->withHost("example.org")
->withScheme("http")
->withPort(80)
),
true,
10000
);
The primary aim of this is to make JSON API proxies simpler.
Limitations
This does not attempt to support every possible Curl feature nor everything that can be expressed in PSR-7 requests/responses. Key functionality that should always work includes:
- HEAD requests with properly retained headers
- GET requests to JSON endpoints which may or may not stream
- JSON-RPC request and response passthrough via POST
- DELETE requests
- OPTIONS requests
- PUT requests for short JSON resources
Advanced Usage
If you want to do two or more requests in parallel, you can. To do this, you use:
$handler = new \CurlPsr\Handler();
$responses = $handler->withTimeout(10000)->runSimple(
$request1,
$request2
);
foreach($responses as $k => $response) {
if($response->getBody()->getSize()) {
echo "" . $response->getUri();
echo "" . $response->getBody();
}
}
The return to runSimple() is in fact an iterator which will run over the same
objects multiple times: once for the headers, once or more for body chunks, and
once more when the transfer is known to be complete. The body attached to the
response will always be up-to-date with the latest packet, but won't report a
size until it's done.
If you want specific keys to make it easier to tell which response is which, you
can use runMap():
$handler = new \CurlPsr\Handler();
$responses = $handler->withTimeout(10000)->runMap([
"a" => $request1,
"b" => $request2
]);
foreach($responses as $k => $response) {
if($response->getBody()->getSize()) {
echo "" . $response->getUri();
echo "" . $response->getBody();
}
}
jdriscoll/curl-psr 适用场景与选型建议
jdriscoll/curl-psr 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 30 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 05 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 jdriscoll/curl-psr 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jdriscoll/curl-psr 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 30
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 13
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: BSD-2-Clause
- 更新时间: 2019-05-18