十 hyperf 常用工具类使用 guzzle ,log, request

140 阅读1分钟

一 log

1.1 log 介绍 。

hyperf/logger 组件是基于 psr/logger 实现的,默认使用 monolog/monolog 作为驱动,在 hyperf-skeleton 项目内默认提供了一些日志配置,默认使用 Monolog\Handler\StreamHandler, 由于 Swoole 已经对 fopenfwrite 等函数进行了协程化处理,所以只要不将 useLocking 参数设置为 true,就是协程安全的。

1.2 log 安装

composer require hyperf/logger

1.3 配置与使用。

config/autoload/logger.php


<?php

declare(strict_types=1);
use Monolog\Handler;
use Monolog\Formatter;
use Monolog\Logger;
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
return [
    'default' => [
        'handlers' =>
            [
                [
                    'class' =>  \Monolog\Handler\StreamHandler::class,
                    'constructor' => [
                        'stream' => BASE_PATH . '/runtime/logs/hyperf.log',
                        'level' => Logger::INFO,
                    ],
                    'formatter' => [
                        'class' => Formatter\LineFormatter::class,
                        'constructor' => [
                            'format' => "||%datetime%||%channel%||%level_name%||%message%||%context%||%extra%\n",
                            'dateFormat' => null,
                            'allowInlineLineBreaks' => true,
                        ],
                    ],
                ],
                [
                    'class' => Handler\StreamHandler::class,
                    'constructor' => [
                        'stream' => BASE_PATH . '/runtime/logs/hyperf-debug.log',
                        'level' => Logger::DEBUG,
                    ],
                    'formatter' => [
                        'class' => Formatter\JsonFormatter::class,
                        'constructor' => [
                            'batchMode' => Formatter\JsonFormatter::BATCH_MODE_JSON,
                            'appendNewline' => true,
                        ],
                    ],
                ],
            ]
    ],
];

用法。 先注入,再使用。

protected LoggerInterface $logger;

public function __construct(LoggerFactory $loggerFactory)
{
    // 第一个参数对应日志的 name, 第二个参数对应 config/autoload/logger.php 内的 key
    $this->logger = $loggerFactory->get('log', 'default');
}


// Hyperf 会自动为此方法生成一个 /user/index 的路由,允许通过 GET 或 POST 方式请求
#[RequestMapping(path: "rq/get", methods: "get")]
public function get()
{
    // Do something.
    $this->logger->info("request-debug \t ". json_encode($this->request->query()));

    // 从请求中获得 id 参数
   // return $this->code(0, $this->request->query());
}

二 guzzle

2.1 介绍

Guzzle is a PHP HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services.

  • Simple interface for building query strings, POST requests, streaming large uploads, streaming large downloads, using HTTP cookies, uploading JSON data, etc...
  • Can send both synchronous and asynchronous requests using the same interface.
  • Uses PSR-7 interfaces for requests, responses, and streams. This allows you to utilize other PSR-7 compatible libraries with Guzzle.
  • Supports PSR-18 allowing interoperability between other PSR-18 HTTP Clients.
  • Abstracts away the underlying HTTP transport, allowing you to write environment and transport agnostic code; i.e., no hard dependency on cURL, PHP streams, sockets, or non-blocking event loops.
  • Middleware system allows you to augment and compose client behavior.

2.2 安装


 composer require guzzlehttp/guzzle
 
 
 72 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
No security vulnerability advisories found
Using version ^7.7 for guzzlehttp/guzzle
 

2.3 使用。

未完待续。