Skip to content

Commit d3b1aab

Browse files
authored
Merge branch 'main' into main
2 parents 7bbcf6d + 7dd8e32 commit d3b1aab

File tree

6 files changed

+396
-0
lines changed

6 files changed

+396
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2025 Google LLC.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* For instructions on how to run the full sample:
21+
*
22+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/blob/main/pubsub/api/README.md
23+
*/
24+
25+
namespace Google\Cloud\Samples\PubSub;
26+
27+
# [START pubsub_create_unwrapped_push_subscription]
28+
use Google\Cloud\PubSub\PubSubClient;
29+
30+
/**
31+
* Create unwrappped push subscription.
32+
*
33+
* @param string $projectId The Google project ID.
34+
* @param string $topicName The Pub/Sub topic name.
35+
* @param string $subscriptionId The ID of the subscription.
36+
*/
37+
function create_unwrapped_push_subscription(
38+
string $projectId,
39+
string $topicName,
40+
string $subscriptionId
41+
): void {
42+
43+
$pubsub = new PubSubClient([
44+
'projectId' => $projectId,
45+
]);
46+
$pubsub->subscribe($subscriptionId, $topicName, [
47+
'pushConfig' => [
48+
'no_wrapper' => [
49+
'write_metadata' => true
50+
]
51+
]
52+
]);
53+
printf('Unwrapped push subscription created: %s' . PHP_EOL, $subscriptionId);
54+
}
55+
# [END pubsub_create_unwrapped_push_subscription]
56+
require_once __DIR__ . '/../../../testing/sample_helpers.php';
57+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2025 Google LLC.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* For instructions on how to run the full sample:
21+
*
22+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/blob/main/pubsub/api/README.md
23+
*/
24+
25+
namespace Google\Cloud\Samples\PubSub;
26+
27+
# [START pubsub_optimistic_subscribe]
28+
use Google\Cloud\Core\Exception\NotFoundException;
29+
use Google\Cloud\PubSub\PubSubClient;
30+
31+
/**
32+
* Optimistically subscribes to a topic
33+
*
34+
* @param string $projectId The Google project ID.
35+
* @param string $topicName The Pub/Sub topic name.
36+
* @param string $subscriptionId The ID of the subscription.
37+
*/
38+
function optimistic_subscribe(
39+
string $projectId,
40+
string $topicName,
41+
string $subscriptionId
42+
): void {
43+
44+
$pubsub = new PubSubClient([
45+
'projectId' => $projectId,
46+
]);
47+
48+
$subscription = $pubsub->subscription($subscriptionId);
49+
50+
try {
51+
$messages = $subscription->pull();
52+
foreach ($messages as $message) {
53+
printf('PubSub Message: %s' . PHP_EOL, $message->data());
54+
$subscription->acknowledge($message);
55+
}
56+
} catch (NotFoundException $e) { // Subscription is not found
57+
printf('Exception Message: %s' . PHP_EOL, $e->getMessage());
58+
printf('StackTrace: %s' . PHP_EOL, $e->getTraceAsString());
59+
// Create subscription and retry the pull. Any messages published before subscription creation would not be received.
60+
$pubsub->subscribe($subscriptionId, $topicName);
61+
optimistic_subscribe($projectId, $topicName, $subscriptionId);
62+
}
63+
}
64+
# [END pubsub_optimistic_subscribe]
65+
require_once __DIR__ . '/../../../testing/sample_helpers.php';
66+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

