百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
P

phpcent

> 编程语言
开源

用于与 Centrifugo HTTP API 通信的 PHP 库

187 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

用于与 Centrifugo HTTP API 通信的 PHP 库

phpcent

PHP library to communicate with Centrifugo v5 HTTP API (for Centrifugo v4 use phpcent v5.x, for Centrifugo v3 use phpcent v4.x).

Library is published on the Composer: https://packagist.org/packages/centrifugal/phpcent

bash
composer require centrifugal/phpcent:~6.0

See Centrifugo server API documentation.

Basic Usage:

php
$client = new \phpcent\Client("http://localhost:8000/api");
$client->setApiKey("Centrifugo API key");
$client->publish("channel", ["message" => "Hello World"]);

You can use phpcent to create connection token (JWT):

php
$token = $client->setSecret("Centrifugo secret key")->generateConnectionToken($userId);

Connection token that will be valid for 5 minutes:

php
$token = $client->setSecret("Centrifugo secret key")->generateConnectionToken($userId, time() + 5*60);

It's also possible to generate channel subscription token:

php
$token = $client->setSecret("Centrifugo secret key")->generateSubscriptionToken($userId, $channel);

It's also possible to generate channel subscription token with expiration time, for example token for 30 mins:

php
$token = $client->setSecret("Centrifugo secret key")->generateSubscriptionToken($userId, $channel, time() + 30*60);

Also API key and secret can be set in constructor:

php
$client = new \phpcent\Client("http://localhost:8000/api", "", "");

Timeouts:

php
$client->setConnectTimeoutOption(0); // Seconds | 0 = never
$client->setTimeoutOption(2); // Seconds

All available API methods:

php
$response = $client->publish($channel, $data);
$response = $client->broadcast($channels, $data);
$response = $client->subscribe($channel, $userId);
$response = $client->unsubscribe($channel, $userId);
$response = $client->disconnect($userId);
$response = $client->presence($channel);
$response = $client->presenceStats($channel);
$response = $client->history($channel);
$response = $client->historyRemove($channel);
$response = $client->channels();
$response = $client->info();
$response = $client->batch($data);

To use assoc option while decoding JSON in response:

php
$client->setUseAssoc(true);

SSL

In case if your Centrifugo server has invalid SSL certificate, you can use:

php
$client->setSafety(false);

You can also use self signed certificate in safe manner:

php
$client = new \phpcent\Client("https://localhost:8000/api");
$client->setCert("/path/to/certificate.pem");
$client->setCAPath("/ca/path"); // if you need.

Note: Certificate must match with host name in Client address (localhost in example above).

DNS Resolution

This error may indicate your system is having trouble resolving IPv6 addresses:

cURL error: Resolving timed out after [value] milliseconds

By default, both IPv4 and IPv6 addresses will attempt to be resolved. You can force it to only resolve IPv4 addresses with:

php
$client->forceIpResolveV4();

Testing

Requirements:

  • git
  • A supported version of PHP
  • composer
  • Docker

The provided PHPUnit tests assume that a local Centrifugo server is running and available at port 8000. This can be accomplished using Docker and the official Centrifugo image.

bash
# Install package dependencies.
$ composer install

# The following command starts a Centrifugo server running in a background Docker container.
$ docker run -d -p 8000:8000 --name centrifugo centrifugo/centrifugo centrifugo --api_insecure

# Run the test suite.
$ vendor/bin/phpunit

# Shut down the Centrifugo container.
$ docker stop centrifugo

Authors

  • Dmitriy Soldatenko
  • Dmitriy Tetekin

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHP

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言