Form submission (no button) using PHP+cURL+ transfer control to other site

Discussion in 'PHP' started by jsherk, Jul 13, 2007.

  1. #1
    I've spent HOURS searching the net for answer to this, but can't find it...

    I'm trying to send data using the POST method and also have control of the browser be turned over to the website that's receiving the POST data (note that this is NOT the same as sending the data via POST method and then redirecting to the website after). I need it to work exactly like hitting the submit button on an html form, and I need it to work from PHP. It needs to POST the data to the site and also let the other site have control at the same time.

    I've tried a couple of methods using both cURL and an fsocket function, but it seems like in both cases that (1) the data gets sent to the other site first, (2) then my program gets a response back, (3) and then it redirects to the other site afterwards. The data gets sort of POSTed to the other site (I get a 200 response back, and I get info showing it was posted correctly), but since the other site does not get control, it seems to dump (not save) the posted data.

    Here's my cURL example:
    <?php
        $user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
        $url = 'https://www.xyz.com/xyz.exe/xyz-AddItem';
        $part1 = 'SB123';
        $item1 = 'blue socks';
        $qty1 = '1';
        $price1 = 5;
        $dat = "PartNo=".$part1."&Item=".$item1."&Qty=".$qty1."&price=".$price1."&";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST,1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,$dat);
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  1);
        curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
        $result=curl_exec ($ch);
        curl_close ($ch);
        echo("Results: <br>".$result);
    ?>
    Code (php):
    Somehow I think it needs to redirect (hand over control) exactly when the curl_exec function is called, because this is when the data would be POSTed.

    Is there a way to terminate my php program AND hand the browser over to this other site AND pass the data over using the POST method, all at the exact same time?

    I tried:
    header ("Location: https://www.othersite.com);
    exit();
    Code (php):
    But this also seems to POST the data first, and then redirects to the other site after, which has the same effect of my POSTed data being dumped.

    Any help would be appreciated.
    Thanks
     
    jsherk, Jul 13, 2007 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    As far as I have ever found, there is no way to redirect a user with post variables using php. On a few incidences I resorted to using javascript to do an onload submit.
     
    jestep, Jul 13, 2007 IP
  3. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #3
    If you explain to us exactly what you are trying to do and why you can't use the submit button than we might be able to be of more help.
     
    MMJ, Jul 13, 2007 IP
  4. jsherk

    jsherk Guest

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I have a form which submits the data using the POST method to our shopping cart on another website (a third party site: www.xyz.com).

    We decided we wanted to store the information from the form to a database first, before submitting the data to the other shopping cart site .

    I changed the action from 'https://www.xyz.com/xyz.exe/xyz-AddItem' to "myprocess.php", and I succeeded in writing the PHP code that stores the info in the DB, but the problem I'm having is when I try to hand over the form data to the other site (which will only accept POST method).

    In the example above, I get an appropriate response from the "other site" server, and I can display the new webpage from their site, BUT the browser address bar shows that I am still on my site. This causes a problem, because although it appears that the items were added to the shopping cart, when you return to view the cart the items are no longer there.

    If you do a redirect after you post the data, then beacuse it already posted the data and the server already returned a response, the redirect takes you to a page that says you did not post any data.

    It's almost like I need to do the redirect FIRST, and then send the POST data with it or after it.

    Hope somebody can help!!!
     
    jsherk, Jul 19, 2007 IP
  5. Greg Carnegie

    Greg Carnegie Peon

    Messages:
    385
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You are doing it wrong way. If you first use PHP+cURL to send variables and then use redirection, it is like opening two completely different sessions on website with shopping cart.

    The easiets way to do this is:
    
    header("Location: http://redirect.com?var1=x&var2=y");
    
    PHP:
    You will have your variables in $_GET, you can also try setting POST headers and send variables as POST.

    Another solution is to send form directly to shopping cart, but add in form onsubmit event and using ajax you can verify and save data before it is sent to shopping cart.
     
    Greg Carnegie, Jul 19, 2007 IP
  6. bloodredxxx

    bloodredxxx Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I'm not sure if I understand it correctly but you can use an intermediate script that will save POSTed data in your DB, then have the page generate a form with the POSTed data as hidden inputs then automatically resubmit itself using <BODY onload="document.form.submit();"> or some other JavaScript trigger.

    The problem with this solution is that there is a small chance that the data can be saved in your DB but not POSTed to the other server.
     
    bloodredxxx, Jul 25, 2007 IP
  7. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #7
    FOLLOW_LOCATION for cUrl will allow the page after you 'POST' the variables to the information. Also, it would help if you logged the cookies. Some websites will not work if you don't have cookies enabled at all.
     
    exodus, Jul 25, 2007 IP
  8. chirayu

    chirayu Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Hi all,

    I am looking for working auto form fill up script. Can you help me out. If you can provide me, contact me at .

    Thanks a lot in advance.
     
    chirayu, Jun 5, 2008 IP