开发者

send xml to external site in background

开发者 https://www.devze.com 2022-12-11 16:35 出处:网络
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 thir

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.

How can I achieve 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 aft开发者_JS百科er 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.


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!


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号