dosser
asked on
Using PHP Curl to post data to Java Servlet
Hi,
I've been toying around with something over the last few days but can't quite get it to work. Wondering if anyone can help me.
I have a Java servlet running on Tomcat (this is all on my local machine for testing purposes). This servlet accepts one variable (a block of XML) and returns another block of XML as a result. The servlet is running fine (I can test it through my browser and even POST successfully to it from a PHP form receiving the correct XML block as result). What I want to be able to do post the data and access the result with curl.
Here is my current code -
$xml='<result><name>bob</n ame></resu lt>';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost:8080/myServer/TestServlet" );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt ($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$postResult = curl_exec($ch);
curl_close($ch);
$xmlResult = file_get_contents($postRes ult);
var_dump($xmlResult);
I want to be able to read $xmlResult. Everytime I try to run this, I get an error message that the posted data is empty. What am I doing wrong? Is there an alternative to curl that I could be using?
Thanks in advance
I've been toying around with something over the last few days but can't quite get it to work. Wondering if anyone can help me.
I have a Java servlet running on Tomcat (this is all on my local machine for testing purposes). This servlet accepts one variable (a block of XML) and returns another block of XML as a result. The servlet is running fine (I can test it through my browser and even POST successfully to it from a PHP form receiving the correct XML block as result). What I want to be able to do post the data and access the result with curl.
Here is my current code -
$xml='<result><name>bob</n
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost:8080/myServer/TestServlet" );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt ($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$postResult = curl_exec($ch);
curl_close($ch);
$xmlResult = file_get_contents($postRes
var_dump($xmlResult);
I want to be able to read $xmlResult. Everytime I try to run this, I get an error message that the posted data is empty. What am I doing wrong? Is there an alternative to curl that I could be using?
Thanks in advance
Here is what the bounce post script does.
<?php // RAY_bounce_post.php
error_reporting(E_ALL);
// START A BUFFER TO CAPTURE THE BROWSER OUTPUT
ob_start();
// SHOW WHAT WAS RECEIVED IN POST
var_dump($_POST);
// CAPTURE THE BUFFER
$posted_data = ob_get_clean();
// SAY THANK YOU
echo "<pre>";
echo "THANK YOU " . date('c');
echo PHP_EOL;
echo $posted_data;
ASKER
The problem is that I need to post a block of XML (so can't really fill the array with key/value pairs). Or can you really use the string e.g http://localhost:8080/myServer/TestServlet?queryxml=<result><name>bob</name></ result>
I only gave a simple example in the original post but the XML block I actually need to send is quite complicated.
I only gave a simple example in the original post but the XML block I actually need to send is quite complicated.
How do you manually send request to server? Post an example.
ASKER
I use a php page with a textarea. I input the XML in the textarea, click submit, and the correct XML result block is returned -
<?php
echo'
<form method="post" action="http://localhost:8080/myServer/TestServlet">
<textarea name="inputXml"></textarea >
<input type="submit">
</form> ';
?>
Should I be using a key/value pair of inputXml -> $xml (where $xml is a huge block of the XML to be posted)? Or will that not work?
<?php
echo'
<form method="post" action="http://localhost:8080/myServer/TestServlet">
<textarea name="inputXml"></textarea
<input type="submit">
</form> ';
?>
Should I be using a key/value pair of inputXml -> $xml (where $xml is a huge block of the XML to be posted)? Or will that not work?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Dal. A combo of that and urlencode did the job.
Sorry, but I had to go out to my club for lunch so I couldn't follow this Q until now.
Regarding this, "so can't really fill the array with key/value pairs..."
That's not much of an issue. Consider this string:
inputXml=<result>...</resu lt>
With a little creativity you might find that this array would be right:
$postarray = array("inputXml" => "<result>...</result>");
Or with a little more creativity, you could just bypass the code that converts the array to a raw post string. The examples I post here are just that -- examples, not meant to represent final code, just a standard practices example of a solution. The main reason that an array is present in the example is because most people understand COOKIE, GET and POST arguments in terms of an associative array.
Regarding this, "so can't really fill the array with key/value pairs..."
That's not much of an issue. Consider this string:
inputXml=<result>...</resu
With a little creativity you might find that this array would be right:
$postarray = array("inputXml" => "<result>...</result>");
Or with a little more creativity, you could just bypass the code that converts the array to a raw post string. The examples I post here are just that -- examples, not meant to represent final code, just a standard practices example of a solution. The main reason that an array is present in the example is because most people understand COOKIE, GET and POST arguments in terms of an associative array.
Open in new window