首页 > 解决方案 > PHP 从 pubsub 获取数据

问题描述

我有一个 pub/sub 项目,我想在我的 PHP 项目中使用它,我的主题是设置:在此处输入图像描述

我有一个 php 脚本来服务这个,msg.php:

$file = "text.txt";

$fp = fopen($file, "w");
fwrite($fp, json_encode($_REQUEST));// tried $_POST, $_GET
fclose($fp);

但是我无法从中获取数据,但是似乎执行了脚本(我通过更改 txt 文件的日期看到它)。我总是以 text.txt 中的[]结尾,不管通知是由我通过发布消息选项自动发送还是手动发送。如何从传入的发布/订阅消息中获取数据?

此问题中描述的解决方案对我不起作用 Google Cloud Platform Pub/Sub push empty POST data Google Cloud Pub/Sub Push Messages - Empty POST

我的意思是替换

fwrite($fp, json_encode($_REQUEST));

fwrite(json_decode(file_get_contents('php://input')_);
fwrite(json_decode($HTTP_RAW_POST_DATA));

标签: phpgoogle-cloud-pubsub

解决方案


我遇到了同样的问题,以下解决方案帮助了我。希望这可以帮助某人。

ob_start();

$rawData = file_get_contents("php://input");
echo $rawData."\n";

$JSONObj = json_decode($rawData);

/*All sensitive data comes encoded in base64 and you need to decode that string, which gives another JSON String
*/
echo base64_decode($JSONObj->message->data);

$info = ob_get_contents();
ob_end_clean();

$fp = fopen("test.txt", "w+");
fwrite($fp, $info);
fclose($fp);

推荐阅读