Solved

How to POST and read response from PHP

Posted on 2007-11-15
6
3,917 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
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

856 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