ppajer/php-request

此包已被弃用,不再维护。作者建议使用ppajer/request包。

制作 cURL 和多 cURL 请求的直观且流畅的包装器。

dev-master 2020-07-19 19:27 UTC

This package is auto-updated.

Last update: 2020-07-19 19:27:13 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();