hellonico/guzzle-cli-handler

Guzzle CLI 处理器,用于模拟通过 CLI 进行 HTTP 请求

0.1.0 2021-05-06 12:08 UTC

This package is auto-updated.

Last update: 2024-09-06 19:33:43 UTC


README

Guzzle 处理器,通过 CLI 模拟 HTTP 请求。

动机

执行请求而无需运行 Web 服务器。

用法

use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use HelloNico\GuzzleCliHandler\CliHandler;

// Document root
$docRoot = '/var/www';

// File path
// - If absolute, will directly use $filePath
// - If relative, will be `$docRoot . DIRECTORY_SEPARATOR . $filePath`
// - If URL requested ends with .php, will be overrided and set to `$docRoot . DIRECTORY_SEPARATOR . $urlPath`
$filePath = 'index.php';

$cliHandler = new CliHandler(
    $docRoot,
    $filePath,
    function (array &$globals) {
        // you can mofify global variables here before execution
        $globals['_ENV'] = ['a' => 'Lorem', 'b' => 'ipsum'];
    }
);

$client = new Client(['handler' => HandlerStack::create($cliHandler)]);
$response = $client->get('http://localhost/install.php');

安装

composer require hellonico/guzzle-cli-handler

限制

由于 PHP 在 CLI 中运行,很遗憾无法获取响应头并维护会话。

开发

  • composer test - 运行 phpunit
  • composer analyse - 运行 phpstan
  • composer lint - 运行 ecs(编码规范)