Forum Moderators: coopster

Message Too Old, No Replies

XML Form Post to External URL

How do I send XML form data to an external URL using PHP?

         

JNap

2:57 pm on Mar 18, 2007 (gmt 0)

10+ Year Member



I'm far enough along in PHP to know how to create input forms, grab the form input as $_POST variables, do some basic validation checks and present the errors to the user for resubmit if needed. For the life of me I can't understand why I can't post data as an XML document to one of my advertisers.

For example, a lead form is complete with Name, Address, Phone, and so on. I capture the form data and submit as an XML document. This particular advertiser will only accept data as an "External XML Post." I tried using Javascript and PHP with cURL, but I'm going in circles trying snippets of other people's code -- it's been a week. At this point all I want to do is assume IE7 (nothing else!) and no data checks, just "here's the XML, here's the external URL" and show me that HTTP POST actually works. Right now I'm thinking it's a bunch of junk :-) Someone please help.

Here's my server info:

PHP Verion 4.4.4
Apache/2.0.46 (Red Hat)
CURL support enabled
CURL Information libcurl/7.15.4
DOM/XML enabled
DOM/XML API Version 20020815
libxml Version 20510
HTML Support enabled
XPath Support enabled
XPointer Support enabled
XML Support active
XML Namespace Support active

Here's a sample of XML, let's call it "data.xml"

<?xml version="1.0" encoding="UTF-8"?>
<DataFormSubmit>
<individual id="12345">
<FirstName></FirstName>
<LastName></LastName>
<Address></Address>
<Phone></Phone>
</individual>
</DataFormSubmit>

POST XML document to URL on an external website for example:
http://www.example.com/othersite/xml.aspx

Response XML is equally simple. There is no mention of MethodCall, MethodResponse and Params like the books say, so please don't use those elements in your reply to me. There is mention of field requirements like "Text field" "255 character max" "Numeric" but I think this can be handled by the input validation scripts. I'm just trying to POST and get a response.

I've received errors refering to "permission denied" "access denied" "object expected" but at this point I've edited my code so many times I don't know what's right or wrong any more. Here's one of my failed cURL examples:

<?php
// XML data as string
$request = '<?xml version="1.0" encoding="UTF-8"?>';
$request .= '<DataFormSubmit>';
$request .= '<individual id="12345">';
$request .= '<FirstName></FirstName>';
$request .= '<LastName></LastName>';
$request .= '<Address></Address>';
$request .= '<Phone></Phone>';
$request .= '</individual>';
$request .= '</DataFormSubmit>';

// Create Headers
$header[] = "Host: www.example.com";
$header[] = "Content-type: text/xml";
$header[] = "Content-length: ".strlen($request) . "\r\n";
$header[] = $request;

// Send using CURL
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, "http://www.example.com/othersite/xml.aspx"); // URL to post
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); // return into a variable
curl_setopt( $ch, CURLOPT_HTTPHEADER, $header ); // headers from above
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'POST' ); // special POST with specified Content-type
$result = curl_exec( $ch ); // runs the post
curl_close($ch);

echo $result; // echo reply response
?>

And I tried a Javascript variations using code like
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP") but I'll save that for a followup post since this is getting long.

Anyone out there doing external XML form post?

[edited by: dreamcatcher at 3:10 pm (utc) on Mar. 18, 2007]
[edit reason] Use example.com, thanks. [/edit]

coopster

11:26 pm on Mar 19, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, JNap.

Setup your own "receiving" script first, instead of posting to the *live* destination. In your receiving script, read in the data and send back your own response as per specs you expect to be receiving.

JNap

2:52 pm on Mar 21, 2007 (gmt 0)

10+ Year Member



Hi coopster, thanks for the reply and the welcome.

I think you're saying post the form data variables to a script on another internal web page, then "HTTP Post" the XML to the external destination?

I've tried something similar and I'm all for that. At this time I'm just not sure what the "HTTP Post" script should look like: cURL, Ajax, does anyone have a script that works on hand?

I don't know if IE7 is blocking my HTTP Post or if its my bad code, or if it's my server set up. I'm looking for a simple script that either HTTP Posts or Posts any way to the live destination. No data checks (I'll deal with that later), just the header elements and "POST" function in a working script.

Assume the XML data is ready on it's own page, data.xml for example, how do I send it to the destination without running into security blocks in IE7 and my server?

If it's not a simple answer and someone knows someone who can work with me fee based let me know. I can't imagine it's a difficult thing, just need to find someone who does this sort of thing regularly.

Thanks.

coopster

3:10 pm on Mar 21, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member




I think you're saying post the form data variables to a script on another internal web page

Yes.


, then "HTTP Post" the XML to the external destination?

No. ;)

I mean, simply change this line to a faked up page on your own server, like this:
Old line:

//curl_setopt( $ch, CURLOPT_URL, "http://www.example.com/othersite/xml.aspx"); // URL to post

New line:
curl_setopt( $ch, CURLOPT_URL, "http://www.example.com/path/on/my/site/test.php"); // URL to post

Then in test.php you could simple spit out the POST data, which your curl process will read and return to you in the originating script:

test.php

<pre> 
<?php print_r($_POST);?>
</pre>

You can then tweak your curl code until you have it where you need it.

JNap

11:16 am on Mar 28, 2007 (gmt 0)

10+ Year Member



Hi coopster,

I'll post an update soon, been a busy week...

JNap

11:52 am on Mar 30, 2007 (gmt 0)

10+ Year Member



Hi coopster,

I sent you a stickymail, let me know if you didn't get it. I don't see my outbox copy. Please stickymail me with another address if available.

I will send full code (it's not a lot). Still doesn't work for me.

coopster

1:56 pm on Mar 31, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Stickymail takes us out of the public discussion area which we don't like to do here. We just like to keep any and all discussion in the forum where future readers can benefit.

Posting the data on an internal web page first will allow you to make sure you have all your headers and body data correct before you even think of sending it off over the pipe to the receiver. Once you have everything nailed down, you can change the url to the receiver as opposed to you internal site.

I noticed that I mentioned to change the curl_setopt parameter to reflect that change but forgot to let you know you need to change your header "Host" as well. Don't forget to change this line to your internal server too during testing!

// Create Headers 
$header[] = "Host: www.example.com";