PHP Class Google\Cloud\PubSub\PubSubClient
To enable the
Google Cloud Pub/Sub Emulator,
set the
PUBSUB_EMULATOR_HOST
environment variable.
This client supports transport over
REST or
gRPC.
In order to enable gRPC support please make sure to install and enable
the gRPC extension through PECL:
sh
$ pecl install grpc
Afterwards, please install the following dependencies through composer:
sh
$ composer require google/gax && composer require google/proto-client-php
Please take care in installing the same version of these libraries that are
outlined in the project's composer.json require-dev keyword.
NOTE: Support for gRPC is currently at an Alpha quality level, meaning it is still
a work in progress and is more likely to get backwards-incompatible updates.
Example:
use Google\Cloud\ServiceBuilder;
$cloud = new ServiceBuilder();
$pubsub = $cloud->pubsub();
PubSubClient can be instantiated directly.
use Google\Cloud\PubSub\PubSubClient;
$pubsub = new PubSubClient();
Using the Pub/Sub Emulator
use Google\Cloud\ServiceBuilder;
Be sure to use the port specified when starting the emulator.
8900
is used as an example only.
putenv('PUBSUB_EMULATOR_HOST=http://localhost:8900');
$cloud = new ServiceBuilder();
$pubsub = $cloud->pubsub();
Afficher le fichier
Open project: GoogleCloudPlatform/gcloud-php
Protected Properties
Свойство |
Type |
Description |
|
$connection |
ConnectionInterface |
|
|
Méthodes publiques
Méthode |
Description |
|
__construct ( array $config = [] ) |
Create a PubSub client. |
|
consume ( array $requestData ) : Message |
Consume an incoming message and return a PubSub Message. |
|
createTopic ( string $name, array $options = [] ) : Topic |
Create a topic. |
|
subscribe ( string $name, string $topicName, array $options = [] ) : Subscription |
Create a Subscription. If the subscription does not exist, it will be
created. |
|
subscription ( string $name, string $topicName = null ) : Subscription |
Lazily instantiate a subscription with a subscription name. |
|
subscriptions ( array $options = [] ) : Generator |
Get a list of the subscriptions registered to all of your project's
topics. |
|
topic ( string $name ) : Topic |
Lazily instantiate a topic with a topic name. |
|
topics ( array $options = [] ) : Generator |
Get a list of the topics registered to your project. |
|
Private Methods
Method Details
__construct()
public méthode
public __construct ( array $config = [] ) |
$config |
array |
[optional] {
Configuration Options.
@type string $projectId The project ID from the Google Developer's
Console.
@type CacheItemPoolInterface $authCache A cache for storing access
tokens. **Defaults to** a simple in memory implementation.
@type array $authCacheOptions Cache configuration options.
@type callable $authHttpHandler A handler used to deliver Psr7
requests specifically for authentication.
@type callable $httpHandler A handler used to deliver Psr7 requests.
Only valid for requests sent over REST.
@type string $keyFile The contents of the service account
credentials .json file retrieved from the Google Developers
Console.
@type string $keyFilePath The full path to your service account
credentials .json file retrieved from the Google Developers
Console.
@type int $retries Number of retries for a failed request.
**Defaults to** `3`.
@type array $scopes Scopes to be used for the request.
@type string $transport The transport type used for requests. May be
either `grpc` or `rest`. **Defaults to** `grpc` if gRPC support
is detected on the system.
} |
This method is for use with push delivery only.
Example:
$httpPostRequestBody = file_get_contents('php://input');
$requestData = json_decode($httpPostRequestBody, true);
$message = $pubsub->consume($requestData);
createTopic()
public méthode
Unlike {@see \Google\Cloud\PubSub\PubSubClient::topic()}, this method will send an API call to
create the topic. If the topic already exists, an exception will be
thrown. When in doubt, use {@see \Google\Cloud\PubSub\PubSubClient::topic()}.
Example:
$topic = $pubsub->createTopic('my-new-topic');
echo $topic->info()['name']; // projects/my-awesome-project/topics/my-new-topic
subscribe()
public méthode
Use {@see \Google\Cloud\PubSub\PubSubClient::subscription()} to create a subscription object
without any API requests. If the topic already exists, an exception will
be thrown. When in doubt, use {@see \Google\Cloud\PubSub\PubSubClient::subscription()}.
Example:
Create a subscription. If it doesn't exist in the API, it will be created.
$subscription = $pubsub->subscribe('my-new-subscription', 'my-topic-name');
public subscribe ( string $name, string $topicName, array $options = [] ) : Subscription |
$name |
string |
A subscription name |
$topicName |
string |
The topic to which the new subscription will be subscribed. |
$options |
array |
[optional] Please see {@see \Google\Cloud\PubSub\Subscription::create()}
for configuration details. |
Résultat |
Subscription |
|
subscription()
public méthode
This method will NOT perform any API calls. If you wish to create a new
subscription, use {@see \Google\Cloud\PubSub\PubSubClient::subscribe()}.
Unless you are sure the subscription exists, you should check its
existence before using it.
Example:
$subscription = $pubsub->subscription('my-new-subscription');
subscriptions()
public méthode
Example:
$subscriptions = $pubsub->subscriptions();
foreach ($subscriptions as $subscription) {
$info = $subscription->info();
echo $info['name']; // projects/my-awesome-project/subscriptions/
}
public subscriptions ( array $options = [] ) : Generator |
$options |
array |
[optional] {
Configuration Options
@type int $pageSize Maximum number of results to return per
request.
} |
Résultat |
Generator |
|
No API requests are made by this method. If you want to create a new
topic, use {@see \Google\Cloud\PubSub\Topic::createTopic()}.
Example:
No API request yet!
$topic = $pubsub->topic('my-new-topic');
This will execute an API call.
echo $topic->info()['name']; // projects/my-awesome-project/topics/my-new-topic
Example:
$topics = $pubsub->topics();
foreach ($topics as $topic) {
$info = $topic->info();
echo $info['name']; // projects/my-awesome-project/topics/my-new-topic
}
public topics ( array $options = [] ) : Generator |
$options |
array |
[optional] {
Configuration Options
@type int $pageSize Maximum number of results to return per
request.
} |
Résultat |
Generator |
|
Property Details
$connection protected_oe property
protected ConnectionInterface $connection |
Résultat |
ConnectionInterface |
|