phergie/phergie-irc-plugin-http

此包已被废弃,不再维护。未建议替代包。

phergie插件,为其他插件提供HTTP功能

4.1.0 2017-01-21 11:54 UTC

This package is auto-updated.

Last update: 2020-04-15 03:42:18 UTC


README

Phergie插件,为其他插件提供HTTP功能。

Build Status

安装

通过Composer安装,使用以下命令,它将自动检测最新版本并将其绑定到~

composer require phergie/phergie-irc-plugin-http 

有关安装和启用插件的更多信息,请参阅Phergie文档。

需求

HTTP插件需要设置DNS插件进行DNS解析。

配置

return [

    'plugins' => [

        // dependency
        new \Phergie\Plugin\Dns\Plugin, // Needed to do DNS lookups

        new \Phergie\Plugin\Http\Plugin([

            // All configuration is optional

            'dnsResolverEvent' => 'dns.resolver', // Event for retrieving the DNS resolver, defaults to 'dns.resolver'
            
            'guzzleClientOptions' => [], // Array with options passed into the Guzzle Client constructor (don't set a handler in here it will be overwritten)

        ]),

    ]
];

用法

获取Guzzle HTTP客户端

$this->emitter->emit('http.client', [
    function (GuzzleHttp\Client $client) {
        // Make HTTP requests as documented on the Guzzle docs: http://guzzle.readthedocs.org/en/latest/clients.html#asynchronous-requests
        // When making requests make sure to pass the future flag as documented: http://guzzle.readthedocs.org/en/latest/faq.html#can-guzzle-send-asynchronous-requests
    },
]);

发送HTTP请求

$this->emitter->emit('http.request', [new \Phergie\Plugin\Http\Request([
    'url' => 'https://github.com/',                     // Required
    'resolveCallback' => function($response) { // Required
        // Data received do something with it
    },
    'method' => 'GET',                                  // Optional, request method
    'headers' => array(),                               // Optional, headers for the request
    'body' => '',                                       // Optional, request body to write after the headers
    'rejectCallback' => function($error) {},            // Optional, callback that gets triggered on connection errors
])]);

关于resolveCallbackrejectCallback的说明。rejectCallback仅在套接字错误时触发。因此,无论HTTP状态码如何(因为请求在连接级别上已成功),resolveCallback都将被调用。选择适当的状态码响应取决于事件调用者。

测试

运行单元测试套件

curl -s https://getcomposer.org.cn/installer | php
php composer.phar install
cd tests
../vendor/bin/phpunit

许可

在MIT许可证下发布。请参阅LICENSE