cURLing an XML Post

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ngotb
Forum Newbie
Posts: 6
Joined: Tue Feb 05, 2008 12:33 am

cURLing an XML Post

Post 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.
ngotb
Forum Newbie
Posts: 6
Joined: Tue Feb 05, 2008 12:33 am

Re: cURLing an XML Post

Post 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....
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: cURLing an XML Post

Post by John Cartwright »

I don't see anything specifically wrong, it should be as simple as you have put.

What is the output of

Code: Select all

var_dump($my_curl_results);
?

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
ngotb
Forum Newbie
Posts: 6
Joined: Tue Feb 05, 2008 12:33 am

Re: cURLing an XML Post

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: cURLing an XML Post

Post 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.
ngotb
Forum Newbie
Posts: 6
Joined: Tue Feb 05, 2008 12:33 am

Re: cURLing an XML Post

Post 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);
?>
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: cURLing an XML Post

Post by Benjamin »

echo your $xmlstring variable.
ngotb
Forum Newbie
Posts: 6
Joined: Tue Feb 05, 2008 12:33 am

Re: cURLing an XML Post

Post 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?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: cURLing an XML Post

Post by Benjamin »

Post the contents of what your sending to CURLOPT_POSTFIELDS
ngotb
Forum Newbie
Posts: 6
Joined: Tue Feb 05, 2008 12:33 am

Re: cURLing an XML Post

Post 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]
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: cURLing an XML Post

Post 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.
Post Reply