lucinda/requester 问题修复 & 功能扩展

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

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

lucinda/requester

Composer 安装命令:

composer require lucinda/requester

包简介

Light weight complete object oriented cURL wrapper for HTTP/HTTPS requests

README 文档

README

Table of contents:

About

This API is a light weight cURL wrapper aimed at completely hiding chaotic native library underneath through a full featured OOP layer that is easy and logical to work with. It is built as an antithesis of Guzzle, the "industry standard" today, by being:

  • self-reliant: unlike Guzzle, which has no less than 40 dependencies, it depends only on 1 library for unit testing
  • very simple: each class is designed to cover an aspect of a URL request and response processing, but only a limited number are relevant for developers
  • very fast: all code inside is developed on "less is more" paradigm: no over abstraction, no line of code more than strictly needed

Library is 99% unit tested (some areas of cURL functionality have zero documentation), fully PSR-4 compliant, only requiring PHP8.1+ interpreter and cURL extension. For installation you just need to write this in console:

composer require lucinda/requester

Then use one of main classes provided:

Each of above classes branches through its methods to deeper classes that become relevant depending on the complexity of request. The end result of every request is a Lucinda\URL\Response object, encapsulating all response information (headers, body, status).

Running single requests

Performing a single HTTPs request is as simple as this:

$request = new Lucinda\URL\Request("https://www.lucinda-framework.com");
$response = $request->execute();

This automatically sends target host embedded CAINFO certificate along with URL in order to establish a TLS connection then receives a Lucinda\URL\Response object. The class in charge of single requests is Lucinda\URL\Request, defining following public methods:

Method Arguments Returns Description
__construct ?string $url = null void Opens a cURL handle and optionally sets target URL
setURL string $url void Manually sets request URL
Throws Lucinda\URL\RequestException if url is invalid
setMethod Lucinda\URL\Request\Method $method void Sets request method as one of Lucinda\URL\Request\Method enum values otherwise assumes GET!
Throws Lucinda\URL\RequestException if request method is invalid
setParameters array $parameters = [] Lucinda\URL\Request\Parameters Sets POST request parameters directly (by key and value) or delegates to specialized class.
setRaw string $body void Sets binary request parameters, available if request method is POST / GET / DELETE!
setHeaders void Lucinda\URL\Request\Headers Sets request headers by delegating to specialized class
setSSL string $certificateAuthorityBundlePath Lucinda\URL\Request\SSL Sets custom certificate authority bundle to use in SSL requests and delegates to specialized class.
If not used, API will employ embedded cacert.pem file downloaded from official site!
setCustomOption int $curlopt,
mixed $value
void Sets a custom CURLOPT_* request option not covered by API already.
Throws Lucinda\URL\RequestException if option is already covered.
setReturnTransfer bool $returnTransfer void Sets whether or not response body should be return (default TRUE if method not used)
execute int $maxRedirectionsAllowed = 0,
int $timeout = 300000
Lucinda\URL\Response Executes request and produces a response.
Throws Lucinda\URL\RequestException if invalid request options combination was found.
Throws Lucinda\URL\ResponseException if response retrieval failed (eg: response exceeded timeout).

File Uploading

API comes with Lucinda\URL\Request extensions specifically designed for file upload. To upload a file you must use Lucinda\URL\FileUpload, which comes with following additional public methods:

Method Arguments Returns Description
__destruct void void Closes file handles created by setFile method below
setMethod Lucinda\URL\Request\Method $method void Specializes parent method to only allow POST and PUT requests
setFile string $path void Sets absolute location of file to upload, available if request method is PUT!
Throws Lucinda\URL\FileNotFoundException if file isn't found.
setRaw string $body void Sets binary body of file to upload, available if request method is POST!
setProgressHandler Lucinda\URL\Request\Progress $progressHandler void Sets handle to use in tracking upload progress.
execute int $maxRedirectionsAllowed = 0,
int $timeout = 300000
Lucinda\URL\Response Uploads file, updating response status accordingly.

Example (uploads LOCAL_FILE_PATH to REMOTE_FILE_PATH):

$request = new Lucinda\URL\FileUpload(REMOTE_FILE_PATH);
$request->setMethod(Lucinda\URL\Request\Method::PUT);
$request->setFile(LOCAL_FILE_PATH);
$request->execute();

File Downloading

API comes with Lucinda\URL\Request extensions specifically designed for file download. To download a file you must use Lucinda\URL\FileDownload, which comes with following additional public methods:

Method Arguments Returns Description
setMethod Lucinda\URL\Request\Method $method void Specializes parent method to only allow GET requests
setFile string $path void Sets absolute location where file will be downloaded (incl. file name and extension)!
Using this method is mandatory!
setProgressHandler Lucinda\URL\Request\Progress $progressHandler void Sets handle to use in tracking download progress.
execute int $maxRedirectionsAllowed = 0,
int $timeout = 300000
Lucinda\URL\Response Downloads file, updating response status accordingly.

Example (downloads REMOTE_FILE_PATH into LOCAL_FILE_PATH):

$request = new Lucinda\URL\FileDownload(REMOTE_FILE_PATH);
$request->setMethod(Lucinda\URL\Request\Method::GET);
$request->setFile(LOCAL_FILE_PATH);
$request->execute();

Running multiple asynchronous requests

Performing multiple requests at once it is no less simple:

