[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to POST and read response from PHP

Posted on 2007-11-15
6
Medium Priority
?
3,936 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 2000 total points
ID: 20291412
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:Mark Gilbert
ID: 20291429
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
ID: 20291487
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 18

Expert Comment

by:Mark Gilbert
ID: 20291514
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
ID: 20291571
$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
ID: 31409394
Thank you very much! worked like a charm :)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month19 days, 16 hours left to enroll

873 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