marein / php-nchan-client

PHP 版的 https://nchan.io 客户端。

3.1.1 2023-12-23 23:08 UTC

This package is auto-updated.

Last update: 2024-09-24 00:51:51 UTC


README

CI

目录

概述

这是一个针对 https://nchan.io 的 PHP 客户端。

安装和需求

composer require marein/php-nchan-client

如果您想使用 PSR-18 适配器,请安装一个实现 PSR-18 http 客户端的库 (见此处) 以及一个实现 PSR-17 http 工厂接口的库 (见此处)。

如果您想使用内置的 http 客户端(默认值,除非您进行了设置),请启用 php 配置 allow_url_fopen

使用方法

以下代码示例使用内置的 http 客户端。

发布消息

显示代码
<?php

namespace {

    use Marein\Nchan\Api\Model\PlainTextMessage;
    use Marein\Nchan\Nchan;

    include '/path/to/autoload.php';

    $nchan = new Nchan('http://my-nchan-domain');
    $channel = $nchan->channel('/path-to-publisher-endpoint');
    $channelInformation = $channel->publish(
        new PlainTextMessage(
            'my-message-name',
            'my message content'
        )
    );

    // Nchan returns some channel information after publishing a message.
    var_dump($channelInformation);
}

获取频道信息

显示代码
<?php

namespace {

    use Marein\Nchan\Nchan;

    include '/path/to/autoload.php';

    $nchan = new Nchan('http://my-nchan-domain');
    $channel = $nchan->channel('/path-to-publisher-endpoint');
    $channelInformation = $channel->information();

    var_dump($channelInformation);
}

删除频道

显示代码
<?php

namespace {

    use Marein\Nchan\Nchan;

    include '/path/to/autoload.php';

    $nchan = new Nchan('http://my-nchan-domain');
    $channel = $nchan->channel('/path-to-publisher-endpoint');
    $channel->delete();
}

Nchan 状态信息

带有 nchan_stub_status 指令的端点可以按以下方式查询。

显示代码
<?php

namespace {

    use Marein\Nchan\Nchan;

    include '/path/to/autoload.php';

    $nchan = new Nchan('http://my-nchan-domain');
    $status = $nchan->status('/path-to-status-location');
    $statusInformation = $status->information();

    var_dump($statusInformation);
}

授权请求

带有 nchan_authorize_request 指令的端点必须授权。内置 http 客户端的构造函数接受一个类型为 Credentials 的实现。此库包含 2 个内置实现,分别是 BasicAuthenticationCredentialsBearerAuthenticationCredentials

显示代码
<?php

namespace {

    use Marein\Nchan\HttpAdapter\BasicAuthenticationCredentials;
    use Marein\Nchan\HttpAdapter\BearerAuthenticationCredentials;
    use Marein\Nchan\HttpAdapter\HttpStreamWrapperClient;
    use Marein\Nchan\Nchan;

    include '/path/to/autoload.php';

    // Client with basic authentication
    $adapter = new HttpStreamWrapperClient(
        new BasicAuthenticationCredentials('nchan', 'password')
    );

    // Client with bearer authentication
    $adapter = new HttpStreamWrapperClient(
        new BearerAuthenticationCredentials('my-token')
    );

    $nchan = new Nchan('http://my-nchan-domain', $adapter);
}

如果您通过 PSR-18 适配器 使用其他 http 客户端,则相应的 http 客户端在其发送请求之前有自己的扩展点来修改请求。

PSR-18 兼容性

此库包含一个 PSR-18 兼容的 适配器。不使用内置客户端有很好的理由。它基于 http 流包装器和 file_get_contents。这会在每次请求后关闭 TCP 连接。其他客户端,见下文,可以保持连接打开。

以下示例使用 guzzlehttp/guzzleguzzlehttp/psr7

显示代码
<?php

namespace {

    use GuzzleHttp\Client;
    use GuzzleHttp\Psr7\HttpFactory;
    use Marein\Nchan\HttpAdapter\Psr18ClientAdapter;
    use Marein\Nchan\Nchan;

    include '/path/to/autoload.php';

    $nchan = new Nchan(
        'http://my-nchan-domain',
        new Psr18ClientAdapter(
            new Client(),
            new HttpFactory(),
            new HttpFactory()
        )
    );
}

以下代码示例使用 symfony/http-clientnyholm/psr7

显示代码
<?php

namespace {

    use Marein\Nchan\HttpAdapter\Psr18ClientAdapter;
    use Marein\Nchan\Nchan;
    use Nyholm\Psr7\Factory\Psr17Factory;
    use Symfony\Component\HttpClient\HttpClient;
    use Symfony\Component\HttpClient\Psr18Client;

    include '/path/to/autoload.php';

    // Symfony itself needs an adapter to be PSR-18 compliant.
    $httpClient = new Psr18Client(
        HttpClient::create(),
        new Psr17Factory(),
        new Psr17Factory()
    );

    $nchan = new Nchan(
        'http://my-nchan-domain',
        new Psr18ClientAdapter(
            $httpClient,
            $httpClient,
            $httpClient
        )
    );
}