You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于Google Action Builder对接自有API、PHP开发及本地测试的技术咨询

Google Action Webhook with PHP: Answers to Your Core Questions

Hey there! I’ve been through this exact setup before, so let’s walk through each of your questions to get your Google Action connected to your PHP server smoothly.


1. How to connect Google Action Builder to non-Google third-party services (APIs)?

The first option in the Webhook section is specifically designed for this scenario—linking your Action to custom APIs or external services. Here’s the core workflow:

  • In Action Builder, head to the Webhook tab and select the "Custom webhook" option.
  • Enter the public HTTPS URL of your API endpoint (Google requires secure connections for webhooks).
  • Every time your Action triggers a webhook call (like when an intent needs backend logic), Google’s servers will send a POST request to this URL with a JSON payload containing conversation details. Your API needs to:
    1. Receive and parse the incoming JSON data.
    2. Run your custom logic (e.g., fetch data from a database, call another service).
    3. Return a properly formatted JSON response that Google Assistant can interpret.

2. Can I use PHP to build my own server, and how to interact with Google Action?

Absolutely! PHP works perfectly for this—you just need to handle the JSON request/response cycle correctly. Here’s a step-by-step breakdown:

Step 1: Set up your PHP endpoint

Create a PHP file (e.g., google-action-webhook.php) on your server. This file will handle incoming POST requests from Google.

Step 2: Parse the incoming request

Google sends all conversation data as a JSON payload in the request body. Use php://input to read and decode it:

<?php
// Read raw POST data from Google
$requestBody = file_get_contents('php://input');
$requestData = json_decode($requestBody, true);

// Extract key details (example: intent name and user's input)
$intentName = $requestData['intent']['name'];
$userInput = $requestData['query']['text'];
?>

Step 3: Construct a valid response

Google expects a specific JSON structure for the response. At minimum, you need to include a text/speech response. Here’s a simple example:

<?php
// Add your custom logic here (e.g., query a database, compute data)
$responseText = "Got it! You said: '$userInput'. This is coming from my PHP server.";

// Build the response payload matching Google's requirements
$response = [
    'fulfillment_response' => [
        'messages' => [
            [
                'text' => [
                    'text' => [$responseText]
                ]
            ]
        ]
    ]
];

// Set the correct content type and output the JSON
header('Content-Type: application/json');
echo json_encode($response);
?>

Step 4: Configure the endpoint in Action Builder

Make sure your PHP file is hosted on a server with HTTPS (for local testing, we’ll use a workaround below). Enter the full URL (e.g., https://your-domain.com/google-action-webhook.php) in the Webhook section of Action Builder.


3. Can I test the server response on a local server (like MAMP + phpMyAdmin)?

Yes! The only hurdle is that Google’s servers need to reach your local server (which is usually behind a firewall). Here’s how to make it work:

  • Use an ngrok tunnel: Ngrok creates a public HTTPS URL that forwards requests to your local server.
    1. Download ngrok (it’s a portable tool, no installation needed).
    2. Start your MAMP server—confirm your PHP endpoint is accessible at http://localhost:8888/google-action-webhook.php (adjust the port to match your MAMP setup).
    3. Open a terminal and run: ngrok http 8888 (replace 8888 with your MAMP port).
    4. Ngrok will generate a public URL (e.g., https://abc123.ngrok.io). Use this plus your PHP file path as the webhook URL in Action Builder: https://abc123.ngrok.io/google-action-webhook.php.
  • Test with Postman first: Before connecting to Google Action, you can test your endpoint directly. Create a POST request with a sample Google Action JSON payload (grab one from the Action Builder simulator’s logs) and send it to your local URL. Verify the response matches the required format.

内容的提问来源于stack exchange,提问作者XCarb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 14:38:14