Solved

How to POST and read response from PHP

Posted on 2007-11-15
6
3,909 Views
Last Modified: 2012-08-14
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
 

0
Comment
Question by:Michael_D
  • 2
  • 2
  • 2
6 Comments
 
LVL 20

Accepted Solution

by:
steelseth12 earned 500 total points
Comment Utility
You will use CURL to send the form to the bot.
Have a look at http://www.php.net/manual/en/ref.curl.php
Let me know if you need an example.
0
 
LVL 18

Expert Comment

by:ingwa
Comment Utility
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.
0
 
LVL 13

Author Comment

by:Michael_D
Comment Utility
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
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 18

Expert Comment

by:ingwa
Comment Utility
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.
0
 
LVL 20

Expert Comment

by:steelseth12
Comment Utility
$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);
0
 
LVL 13

Author Closing Comment

by:Michael_D
Comment Utility
Thank you very much! worked like a charm :)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now