【问题标题】:How to add attributes to Google Pubsub message using Google_Service_Pubsub_Topics_Resource (PHP)?如何使用 Google_Service_Pubsub_Topics_Resource (PHP) 向 Google Pubsub 消息添加属性?
【发布时间】:2019-06-26 14:34:05
【问题描述】:

我有一些遗留代码,我需要使用 Google Pubsub 旧 API Google_Service_Pubsub_Topics_Resource 来发布消息。如何为消息添加属性?我不断收到错误消息:

(publish) unknown parameter: 'myattr'

代码如下:

$pubsubService = new Google_Service_Pubsub($client);
$req = new Google_Service_Pubsub_PublishRequest();
$req->setTopic("testtopic");
$msg = new Google_Service_Pubsub_PubsubMessage();
$msg->setData("....");
$req->setMessage($msg);
$opts = array(
    "myattr" => "test"
);
$ret = $pubsubService->topics->publish($req, $opts);

【问题讨论】:

    标签: php google-cloud-pubsub


    【解决方案1】:

    看着google cloud documentation

    你需要把你的属性放在一个带有 attributes 键的数组中。

    所以是这样的:

    $pubsubService->topics->publish($req, ['attributes' => ['myattr'=>'test']] );
    

    【讨论】:

    • 试过了,得到:未知参数:'属性'。请注意,我没有使用新的 pubsub API,而是使用上面提到的旧版 API。在网上查起来很困难,这里有一个例子:api.kdyby.org/class-Google_Service_Pubsub_Topics_Resource.html
    • 来自 programcreek.com/java-api-examples/… 的示例 4 是否适合您?
    • 这个例子是用 Java 编写的,而不是 PHP。虽然这个 Java API 版本似乎明确允许您指定属性,但我无法在我正在使用的 PHP API 版本中找到如何做到这一点。
    猜你喜欢
    • 2021-02-01
    • 2015-01-23
    • 2011-07-10
    • 2020-01-20
    • 1970-01-01
    • 2012-02-04
    • 2018-05-20
    • 1970-01-01
    • 2020-02-27
    相关资源
    最近更新 更多