php - 谷歌上的操作如何通过 php 做出响应?
问题描述
这是我的 php 代码响应,但我收到“来自 webhook 的错误无效响应:无法将 JSON 转换为 ExecuteHttpResponse”。
这是生成 JSON 响应的 webhook 代码,但谷歌使用此返回 json 抛出无效错误:
$method = $_SERVER['REQUEST_METHOD'];
if($method == 'POST')
{
$requestBody = file_get_contents('php://input');
$json = json_decode($requestBody, true, 512, JSON_BIGINT_AS_STRING);
$customer_name = $json["requestJson"]["intent"]["params"]["customer_name"]["original"];
$returnText="Customer name is $customer_name"
$response = new \stdClass();
$response->speech = $returnText;
$response->displayText = $returnText;
$response->source = "webhook";
echo json_encode($response);
}
带有无效错误的 webhook 响应来自 webhook 的无效响应:无法将 JSON 转换为 ExecuteHttpResponse
"responseJson": {
"session": {
"id": "1234"
},
"textToSpeech": "bala",
"displayText": "bala",
"source": "webhook"
}
我究竟做错了什么?
解决方案
问题是您的响应 JSON 与Actions on Google 所期望的响应正文格式不匹配。
你需要一个类似于 JSON 的结构
{
"prompt": {
"firstSimple": {
"speech": "bala",
"text": "bala"
}
}
}
你可能可以用 PHP 做这样的事情(未经测试):
$response = array (
'prompt' => array (
'firstSimple' => array (
'speech' => $returnText,
'text' => $returnText
)
)
);
echo json_encode( $response );
推荐阅读
- model - 仅包含几何图形的 3D 模型文件格式
- azure-cosmosdb - 如何在 Azure Cosmos DB 中使用 SUM 聚合函数
- angular - 如何使用 Angular 在每次点击高级搜索时重置模式
- typescript - 在 webpack 中查询
- c# - 带有用户名身份验证的 WCF 中的异常
- python - 升级 brew 后 virtualenvwrapper 的问题
- security - 有什么方法可以检测特定浏览器扩展是否正在与服务器通信?
- php - 为什么 Google Analytics API for PHP 会忽略 2017-11-05 之前的日期
- python - 如何将输出文件转换为数组
- java - Spring Batch 是否在处理完每个批次后释放堆内存?