ScotLUG:Programming

From ScotLUG

Jump to: navigation, search

The Problem: I need users to reigster on one site and have their details used to create an account and also on another server which I don't control (but have permission to allow others to use)

Server#2 runs a non-OSS stock ordering app. "our" users get an agreed discount rate

Solution

Not sure if you've already solved this, but I can't sleep and might as well pass my time semi-constructively. This is off the top of my head, there might be some syntax errors and it needs validation, but something along these lines will get you a bit further:

<?
if(isset($_POST['submitvariable'])) {
  // do whatever you are doing with your post fields and database..

  // fill the values the remote form needs (from your $_POST variable or if they need to be hardcoded)
  $curlPostValues = "meaningfulVarName1=".$_POST['meaningfulVarName1']."&var2=".$_POST['var2']."&submitVar=1;

  // start and set some of curl's options:
  $c = curl_init();
  curl_setopt($c, CURLOPT_URL, "http://theremoteserver/form.php");
  curl_setopt($c, CURLOPT_HEADER, 1);
  curl_setopt($c, CURL_RETURNTRANSFER, 1);
  curl_setopt($c, CURLOPT_POST, 1);
  curl_setopt($c, CURLOPT_POSTFIELDS, $curlPostValues); // populated above
  $sendform = curl_exec();
  curl_close($c);
}
?>

Hope that helps, and of course, if there are any problems give me a shout in #scotlug — if you need to get more complicated my rates aren't too expensive :-P As always, the php.net documentation on this subject is not bad: http://php.net/curl

Neilmckillop 03:26, 12 March 2007 (GMT)

22:05 < willie> thinker: PHP guru: ol'buddy ......I have a form which when poulated and validated
                has its values passed off to mysql and a new record is created - I also want to
                save a subset of these form inputs and submit them to a totally different form on a
                different site.  Which php functions are best suited?

22:06 < thinker> willie: it's not really a php job
22:07 < willie> well my first thought was to have 2 form actions but gjcp told me that uthere could
                only be one
22:08 < Elwell> willie: client side javascript - go on, you know you want to open up a pile of
                vulns to x-site scripting and other abuse :-)

22:10 < thinker> willie: are you in charge of the server that the first form is being posted to?
22:11 -!- alunt2003 [n=alunt200@87.127.125.113] has joined #scotlug
22:12 < willie> yes but not the second
22:12 < willie> for loose values of "in charge"

22:12 < willie> but yes I can put code on server#1 but not #2
22:12 < thinker> if so, in the same script as you make the db insert, you could use file() if it is
                 expecting a get, or,  curl to post
22:13 < thinker> the posting is more complicated
22:13 < willie> server#2 knows and approves of what we are up to btw
22:13 < willie> in principle anyway

22:16 < willie> #php seem to think that having two <FORM method=post action=   lines is OK
22:16 < willie> which keeps it out of PHP
22:19 < Elwell> willie: that gives you 2 forms surely that you then need 2 submits too
23:34 < bigkevmcd> willie: the gist of it is that you need to use curl and encode the POST payload
                   correctly
23:34 < dansheare2> Ok lads back again. Wiki probs?
23:35 < bigkevmcd> willie: I use the technique in my Protx payment module
23:35 < thinker> [22:12:40] <thinker> if so, in the same script as you make the db insert, you
                 could use file() if it is expecting a get, or,  curl to post
Personal tools