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
)
);
}