ppajer / request
制作 cURL 和多 cURL 请求的直观流畅包装器。
dev-master
2020-07-19 19:27 UTC
This package is auto-updated.
Last update: 2024-09-20 05:06:05 UTC
README
制作 cURL 和多 cURL 请求的直观流畅包装器。
安装
要开始使用此库,您有两个选择:将其添加到项目的依赖项中并使用 composer install
,或者手动下载并包含 autoload.php
文件或类文件。
用法
单个请求 - Request
Request
类代表对一个资源的单个请求及其设置。它提供简单的流畅 API 来创建和编辑请求,并内置了一些常用的常量,以节省您查找用户代理字符串和内容类型的时间。随着时间的推移,还将添加更多的便利常量。
属性
class Request {
const UA_MOZILLA;
const UA_CHROME;
const UA_SAFARI;
const UA_EXPLORER;
const UA_EDGE;
const UA_OPERA;
const CONTENT_TYPE_XML;
const CONTENT_TYPE_XHTML;
const CONTENT_TYPE_TXT;
const CONTENT_TYPE_TTF;
const CONTENT_TYPE_MJS;
const CONTENT_TYPE_JSONLD;
const CONTENT_TYPE_JSON;
const CONTENT_TYPE_JS;
const CONTENT_TYPE_CSV;
const CONTENT_TYPE_CSS;
public $URL; // Default: null
public $UA; // Default: UA_SAFARI
public $method; // Default: "GET"
public $content; // Default: null
public $headers; // Default: null
public $contentType; // Default: CONTENT_TYPE_TXT
public $follow; // Default: true
}
方法
除了 response
方法外,所有方法都返回其 Request
实例,以允许方法链式调用。
class Request {
public function __construct(String $url);
public function URL(String $url) : Request // Sets the URL of the request.
public function userAgent(String $UA) : Request // Sets the userAgent of the request.
public function method(String $method) : Request // Sets the method of the request.
public function content(Array $content) : Request // Sets the content of the request.
public function headers(Array $headers) : Request // Sets the headers of the request.
public function contentType(String $contentType) : Request // Sets the contentType of the request.
public function follow(Bool $follow) : Request // Sets if the request should follow redirects.
public function send() : Request // Processes the request.
public function response() : String // Fetches the response body.
public static function parallel(Array $options) : ParallelRequest // Creates an instance of ParallelRequest to handle multiple URLs. See the docs for ParallelRequest
}
多个请求 - ParallelRequest
ParallelRequest
类允许您异步获取多个资源,与顺序请求相比,可以提供大幅的性能提升。您可以直接使用 new
关键字创建一个,或者通过调用 Request::parallel()
静态方法。
方法
API 的大多数部分与常规请求相同,只是构造函数接受一个请求选项数组。数组必须包含与类参数匹配的键 - 如果提供了这些键,它们将被用作单个请求的设置,缺失的值将使用类的默认值填充。有关更多详细信息,请参阅ParallelRequest 构造函数选项。
class ParallelRequest {
public function __construct(Array $requestList);
public function awaitAll() : ParallelRequest // Process all requests and wait for them to finish
public function response() : Array // Gets the results of the batch. Each response is found under the same key in the resulting array as the one used to mark its request in the request options array passed to the constructor. Example below.
}
构造函数请求选项示例
$opts = [
'someKey' => [
'URL' => 'https://...',
'method' => 'POST',
'content' => [
'foo' => 'bar'
]
],
'someOtherKey' => [
'URL' => 'https://...'
]
];
/*
Will return:
[
'someKey' => '<!DOCTYPE html...', // Response from the URL with `?foo=bar` sent via POST
'someOtherKey' => '...' // Response from the URL with default GET
]
*/
完整示例
require 'php-request/autoload.php';
// Single
$url = 'http://some.api.com';
$singleRequest = new Request($url);
$singleRequest->userAgent(Request::UA_CHROME)
->contentType(Request::CONTENT_TYPE_JSON)
->content(json_encode($someData))
->method('POST')
->send();
$response = $singleRequest->response();
// Multiple
$opts = [
'A' => [
'URL' => '...'
],
'B' => [
'URL' => '...'
],
...
];
$parallel = new ParallelRequest($opts);
$response = $parallel->awaitAll()->response();