$multiRequest = new Lucinda\URL\MultiRequest(Lucinda\URL\Request\Pipelining::HTTP2);
$multiRequest->add(new Lucinda\URL\Request("https://www.lucinda-framework.com/news"));
$multiRequest->add(new Lucinda\URL\Request("https://www.lucinda-framework.com/tutorials"));
$responses = $multiRequest->execute();

This executes two requests simultaneously using HTTP2 pipelining and receives a Response object array. The class in charge of single requests is Lucinda\URL\MultiRequest, defining following public methods:

Method Arguments Returns Description
__construct Lucinda\URL\Request\Pipelining $pipeliningOption void Initiates a multi URL connection based on one of enum values
add Request $request void Adds request to execution pool.
setCustomOption int $curlMultiOpt,
mixed $value
void Sets a custom CURLMOPT_* request option not covered by API already.
Throws Lucinda\URL\RequestException if option is already covered
setReturnTransfer bool $returnTransfer void Sets whether or not response body should be return (default TRUE if method not used)
execute bool $returnTransfer = true,
int $maxRedirectionsAllowed = 0,
int $timeout = 300000
Lucinda\URL\Response [] Validates requests in pool then executes them asynchronously in order to produce responses.
Throws Lucinda\URL\RequestException if invalid request options combination was found.
Throws Lucinda\URL\ResponseException if response retrieval failed (eg: response exceeded timeout).

Unlike native curl_multi, responses will be received in the order requests were pooled! Execution will be performed depending on pipelining option (see constructor):

  • DISABLED: connections are not pipelined
  • HTTP1: attempts to pipeline HTTP/1.1 requests on connections that are already established
  • HTTP2: attempts to multiplex the new transfer over an existing connection if HTTP/2
  • HTTP1_HTTP2: attempts pipelining and multiplexing independently of each other

Running cookie sharing synchronous requests

To make multiple requests share cookies/dns, it is as simple as:

$sharedRequest = new Lucinda\URL\SharedRequest(Lucinda\URL\Request\ShareType::COOKIE);
$request1 = new Lucinda\URL\Request("http://www.example.com/page1");
$multiRequest->add($request1);
$request2 = new Lucinda\URL\Request("http://www.example.com/page1");
$multiRequest->add($request2);
$response1 = $request1->execute();
$response2 = $request2->execute();

This very poorly documented feature makes 2nd request able to see cookies in first request. The class in charge of cookie-sharing requests is Lucinda\URL\SharedRequest, defining following public methods:

Method Arguments Returns Description
__construct Lucinda\URL\Request\ShareType $shareOption void Initiates a shared URL connection based on one of enum values
add Request $request void Adds request to share pool.

Unlike the other two classes of running requests, each request must be executed manually in order to produce a response! Cookie sharing will be performed depending on share type option (see constructor):

  • COOKIES: connections will share HTTP cookies
  • DNS: connections will share cached DNS hosts
  • SSL_SESSION: connections will share SSL session IDs

Working with HTTP cookies

API comes with a number of classes for working with cookies, whose area is IN BETWEEN requests and responses:

They are rarely needed in everyday usage, so for more info click on their links to see documentation. To see how they can be used, please check respective unit tests!

Working with responses

The end result of every successful request (defined as one that received ANY response) is encapsulated into a Lucinda\URL\Response object that includes:

  • response status: HTTP status that came with response
  • response headers: HTTP headers received along with response
  • response body: actual contents of response (minus headers and status)
  • response statistics: as supplied by API or cURL driver underneath (eg: total duration)

All this information can be queried via following public methods:

Method Arguments Returns Description
getDuration void int Gets total response duration in milliseconds
getStatusCode void int Gets response HTTP status code
getURL void string Gets url requested
getBody void string Gets response body
getHeaders void string[string] Gets response headers by name and value
getCustomOption int $curlinfo mixed Gets value of a custom CURLINFO_* response option not covered by API already.
Throws Lucinda\URL\ResponseException if option is already covered

Examples

HTTP GET Request

# any request that doesn't *setMethod* is considered GET by default
$request = new Lucinda\URL\Request("https://www.example.com/?id=1");
$response = $request->execute();

HTTP POST Request

# any POST request MUST *setParameters*
$request = new Lucinda\URL\Request("https://www.example.com/add");
$request->setMethod(\Lucinda\URL\Request\Method::POST);
$request->setParameters(["asd"=>"fgh", "qwe"=>"rty"]);
$response = $request->execute();

HTTP PUT Request

# any PUT request MUST *setRaw* AND stringify parameters
$request = new Lucinda\URL\Request("https://www.example.com/edit");
$request->setMethod(\Lucinda\URL\Request\Method::PUT);
$request->setRaw(http_build_query(["id"=>1, "qwe"=>"tyu"]));
$response = $request->execute();

HTTP DELETE Request

# any DELETE request MUST *setRaw* AND stringify parameters
$request = new Lucinda\URL\Request("https://www.example.com/delete");
$request->setMethod(\Lucinda\URL\Request\Method::DELETE);
$request->setRaw(http_build_query(["id"=>1]));
$response = $request->execute();

Error handling

Following exceptions may be thrown during request-response process by this API:

A few observations:

  • No support for curl_share_err errors, for whom there is zero documentation in both PHP cURL documentation as well as the C library it wraps
  • As long as response is received for a request, no exception is thrown! API does not consider statuses such as 404 or 500 to be errors by default, so it is up to developers to decide how to handle them

lucinda/requester 适用场景与选型建议

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

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

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

围绕 lucinda/requester 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-10-27