views:

67

answers:

3

I have a form allowing a user to signup for a news letter which submits back to the page it's sat in for validation and adding the content to the db, however I also need to send an xml file to a third part using the information collected from the form to add to a mailing list. The data sent to the third party seems to need to be sent using the post method.

What's the best way of doing this?

I tried ajax, but realised after a bit that ajax isn't able to send info to external links so abandoned that.

Essentially the site needs to reload the page, validate the info sent to it, either return errors or add info the db and fire off the xml in the background, so having it send a separate form after reload isn't ideal either. Also the third party page when sent the xml through the main form loads it's own page, which is far from pretty and takes the user away from our site, not good at all.

Any help will be greatly appreciated

+1  A: 

How about the XML is sent not by your user's browser, but generated and sent by your server? You could still use AJAX, and you'd have no headaches about users leaving your site.

Something along the lines of

Browser -> Server

Server -> write into own DB Server -> generate an XML file and send it to the foreign server

Martin Hohenberg
could you explain a little more. I'd call myself an intermediate when it comes to coding of this sort (possibly a bit lower).How would I go about telling my server to generate and send the xml?would ajax not still refuse to send to a different server?
andy-score
No, as your AJAX-enabled client-machine wouldn't have to send the server-generated XML - your server would connect the different server directly.
Martin Hohenberg
+1  A: 

You will have to validate in PHP and then send the XML from the

<?php
   $hCurl = curl_init();
   curl_setopt($hCurl, CURLOPT_PUT,            true);
   curl_setopt($hCurl, CURLOPT_HEADER,         true);
   curl_setopt($hCurl, CURLOPT_RETURNTRANSFER, true);
   curl_setopt($hCurl, CURLOPT_CONNECTTIMEOUT, 60);
   curl_setopt($hCurl, CURLOPT_URL,            $URL_TO_UPLOAD);
   curl_setopt($hCurl, CURLOPT_HTTPHEADER,     $aCurlHeaders);
   // TODO it could be possible that fopen() would return an invalid handle or not work        altogether.  Should handle that
   $fp = fopen ($XML_FILE, "r");
   curl_setopt($hCurl, CURLOPT_INFILE,         $fp);
   curl_setopt($hCurl, CURLOPT_INFILESIZE,     $finfo['size']);
   $sResp = curl_exec($hCurl);
?>

Just replace $URL_TO_UPLOAD with your server that you want to POST to and $XML_FILE with the file you want to send and we are done!

Pasta
sounds like what I need, couple of problems I'm having, and having not used curl before, I'm failing to know what to do.Firstly, $aCurlHeaders I assume has been set somewhere in your code, what needs to go in here?Secondly, if I wanted to send xml which was generated in xml and stored in a variable, how would I do that, rather than create an xml file, send it, then delete it, this is for a newsletter signup form, so to keep making and deleting a file would be annoying.
andy-score
$aCurlHeaders[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg'; $aCurlHeaders[] = 'Connection: Keep-Alive'; $aCurlHeaders[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8'; I leave the file creation/deleting process with you.
Pasta
Thanks for your help. I'd push your answer up, but I'm too much of a lowly newb to do that.As I realised that I needed to use the post method for the code I made some changes to your code to make it work to my needs so that it fires the information off via post.Everything's now working and I'm a happy bunny, so thanks.Curl is pretty cool.
andy-score
Cool.. Happy to help..
Pasta
+1  A: 

I would recommend getting your server to submit the data to the third party once it has added the information to the database. It can even queue up this process and deal with it at a later date if needed.

There are lots of ways of doing this in PHP, such as Curl.

rikh