php nchan client
3.1.1
目录
这是 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_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 个内置实现:BasicAuthenticationCredentials 和 BearerAuthenticationCredentials。
<?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 兼容适配器。有充分的理由不使用内置客户端。它基于 http 流包装器和file_get_contents
。这会在每次请求后关闭 TCP 连接。其他客户端(见下文)可以保持连接打开。
以下示例使用 guzzlehttp/guzzle 和 guzzlehttp/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-client 和 nyholm/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
)
);
}