pubsub/api/src/subscribe_exactly_once_delivery.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ function subscribe_exactly_once_delivery(
3838
): void {
3939
$pubsub = new PubSubClient([
4040
'projectId' => $projectId,
41+
// use the apiEndpoint option to set a regional endpoint
42+
'apiEndpoint' => 'us-west1-pubsub.googleapis.com:443'
4143
]);
4244

4345
$subscription = $pubsub->subscription($subscriptionId);
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2025 Google LLC.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* For instructions on how to run the full sample:
21+
*
22+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/blob/main/pubsub/api/README.md
23+
*/
24+
25+
namespace Google\Cloud\Samples\PubSub;
26+
27+
# [START pubsub_subscriber_error_listener]
28+
use Google\Cloud\PubSub\PubSubClient;
29+
30+
/**
31+
* Subscribes with an error listener
32+
*
33+
* @param string $projectId The Google project ID.
34+
* @param string $topicName The Pub/Sub topic name.
35+
* @param string $subscriptionId The ID of the subscription.
36+
*/
37+
function subscriber_error_listener(
38+
string $projectId,
39+
string $topicName,
40+
string $subscriptionId
41+
): void {
42+
43+
$pubsub = new PubSubClient([
44+
'projectId' => $projectId,
45+
]);
46+
$subscription = $pubsub->subscription($subscriptionId, $topicName);
47+
48+
try {
49+
$messages = $subscription->pull();
50+
foreach ($messages as $message) {
51+
printf('PubSub Message: %s' . PHP_EOL, $message->data());
52+
$subscription->acknowledge($message);
53+
}
54+
} catch (\Exception $e) { // Handle unrecoverable exceptions
55+
printf('Exception Message: %s' . PHP_EOL, $e->getMessage());
56+
printf('StackTrace: %s' . PHP_EOL, $e->getTraceAsString());
57+
}
58+
}
59+
# [END pubsub_subscriber_error_listener]
60+
require_once __DIR__ . '/../../../testing/sample_helpers.php';
61+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2025 Google LLC.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* For instructions on how to run the full sample:
21+
*
22+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/blob/main/pubsub/api/README.md
23+
*/
24+
25+
namespace Google\Cloud\Samples\PubSub;
26+
27+
# [START pubsub_update_topic_type]
28+
use Google\Cloud\PubSub\PubSubClient;
29+
30+
/**
31+
* Changes the type of a Pub/Sub topic.
32+
*
33+
* @param string $projectId The Google project ID.
34+
* @param string $topicName The Pub/Sub topic name.
35+
* @param string $streamArn The Kinesis stream ARN to ingest data from.
36+
* @param string $consumerArn The Kinesis consumer ARN to used for ingestion in Enhanced Fan-Out mode. The consumer must be already created and ready to be used.
37+
* @param string $awsRoleArn AWS role ARN to be used for Federated Identity authentication with Kinesis. Check the Pub/Sub docs for how to set up this role and the required permissions that need to be attached to it.
38+
* @param string $gcpServiceAccount The GCP service account to be used for Federated Identity authentication with Kinesis (via a AssumeRoleWithWebIdentity call for the provided role). The $awsRoleArn must be set up with accounts.google.com:sub equals to this service account number.
39+
*/
40+
function update_topic_type(
41+
string $projectId,
42+
string $topicName,
43+
string $streamArn,
44+
string $consumerArn,
45+
string $awsRoleArn,
46+
string $gcpServiceAccount
47+
): void {
48+
$pubsub = new PubSubClient([
49+
'projectId' => $projectId,
50+
]);
51+
52+
$topic = $pubsub->topic($topicName);
53+
54+
$topic->update([
55+
'ingestionDataSourceSettings' => [
56+
'aws_kinesis' => [
57+
'stream_arn' => $streamArn,
58+
'consumer_arn' => $consumerArn,
59+
'aws_role_arn' => $awsRoleArn,
60+
'gcp_service_account' => $gcpServiceAccount
61+
]
62+
]
63+
], [
64+
'updateMask' => [
65+
'ingestionDataSourceSettings'
66+
]
67+
]);
68+
69+
printf('Topic updated: %s' . PHP_EOL, $topic->name());
70+
}
71+
# [END pubsub_update_topic_type]
72+
require_once __DIR__ . '/../../../testing/sample_helpers.php';
73+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)