Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Mar 2004
    Posts
    95
    Thanks
    8
    Thanked 0 Times in 0 Posts

    post hidden variables to another site without submitting?

    I have a bit of a weird problem. I have a form on a page that posts to itself. There are 3 radio button options on the form. With 2 of the options the form is handled by itself, updates the database, sends some emails and finishes. But if the 3rd option is selected I need to "post" a bunch of hidden variables to another website completely. Is there some way to post a form from inside my "submit" code? Or maybe I can change the place the form is posted to when the radio button is selected and have the hidden variables set? Any ideas? Thanks in advance.

  • #2
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    you can use curl, it lets you put together the bits you need to send, and then it does the post request for you.

    http://php.net/curl

  • #3
    Senior Coder
    Join Date
    Nov 2002
    Location
    North-East, UK
    Posts
    1,265
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You could also use fsockopen
    http://uk2.php.net/fsockopen

    PHP Code:
    <?php
    $httpRequest 
    = Array();
    $errors = Array();
    // Create the HTTP Request
    $postContent =
    "intInstID=12345&intTransID=12345&strSecurityToken=12345&fltAmount=10.00&strDesc=Refund&fltAPIVersion=1.3&…
    …strTransType=REFUND"
    ;
    $httpRequest[] = "POST /subfolder/ HTTP/1.0";
    $httpRequest[] = "Host: secure.domain.com";
    $httpRequest[] = "Content-Type: application/x-www-form-urlencoded";
    $httpRequest[] = "Content-Length: "strlen($postContent) ;
    $httpRequest[] = "Connection: Close";
    $httpRequest[] = "";
    $httpRequest[] = $postContent;
    // Open socket connection
    // Send HTTP Request
    // Read HTTP Response
    $httpResponse "";
    $secureSocket fsockopen("ssl://secure.domain.com",443,$errno$errstr3);
    if ( !
    $secureSocket || ! is_resource($secureSocket) ) {
    $errors[] = "Could not establish connection [$errno : $errstr]";
    } elseif( 
    fwrite$secureSocket join("\n",$httpRequest) ) ) {
    while (!
    feof($secureSocket)){
    $httpResponse .= fgets($secureSocket,128);
    }
    fclose($secureSocket);
    } else {
    $errors[] = "Could not write to secure connection";
    }
    ?>

  • #4
    New Coder
    Join Date
    Mar 2004
    Posts
    95
    Thanks
    8
    Thanked 0 Times in 0 Posts
    I have been trying the fsockopen suggestion. I think I must be missing something. I got this example from the link degsy posted.
    Code:
    If you want to post $_POST vars and (in this case) one file named userfile to $remote_server and $remote_url.
    
    <?php
           // get the necessary data
           $file_name = $_FILES['userfile']['name'];    // the file
           $tmp_name = $_FILES['userfile']['tmp_name'];    // the file
           $content_type = $_FILES['userfile']['type'];    // the file mime type
          
           srand((double)microtime()*1000000);
           $boundary = "---------------------".substr(md5(rand(0,32000)),0,10);
          
           // Build the header
           $header = "POST $remote_url HTTP/1.0\r\n";
           $header .= "Host: $remote_server\r\n";
           $header .= "Content-type: multipart/form-data, boundary=$boundary\r\n";
           // attach post vars
           foreach($_POST AS $index => $value){
               $data .="--$boundary\r\n";
               $data .= "Content-Disposition: form-data; name=\"".$index."\"\r\n";
               $data .= "\r\n".$value."\r\n";
               $data .="--$boundary\r\n";
           }
           // and attach the file
           $data .= "--$boundary\r\n";
           $content_file = join("", file($tmp_name));
           $data .="Content-Disposition: form-data; name=\"userfile\"; filename=\"$file_name\"\r\n";
           $data .= "Content-Type: $content_type\r\n\r\n";
           $data .= "".$content_file."\r\n";
           $data .="--$boundary--\r\n";
           $header .= "Content-length: " . strlen($data) . "\r\n\r\n";
                     // Open the connection
           $fp = fsockopen($remote_server, 80);
           // then just
           fputs($fp, $header.$data);
           fclose($fp);
    ?>
    So I have set the $remote_server, $remote_url and added some variables to post to $data. When the code executes I think it is posting the variables to the test.php page I am using, but I need it to open that page so it can show me the variables. The code runs without errors, but doesn't open the page I have posted to. What have I missed?
    Last edited by Taipan; 05-03-2006 at 10:21 PM.

  • #5
    New Coder
    Join Date
    Mar 2004
    Posts
    95
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Tis cool, I got curl working for me

  • #6
    New Coder
    Join Date
    Apr 2006
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, good for you. I was going to offer you a fix for your fsockopen code

  • #7
    Senior Coder
    Join Date
    Nov 2002
    Location
    North-East, UK
    Posts
    1,265
    Thanks
    0
    Thanked 0 Times in 0 Posts
    For future reference. This kind of coding doesn't open the page. It sends a request and recieves a response.

  • #8
    New Coder
    Join Date
    Mar 2004
    Posts
    95
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by degsy
    For future reference. This kind of coding doesn't open the page. It sends a request and recieves a response.
    Thanks for that. I should have explained myself better. I was trying to send shopping cart variables to an external payment form, so I need to send the variables and open the page. But I only need to do this if "credit card" was the selected payment method, hence the problem. Determine which method of payment and then go to the remote server if its for credit card.

    With curl I used this line:-
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); which opens the page. I have tried it on a test page I made, and it seems to work OK. Thanks again.

  • #9
    New to the CF scene
    Join Date
    Aug 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    I'm looking for a similar solution

    ...unfortunately my client's server doesn't have curl, so I can't copy Taipan's solution.

    I have a similar function that POSTs data, which I use to send users to a credit card authorisation site (I want to do this without them pressing a 'submit' button on a form, because several things have to happen at that moment: an order is created in a local orders table, and the user is sent off to get authorisation from an external source).

    My problem is that, while my function sends the POST variables ok, and returns the resulting page into an array, it doesn't actually forward the browser to the remote site with those variables so that the can be processed together in the remote script.

    You'll see from the commented out line, that I tried a header("Location: [target url]"); - but that sends a seperate redirect, without the POSTed variables, so the user arrives at a script that responds 'error, no data sent'.

    Grr! Any solutions anyone? I've been surfing for hours, and I'm sure I'm really close! I suspect that I should be able to process the returned '$result' data somehow - it returns the whole resulting page that I was expecting, complete with what looks like headers a the top....

    Here's my function:
    PHP Code:

    function post_data($datastream$url)
    {
        
    $originalurl=$url;

        if (
    substr($originalurl05) == "https") {
            
    $protocol "https";
            
    $url  preg_replace("@^https://@i"""$url); 
            
    $port 443;
        } else {
            
    $protocol "http";
            
    $url preg_replace("@^http://@i"""$url);
            
    $port 80;
        }

        
    $host substr($url0strpos($url"/"));
        
    $uri  strstr($url"/");
        

        
    $reqbody ""
        
        foreach(
    $datastream as $key=>$val) {
            if(
    is_array($val)){ //don't url encode if we're passing an array
            
                
    if (!empty($reqbody)) $reqbody .= "&";
                
    $reqbody .= $key."=".$val;
            
            }else{
            
                if (!empty(
    $reqbody)) $reqbody .= "&";
                
    $reqbody .= $key."=".urlencode($val);
            
            }
            
        }    
        
    $reqlength strlen($reqbody);
        
        
    $reqheader =    "POST $uri HTTP/1.0\r\n".
                        
    "Host: $host\r\n" "User-Agent: CK-Conception POST-o-matic\r\n".
                        
    "Content-Type: application/x-www-form-urlencoded\r\n".
                        
    "Content-Length: $reqlength\r\n\r\n".
                        
    "$reqbody\r\n";
        
        
    # header("Location: $originalurl");
        

        
    if (substr($originalurl05) == "https") {
            
    $socket fsockopen("ssl://" $host$port$errno$errstr); 
        } else {
            
    $socket fsockopen($host$port$errno$errstr); 
        }

        
        if (!
    $socket) {
            
    $result["errno"] = $errno
            
    $result["errstr"] = $errstr
            return 
    $result;
        } 
        
    fputs($socket$reqheader); 
        while (!
    feof($socket)) {
            
    $result[] = fgets($socket4096);
        } 
        
    fclose($socket);

        return 
    $result

    Any assistance would be greatly appreciated


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •