chillerlan/php-http-message-utils

PSR-7/17/18 工具

2.2.2 2024-07-26 16:04 UTC

This package is auto-updated.

Last update: 2024-09-03 20:03:33 UTC


README

一组不依赖框架的实用工具,用于与 PSR-7 消息实现 一起使用。

PHP Version Support version license Continuous Integration Coverage Codacy Packagist downloads

文档

要求

安装

需要 composer

composer.json (注意:将 dev-main 替换为一个 版本边界,例如 ^2.2)

{
	"require": {
		"php": "^8.1",
		"chillerlan/php-http-message-utils": "dev-main#<commit_hash>"
	}
}

盈利!

使用方法

URLExtractor

URLExtractor 包装一个 PSR-18 ClientInterface,以提取和跟踪缩短的 URL 到其原始位置。

// @see https://github.com/chillerlan/php-httpinterface
$options                 = new HTTPOptions;
$options->user_agent     = 'my cool user agent 1.0';
$options->ssl_verifypeer = false;
$options->curl_options   = [
	CURLOPT_FOLLOWLOCATION => false,
	CURLOPT_MAXREDIRS      => 25,
];

$httpClient   = new CurlClient($responseFactory, $options, $logger);
$urlExtractor = new URLExtractor($httpClient, $responseFactory);

$request = $factory->createRequest('GET', 'https://t.co/ZSS6nVOcVp');

$urlExtractor->sendRequest($request); // -> response from the final location

// you can retrieve an array with all followed locations afterwards
$responses = $urlExtractor->getResponses(); // -> ResponseInterface[]

// if you just want the URL of the final location, you can use the extract method:
$url = $urlExtractor->extract('https://t.co/ZSS6nVOcVp'); // -> https://api.guildwars2.com/v2/build

EchoClient

EchoClient 返回原始消息的 JSON 表示形式

$echoClient = new EchoClient($responseFactory);

$request  = $requestFactory->createRequest('GET', 'https://example.com?whatever=value');
$response = $echoClient->sendRequest($request);
$json     = json_decode($response->getBody()->getContents());

这将产生类似于以下的对象

{
	"headers": {
		"Host": "example.com"
	},
	"request": {
		"url": "https://example.com?whatever=value",
		"params": {
			"whatever": "value"
		},
		"method": "GET",
		"target": "/",
		"http": "1.1"
	},
	"body": ""
}

LoggingClient

LoggingClient 包装一个 ClientInterface,并通过一个 LoggerInterface 以可读的方式输出 HTTP 消息(不要在生产环境中使用!)。

$loggingClient = new LoggingClient($httpClient, $logger);

$loggingClient->sendRequest($request); // -> log to output given via logger

输出看起来类似于以下(使用 monolog

[2024-03-15 22:10:41][debug] LoggingClientTest:
----HTTP-REQUEST----
GET /get HTTP/1.1
Host: httpbin.org


[2024-03-15 22:10:41][debug] LoggingClientTest:
----HTTP-RESPONSE---
HTTP/1.1 200 OK
Date: Fri, 15 Mar 2024 21:10:40 GMT
Content-Type: application/json
Content-Length: 294
Connection: keep-alive
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true

{
  "args": {},
  "headers": {
    "Host": "httpbin.org",
    "User-Agent": "chillerlanPHPUnitHttp/1.0.0 +https://github.com/chillerlan/phpunit-http",
    "X-Amzn-Trace-Id": "Root=1-65f4b950-1f87b9e37182673438091aea"
  },
  "origin": "93.236.207.163",
  "url": "https://httpbin.org/get"
}

API

以下类包含用于与 PSR-7 http 消息对象一起使用的静态方法。

HeaderUtil

QueryUtil

MessageUtil

UriUtil

MimeTypeUtil

StreamUtil

ServerUtil

ServerUtil 对象在调用时需要一组 PSR-17 工厂,即 ServerRequestFactoryInterfaceUriFactoryInterfaceUploadedFileFactoryInterfaceStreamFactoryInterface。它提供了从 超全局变量 创建服务器请求、URI 和上传文件的便捷方法。

Cookie

实现了一个 HTTP Cookie