Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
谷歌如何通过php做出响应?_Php_Webhooks_Actions On Google_Google Assistant_Actions Builder - Fatal编程技术网

谷歌如何通过php做出响应?

谷歌如何通过php做出响应?,php,webhooks,actions-on-google,google-assistant,actions-builder,Php,Webhooks,Actions On Google,Google Assistant,Actions Builder,这是我的php代码响应,但我得到了“来自webhook的错误无效响应:未能将JSON转换为ExecuteHttpResponse” 以下是生成JSON响应的webhook代码,但google在返回JSON时抛出无效错误: $method = $_SERVER['REQUEST_METHOD']; if($method == 'POST') { $requestBody = file_get_contents('php://input'); $json = json_decode($reques

这是我的php代码响应,但我得到了“来自webhook的错误无效响应:未能将JSON转换为ExecuteHttpResponse”

以下是生成JSON响应的webhook代码,但google在返回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响应与Google上的操作预期不匹配

您需要一个类似于JSON的结构

{
“提示”:{
“第一简单”:{
《讲话》:“巴拉”,
“文本”:“巴拉”
}
}
}
您可能可以使用PHP执行以下操作(未经测试):

$response=array(
'提示'=>数组(
'firstSimple'=>数组(
“speech”=>$returnText,
“文本”=>$returnText
)
)
);
echo json_编码($response);

仍然得到没有错误的空输出,谷歌说“对不起,我没有得到任何回应”。还有什么要做的吗?或者我遗漏了什么?在StackOverflow上把代码放在注释中是个坏主意。更好的方法是更新问题,或者,既然你已经接受了这个答案,那么继续创建一个新问题。