Link to home
Start Free TrialLog in
Avatar of Michael_D
Michael_DFlag for Canada

asked on

How to POST and read response from PHP

Hi, I am trying to use chatter bot  (hosted on pandorabots.com) on my site.

but instead of using IFrame I want to access the API using PHP.
pandorabots provide API

here is quote from the site
=================== QUOTE ===========================
A client can interact with a Pandorabot by POST'ing to:

    http://www.pandorabots.com/pandora/talk-xml 

The form variables the client needs to POST are:

    * botid - see H.1 above.
    * input - what you want said to the bot.
    * custid - an ID to track the conversation with a particular customer. This variable is optional. If you don't send a value Pandorabots will return a custid attribute value in the <result> element of the returned XML. Use this in subsequent POST's to continue a conversation.

This will give a text/xml response. For example:

<result status="0" botid="c49b63239e34d1d5" custid="d2228e2eee12d255">
  <input>hello</input>
  <that>Hi there!</that>
</result>

The <input> and <that> elements are named after the corresponding AIML elements for bot input and last response. If there is an error, status will be non-zero and there will be a human readable <message> element included describing the error. For example:

<result status="1" custid="d2228e2eee12d255">
  <input>hello</input>
  <message>Missing botid</message>
</result>

Note that the values POST'd need to be form-urlencoded.
=================== END QUOTE ===========================

I have created html form that connect to my bot ans POSTs the input
<html>
<head>Test Bot</head>
<body>

<form action="http://www.pandorabots.com/pandora/talk-xml" method="post">
<INPUT id="botid" name="botid" type="hidden" value="e35f2c157e362d3e" />
<INPUT style="WIDTH: 160px; " type="text" size="21" id="input" name="input" value ="">
<INPUT id="Submit1" type="submit" value="Submit" name="Submit1">

</form>
</body>
</html>

Now I need to create one php file that will simulate chat i.e. capable to reseive the response from the bot and show it to user

Any ideas how to do it? I mostly need advice on how to capture the response - I will handle xml parsing and output by myself

Thank you
 

ASKER CERTIFIED SOLUTION
Avatar of steelseth12
steelseth12
Flag of Cyprus image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Change your form action to a php file, and then in your php put:

<?php echo $_POST['custid']; ?>

That should return the customer id to the screen from the form input.  You then have the basis to build up a script that grabs all the post values, and constructs an xml file and writes it to disk using the fopen() functions in php.  You are writing a simple text file, so the formatting would be completely open to your own interpretation.

Hope this helps.
Avatar of Michael_D

ASKER

Steelseth12,
Yes please, an example whould be great


ingwa,
I am on sending side of the pipe - I need to Post data and parse the output while staying on the same page
Yikes, that's what comes from not reading the question correctly...apologies.  If you were hosting the entire app yourself and then building your own data files then my example would work...but I would agree with steelseth that curl would be the route to do data handling, remote posting transparent to the user etc.  Good luck with your project.
$Request = "botid=".$_POST["botid"]."&input=".$_POST["input"]."&Submit=Submit1";

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://www.pandorabots.com/pandora/talk-xml");

curl_setopt( $ch, CURLOPT_POST, TRUE );

curl_setopt( $ch, CURLOPT_POSTFIELDS, $Request );

curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE );

curl_setopt( $ch, CURLOPT_TIMEOUT, 240 );

$Response = curl_exec( $ch );


print $Response;


curl_close($ch);
Thank you very much! worked like a charm :)