fanout/pubcontrol

此软件包已被废弃,不再维护。作者建议使用 fanout/grip 软件包。

一个用于使用 EPCP 协议发布消息的 PHP 便利库。

2.3.0 2017-05-23 19:13 UTC

This package is not auto-updated.

Last update: 2021-08-31 00:39:53 UTC


README

作者:Konstantin Bokarius kon@fanout.io

PHP 的 EPCP 库。

许可证

php-pubcontrol 在 MIT 许可证下提供。请参阅 LICENSE 文件。

要求

  • openssl
  • curl
  • pthreads(异步发布所需)
  • firebase/php-jwt >=1.0.0(通过 Composer 自动获取)

安装

使用 Composer: 'composer require fanout/pubcontrol'

手动:确保已包含 php-jwt,并在 php-pubcontrol 中包含以下文件

require 'php-pubcontrol/src/format.php';
require 'php-pubcontrol/src/item.php';
require 'php-pubcontrol/src/pubcontrolclient.php';
require 'php-pubcontrol/src/pubcontrol.php';

异步发布

为了进行异步发布调用,必须安装 pthreads。如果没有安装 pthreads,则只能进行同步发布调用。要安装 pthreads,请使用以下标志重新编译 PHP: '--enable-maintainer-zts'

请注意,由于传递给 publish_async 方法的回调将在单独的线程中执行,因此该回调及其所属类将受到 pthreads 扩展施加的规则和限制。

有关 pthreads 的更多信息,请参阅: https://php.ac.cn/manual/en/book.pthreads.php

用法

<?php

class HttpResponseFormat extends PubControl\Format
{
    private $body = null;

    public function __construct($body)
    {
        $this->body = $body;
    }

	function name()
    { 
        return 'http-response';
    }

	function export()
    {
        return array('body' => $this->body);
    }
}

function callback($result, $message)
{
    if ($result)
        Print "Publish successful\r\n";
    else
        Print "Publish failed with message: {$message}\r\n";
}

// PubControl can be initialized with or without an endpoint configuration.
// Each endpoint can include optional JWT authentication info.
// Multiple endpoints can be included in a single configuration.

// Initialize PubControl with a single endpoint:
$pub = new PubControl\PubControl(array(
        'uri' => 'https://api.fanout.io/realm/<myrealm>',
        'iss' => '<myrealm>', 'key' => base64_decode('<realmkey>')));

// Add new endpoints by applying an endpoint configuration:
$pub->apply_config(array(array('uri' => '<myendpoint_uri_1>'), 
        array('uri' => '<myendpoint_uri_2>')));

// Remove all configured endpoints:
$pub->remove_all_clients();

// Explicitly add an endpoint as a PubControlClient instance:
$pubclient = new PubControl\PubControlClient('<myendpoint_uri>');
// Optionally set JWT auth: $pubclient->set_auth_jwt(<claim>, '<key>');
// Optionally set basic auth: $pubclient->set_auth_basic('<user>', '<password>');
$pub->add_client($pubclient);

// Publish across all configured endpoints synchronously:
$pub->publish('<channel>', new PubControl\Item(
        new HttpResponseFormat("Test publish!")));

// Use publish_async for async publishing only if pthreads are installed:
// if ($pub->is_async_supported())
//     $pub->publish_async('<channel>', new PubControl\Item(
//     new HttpResponseFormat("Test async publish!")));
// Wait for all async publish calls to complete:
// $pub->finish();
?>