Page 1 of 1
cURLing an XML Post
Posted: Tue Feb 05, 2008 12:35 am
by ngotb
I have been beating my head on this for a while, and lived at borders for a couple of days trying to figure this out. I have a form that a user fills out and then I wrap the data in xml tags. That data is then in turn posted to a remote site. Heres my cURL code
Code: Select all
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"removed!");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "$xmlstring");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$my_curl_results = curl_exec($ch);
echo $my_curl_results;
curl_close($ch);
unset($ch);;
I have tried several variations and reviewed several books looking for the answer. I must be close but something is missing. I want to post the XML data to a remote site and get the results back to the page. Anyone have any ideas? Do I need to set a header for the remote site, its already expecting XML data? Anyone familiar enough with this to help?The $xmlstring is the string I built right before this code, and it is well formed cause I can post that exact string with an HTML form to the test site and its works as expected. But thats not the requirments, that was merely a sanity check to make sure everything else was correct.
Re: cURLing an XML Post
Posted: Sat Feb 09, 2008 12:01 am
by ngotb
So am I just asking for something impossible here, and going down the wrong path? All I want to do is post some XML data to a remote server, and this cant be accomplished by using Javascript, and I'd rather not use Micro$tuff. Someone here has to have some kind of answer....
Thanks in advance,
NewGeekOnTheBlock....
Re: cURLing an XML Post
Posted: Sat Feb 09, 2008 12:22 am
by John Cartwright
I don't see anything specifically wrong, it should be as simple as you have put.
What is the output of
?
You might also take a look at
Code: Select all
curl_setopt('CURLOPT_VERBOSE', true);
curl_setopt('CURLOPT_STDERR', 'path/to/log/file');
to get some more information about whats going on
Re: cURLing an XML Post
Posted: Sat Feb 09, 2008 12:43 am
by ngotb
Thats outstanding direction you offer! I was wondering if the module supported any kind of logging. My Wrox Beginnig PHP5 book only supplies a bare essentials of the command and I have read several sites on the cURL subject. I will check it out. I can intuitively assess that var_dump($variable) will display the contents of the variable, but what is the difference in the curl_setopt(CURL_VERBOSE,true) vs false do? what should I expect to see here? any info is greatly appreciated.Im smart enough to get this far, but some real guidance would be GREAT! as to the purpose of the curl(curlopt_) commannds.
Re: cURLing an XML Post
Posted: Sat Feb 09, 2008 5:46 pm
by Benjamin
$xmlstring needs to be in the format:
XML=$xmlstring&VAR2=foo&VAR3=bar
You cannot just post data without assigning it a variable name.
Re: cURLing an XML Post
Posted: Mon Feb 11, 2008 11:40 pm
by ngotb
Here is part of my code:
Where Im almost finished wrapping the XML around the form data
Code: Select all
$xmlstring=$xmlstring."\t\t\t</modification>\n";
$xmlstring=$xmlstring."\t\t</item>\n";
$xmlstring=$xmlstring."\t</data>\n";
$xmlstring=$xmlstring."</packet>\n";
echo $xmlstring;
$ch = curl_init();
curl_setopt('CURLOPT_VERBOSE', true);
curl_setopt('CURLOPT_STDERR', '/var/log/log.php');
curl_setopt($ch, 'CURLOPT_URL','removed!');
curl_setopt($ch, 'CURLOPT_POST', 1);
curl_setopt($ch, 'CURLOPT_POSTFIELDS', $xmlstring);
curl_setopt($ch, 'CURLOPT_RETURNTRANSFER',1);
$my_curl_results = curl_exec($ch);
echo $my_curl_results
var_dump($my_curl_results)
curl_close($ch);
unset($ch);
?>
Re: cURLing an XML Post
Posted: Tue Feb 12, 2008 1:26 am
by Benjamin
echo your $xmlstring variable.
Re: cURLing an XML Post
Posted: Thu Feb 14, 2008 6:26 pm
by ngotb
My string is correctly formed. all lowercase, all closed....I can post that exact string through a form action but I cant wrap the form data in xml first. Any ideas?
Re: cURLing an XML Post
Posted: Thu Feb 14, 2008 7:59 pm
by Benjamin
Post the contents of what your sending to CURLOPT_POSTFIELDS
Re: cURLing an XML Post
Posted: Sat Feb 16, 2008 12:08 am
by ngotb
Code: Select all
<packet>
<header>
<username>TESTCUST</username>
<password>testing1234</password>
</header>
<data>
<item>
<item_id>20040114002</item_id>
<action>MOD</action>
<mod_action>ADD_NUMBER</mod_action>
<unit_id>367980</unit_id>
<modification>
<npa>847</npa>
<nxx>963</nxx>
</modification>
</item>
</data>
</packet>
If I paste this into a text area on a form with the correct action URL this goes through. No additional info is needed and no authentication. This is strictly a test site for my XML packet.
Scottayy| Please use and
when posting code in the forums.[/color]
Re: cURLing an XML Post
Posted: Sat Feb 16, 2008 12:27 am
by Benjamin
astions wrote:$xmlstring needs to be in the format:
XML=$xmlstring&VAR2=foo&VAR3=bar
You cannot just post data without assigning it a variable name.