overtrue/http

一个简单的 HTTP 客户端包装器。

维护者

详细信息

github.com/overtrue/http

源代码

问题

资助包维护!
overtrue

1.2.3 2022-03-14 06:24 UTC

README

Http

🌵 一个简单的 HTTP 客户端包装器。

Build Status Latest Stable Version Latest Unstable Version Build Status Scrutinizer Code Quality Total Downloads License

Sponsor me

安装

$ composer require overtrue/http -vvv

使用方法

<?php

use Overtrue\Http\Client;

$client = Client::create(); 

$response = $client->get('https://httpbin.org/ip');
//{
//    "ip": "1.2.3.4"
//}

配置

use Overtrue\Http\Client;

$config = [
    'base_uri' => 'https://www.easyhttp.com/apiV2/',
    'timeout' => 3000,
    'headers' => [
        'User-Agent' => 'MyClient/1.0',
        'Content-Type' => 'application/json'
    ]
    //...
];

$client = Client::create($config); // or new Client($config);

//...

自定义响应类型

$config = new Config([
    'base_uri' => 'https://www.easyhttp.com/apiV2/',
    
    // array(default)/collection/object/raw
    'response_type' => 'collection', 
]);

//...

记录请求和响应

安装 monolog

$ composer require monolog/monolog

添加日志中间件

use Overtrue\Http\Client;

$client = Client::create();

$logger = new \Monolog\Logger('my-logger');

$logger->pushHandler(
    new \Monolog\Handler\RotatingFileHandler('/tmp/my-log.log')
);

$client->pushMiddleware(\GuzzleHttp\Middleware::log(
                            $logger,
                            new \GuzzleHttp\MessageFormatter(\GuzzleHttp\MessageFormatter::DEBUG)
                        ));

$response = $client->get('https://httpbin.org/ip');

❤️ 赞助我

Sponsor me

如果你喜欢我的项目并想支持它,点击这里 ❤️

由 JetBrains 支持的项目

非常感谢 JetBrains 好意提供许可证,让我能够参与这个和其他开源项目的工作。

PHP 扩展包开发

想知道如何从零开始构建 PHP 扩展包吗?

请关注我的实战课程,我会在此课程中分享一些扩展开发经验 —— 《PHP 扩展包实战教程 - 从入门到发布》

许可证

MIT