shyam-achuthan / google-cloud
一个用于轻松访问谷歌云平台的库 - PubSub (Amazon SQS 的替代品)
0.1.2
2016-08-19 03:19 UTC
Requires
- google/apiclient: ^2.0.0@RC
This package is not auto-updated.
Last update: 2024-09-20 22:37:50 UTC
README
一个易于使用的PHP谷歌Pub/Sub平台
composer require shyam-achuthan/google-cloud
初始化库
$projectId = "<your-google-project-id>";
$key_file_location = "<path-to-service-account-json-credentials";
$pubsub = new \GoogleCloud\Pubsub($projectId,$key_file_location);
$pubsub->setTopic("<your-message-topic>");
$pubsub->setSubscription("<your-pull-subscription-name>");
向队列发送消息
$data = ["name"=>"Shyam",'email'=>"shyam@shyamachuthan.com" ]; // string key value pair data to pass to the queue receiver
$message = "SEND_USER_WELCOME_EMAIL"; // any text message here am using this string to differentiate the actions on the receiver side
$response = $pubsub->sendMessage($message,$data);
$resp = $response;
接收队列消息
$resp = $pubsub->receiveMessages();
将返回包含三个属性的 message_title - [String] 发送消息时传递的消息 - data - [Array] 数据数组(键值对,如发送消息时传递的)- handle - [String] 消息的唯一标识符,用于删除消息的 acknoledgement-id
删除消息
$response = $pubsub->deleteMessage($message->handle);
接收消息执行操作并删除消息
$resp = $pubsub->receiveMessages();
foreach($resp as $message){
// Do what ever executin needed with $message->message_title, $message->data
$pubsub->deleteMessage($message->handle);
}
一次性删除多个消息
$pubsub->deleteMessage($handles); // where handles is array of handle/acknowledgement-ids