Solved

How to POST and read response from PHP

Posted on 2007-11-15
6
3,921 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
syntax error, unexpected '?' in phpunit 5 44
how to use jquery variable in mysql query 20 69
php output utf-8 problem 6 45
Form submit takes only for one form 23 45
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

736 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