PHP code example of superbalist / php-pubsub-google-cloud

1. Go to this page and download the library: Download superbalist/php-pubsub-google-cloud library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

superbalist / php-pubsub-google-cloud example snippets


putenv('GOOGLE_APPLICATION_CREDENTIALS=' . __DIR__ . '/../your-gcloud-key.json');

$client = new \Google\Cloud\PubSub\PubSubClient([
    'projectId' => 'your-project-id-here',
]);

$adapter = new \Superbalist\PubSub\GoogleCloud\GoogleCloudPubSubAdapter($client);


// disable auto topic & subscription creation
$adapter->setAutoCreateTopics(false); // this is true by default
$adapter->setAutoCreateSubscriptions(false); // this is true by default

// set a unique client identifier for the subscriber
$adapter->setClientIdentifier('search_service');

// consume messages
// note: this is a blocking call
$adapter->subscribe('my_channel', function ($message) {
    var_dump($message);
});

// publish messages
$adapter->publish('my_channel', 'HELLO WORLD');
$adapter->publish('my_channel', json_encode(['hello' => 'world']));
$adapter->publish('my_channel', 1);
$adapter->publish('my_channel', false);
bash
composer 
bash
$ php examples/GoogleCloudConsumerExample.php
$ php examples/GoogleCloudPublishExample.php (in a separate shell)