Correct PHP JSON Setup for DialogFlow

I am working with DialogFlow and attempting to set up a JSON object in PHP for use with DialogFlow fulfillment. The process is explained on this page.
https://dialogflow.com/docs/reference/api-v2/rest/Shared.Types/WebhookResponse

My php script looks like this:

$myObj->fulfillmentText = "This is my test message";
$myObj->fulfillmentMessages = json_encode(array("text" => "text response")); 
$myJSON = json_encode($myObj);
echo $myJSON;
exit;

Open in new window


If I send only the first parameter, "fulfillmentText" it works fine because that is sent as a string.
But the second parameter, "fulfillmentMessages" is supposed to be sent as an object, but I'm not doing something right.  I get back this message from DialogFlow.


Fulfillment Text
{
  "fulfillmentText": "Searchby Choice is searchby|theme :: email| :: ",
  "fulfillmentMessages": "{\"text\":\"text response\"}"
}

Fullfillment Response (the error code)
Webhook call failed. Error: Failed to parse webhook JSON response: Expect an array but found: "{\"text\":\"text response\"}".

So I am clearly not setting up the array/object correctly in my php code to be recognized as an "array" in DialogFlow.  So how do I send the parameter "text" as an object array? What do I need to change in my PHP.

This reference page shows the correct setup for this option in dialogflow.
https://dialogflow.com/docs/reference/api-v2/rest/Shared.Types/Message#Text

Thanks in advance for the assistance.
Paul KonstanskiProject SpecialistAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David FavorLinux/LXD/WordPress/Hosting SavantCommented:
Provide your actual/full code. The code above is only your test code version.

Provide the version where you set fulfillmentText + fulfillmentMessages (or that's what it sounds like your code is doing).

Although... seems like fulfillmentMessages will be returned from the call, rather than passed through the call.
Paul KonstanskiProject SpecialistAuthor Commented:
Providing additional code will only confuse the issue. The rest of my code works just fine.  The issue is with how to set up a JSON response for an object to be read correctly within DialogFlow.  

DialogFlow wants the JSON to be represented this way:
{
  "fulfillmentText": string,
  "fulfillmentMessages": [
    {
      object(Message)
    }
  ]
}

Open in new window


So when I just send the first part of the JSON object that is to be sent as a string, it works fine.
	$myObj->fulfillmentText = $msg;
	
	$myJSON = json_encode($myObj);
	echo $myJSON;
	exit;

Open in new window


But when I try to send the second part that is supposed to be sent as an object, it fails.

	$myObj->fulfillmentText = $msg;
	$myObj->fulfillmentMessages = json_encode(array("text" => "text response")); 
	
	$myJSON = json_encode($myObj);
	echo $myJSON;
	exit;

Open in new window


So my problem is how I am setting up the "object" to be sent to dialog flow.  It is this part of my code that is not set up correctly.
myObj->fulfillmentMessages = json_encode(array("text" => "text response")); 

Open in new window


So the core question how do I use PHP to end the correct format as indicated in this image below:

The part of the JSON code I need help setting up in PHP
hieloCommented:
Instead of:
$myObj->fulfillmentMessages = json_encode(array("text" => "text response"));

try:
$myObj->fulfillmentMessages = [ array("text" => "text response") ];
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Paul KonstanskiProject SpecialistAuthor Commented:
That gives me an error message of:

Webhook call failed. Error: Failed to parse webhook JSON response: Expect message object but got: "text response".
hieloCommented:
How about:
$myObj->fulfillmentMessages = [ json_encode(array("text" => "text response")) ];
Paul KonstanskiProject SpecialistAuthor Commented:
In a dialog with another programmer, I found this solution:

	$myObj->fulfillmentText = "This is my test message";
	$myObj->fulfillmentMessages = array(
		array(
			"text" => array(
			"text" => array("text response")

			) 
		)
	);
	$myJSON = json_encode($myObj);
	echo $myJSON;
	exit;

Open in new window

hieloCommented:
Great.  Thanks for posting back.  You can accept your comment as the solution.
Paul KonstanskiProject SpecialistAuthor Commented:
The most recent comment by hielo also gave an error:

Webhook call failed. Error: Failed to parse webhook JSON response: Expect message object but got: "{\"text\":\"text response\"}".

The solution I offered in previous comment works...
Paul KonstanskiProject SpecialistAuthor Commented:
This is the solution that worked:

	$myObj->fulfillmentText = "This is my test message";
	$myObj->fulfillmentMessages = array(
		array(
			"text" => array(
			"text" => array("text response")

			) 
		)
	);
	$myJSON = json_encode($myObj);
	echo $myJSON;
	exit;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Paul KonstanskiProject SpecialistAuthor Commented:
Thank you all for your comments and questions. It helped me arrive at the solution.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.