Home / API.AI / Creating a PHP webhook for DialogFlow (API.AI)

Creating a PHP webhook for DialogFlow (API.AI)

This is intended to be a very basic, beginner’s article for getting started with API.AI webhooks using PHP. I am making the following assumptions:

a. You have a website where you can run PHP scripts

b. You have sufficient permissions to be able to create new folders etc on the host without too much difficulty

Code sample

This tutorial is primarily based on the following code sample provided on the API.AI forum:

function processMessage($update) {
    if($update["result"]["action"] == "sayHello"){
            "source" => $update["result"]["source"],
            "speech" => "Hello from webhook",
            "displayText" => "Hello from webhook",
            "contextOut" => array()

function sendMessage($parameters) {
    echo json_encode($parameters);

$update_response = file_get_contents("php://input");
$update = json_decode($update_response, true);
if (isset($update["result"]["action"])) {

Note that I made the following changes to the code sample:

a. Stripped out the comments

b. Changed the action value from “buscar.nfe” to “sayHello”

c. Changed the speech and displayText values to “Hello from webhook”

Create test.php

To use the above code, start with a file called test.php. Paste the entirety of the code into test.php and save it. Place the file inside a folder called webhook (for example).

Now upload the webhook folder to your website. I will assume, for this example, that you are uploading the folder to the root of your website. If your website is called example.com, then the php file will be located at example.com/webhook/test.php

Add the webhook URL

Under the Fulfillment tab in your API.AI console, add the following URL:


Make sure you use https instead of http if your site already is https.

Add an action to your intent

Choose an intent that you wish to trigger (let us use the Default Welcome for now). Under the action, you should add the following: sayHello. It should match whatever you are handling in the PHP code.

Trigger the intent

In the API.AI console, type “Hi”. It should trigger the Default Welcome Intent and you should see “Hello from webhook” appearing on your console.

<— End of article —>

This website contains affiliate links. See the disclosure page for more details. 
"The magic key I needed as a non-programmer"

The custom payload generator was the magic key I needed (as a non-programmer) to build a good demo with rich responses in DialogFlow Messenger. I've only used it for 30 minutes and am thrilled. I've spent hours trying to figure out some of the intricacies of DialogFlow on my own. Over and over, I kept coming back to Aravind's tutorials available on-line. I trust the other functionalities I learn to use in the app will save me additional time and heartburn.

- Kathleen R
Cofounder, gathrHealth
"Much clearer than the official documentation to be honest"

Thanks a lot for the advice (of buying and following your videos)! They helped a lot indeed. Everything is very clear when you explain, much clearer than the official documentation to be honest 🙂

Neuraz T
Review for Learn Dialogflow CX
"I will strongly recommend this course because even I can learn how to design chatbot (no programming background)"

I think Aravind really did a great job to introduce dialogflow to people like me, without programming background. He organizes his course in very clear manner since I have been a college professor for 20 years. It is very easy for me to recognize how great Aravind’s course is! Very use-friend and very easy to follow. He doesn’t have any strong accent when he gives the lectures. It is so easy for me to understand. Really appreciate it.

Yes, I will strongly recommend this course because even I can learn how to design chatbot (no programming background) after studying Avarind’s course, you definitely can!

Ann Cai
Review for Learn Dialogflow ES

Similar Posts

  • Webhook call failed. Error: Failed to parse webhook JSON response: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $.

    What is this error,sir??

  • Hello,

    I followed your excellent tutorial
    es it is code example test.php still working at the current time?

    it does not work for me

    console = Not available
    Diagnostic info-> FULFILLMENT STATUS=
    Webhook call failed. Error: Failed to parse webhook JSON response: Expect message object but got: null.

    (sorry for the translate online )
    Please ?

    • This particular sample is for Dialogflow API v1. I will update it for API v2 over the next week or so – but it is a fairly simple change and you should be able to do it yourself once you look into the JSON formats for v1 and v2 and understand the difference between them.

  • Hello,
    Could you also create a tutorial in which you create a php webhook but also connect to a database to query the response which the user sends from the chatbot on api.ai?

    But, I must say all your tutorials are really good.
    Thank you for that.