php - 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));
解决方案
我遇到了同样的问题,以下解决方案帮助了我。希望这可以帮助某人。
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);
推荐阅读
- r - 如何删除数据中应该有数字的文本
- php - 出现验证错误时,似乎无法让表单输入保持不变
- corda - 我可以创建一个流程来将状态迁移到新版本而不是使用合约升级吗?
- npm - NPM 发布模块未检测到 git 存储库
- python - 如何在控制台上的打印语句附近打印 return 语句的结果?
- couchbase - 如何在 N1ql 中使用带有“光标”的分页?
- excel - 1004 将范围从一张纸复制到另一张纸时出错
- spring-batch - 为现有作业设置独立的春季批处理作业管理门户
- firebase - Firebase 身份验证:单击自定义操作链接不验证电子邮件
- java - 如何在递归实体中找到对象?