POSTing data to a page without using a form submit.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Charlie King

    POSTing data to a page without using a form submit.

    I'm trying to send data to a remote script (a credit card processing
    third party) from my site using POST.

    Currently, I'm doing it using the ususal form dynamically built with
    my values.

    This is less than ideal because I want to perform local processing
    (updating order status in the local database) and then to proceed to
    the remote script, with resultant values, without relying on the user
    pressing a [submit] button.

    I'm part of the way there - I have a function that uses fsock to open
    a socket, sends the POST data and retrieves the response from the
    remote server into an array - but it falls short of emulating an
    actual posted form, in that the user isn't transferred to the remote
    site with the data, as they would be with a form.

    I toyed idly with using a header("Locatio n: $originalurl"), but that
    just redirects the user independantly from the data, and so the target
    script b0rks.

    I don't want to use javascript to click-on-open the form, or to use
    AJAX, because that relies on the users having javascript enabled.

    I can't use cURL, because the client's environment doesn't have it
    installed. I understand that cURL could do what I want to do, which
    implies that it's do-able.

    Any ideas?

    Cheers

    Charlie


    function post_data($data stream, $url) {
    $originalurl=$u rl;

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

    $host = substr($url, 0, strpos($url, "/"));
    $uri = strstr($url, "/");


    $reqbody = "";

    foreach($datast ream as $key=>$val) {
    if(is_array($va l)){ //don't url encode if we're passing an array
    if (!empty($reqbod y)) $reqbody .= "&";
    $reqbody .= $key."=".$val;
    }else{
    if (!empty($reqbod y)) $reqbody .= "&";
    $reqbody .= $key."=".urlenc ode($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("Locatio n: $originalurl");


    if (substr($origin alurl, 0, 5) == "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($socket, 4096);
    }
    fclose($socket) ;

    return $result;
    }

    --
    Charlie
  • Chung Leong

    #2
    Re: POSTing data to a page without using a form submit.

    Charlie King wrote:
    I'm trying to send data to a remote script (a credit card processing
    third party) from my site using POST.
    >From the defunct clp FAQ:
    Q. How do I post a form to another site?
    A. Use stream_context_ create() to create a HTTP POST context, then open
    a connection to the site in question with fopen(), passing the context
    as the fourth parameter. Use fread() to read the result from the form
    submission.

    Example:

    $post_vars = array(
    'post_var1' ="hello",
    'post_var2' ="world"
    );

    // build the request body
    foreach($post_v ars as $name =$val) {
    $pairs[] = $name . '=' . rawurlencode($v al);
    }

    $body = implode("&", $pairs);

    // HTTP options
    $opts = array(
    'http'=>array(
    'method'=>"POST ",
    'header'=>"Cont ent-type: application/x-www-form-urlencoded\r\n" .
    "Content-length: " . strlen($body) . "\r\n" .
    "Cookie: foo=bar\r\n",
    'content'=>$bod y
    )
    );

    $context = stream_context_ create($opts);

    $f = fopen('http://localhost/test.php', 'r', false, $context);

    In PHP 5, a context can also be passed to file_get_conten ts() and
    file().

    Although stream_context_ create() is available since PHP 4.3.0, support
    for HTTP POST has only become available in 4.3.5. If you are using an
    older version, you would need the cURL functions or use fsockopen() to
    open the connection and send the request with fputs().

    Comment

    • B.r.K.o.N.j.A

      #3
      Re: POSTing data to a page without using a form submit.

      Charlie King wrote:
      I'm trying to send data to a remote script (a credit card processing
      third party) from my site using POST.
      >
      Currently, I'm doing it using the ususal form dynamically built with
      my values.
      >
      This is less than ideal because I want to perform local processing
      (updating order status in the local database) and then to proceed to
      the remote script, with resultant values, without relying on the user
      pressing a [submit] button.
      >
      Did you tried to print the result of fsock function? if you get html as
      a result you can simply print it and user would get the desired page
      content and all the links there should work (if they are absolute ones,
      if not, you can tweak them a bit ... :))

      Thing is, if you need to redirect to some outside page that displays
      something and let's user do something then I wouldn't update anything
      before user comes back from that particular page (what happens with the
      order if he changes his mind while he's away on that particular page). I
      usually log that user xy has arrived on summary page (one before CC
      processing) and then when user successfully returns from third party,
      then I do the DB stuff.

      Hope it helps a bit... :)

      --

      B.r.K.o.N.j.A = Bionic Robotic Knight Optimized for Nocturnal Judo and
      Assasination

      Comment

      • Charlie King

        #4
        Re: POSTing data to a page without using a form submit.

        On 16 Aug 2006 08:07:26 -0700, in
        <1155740846.121 540.168860@b28g 2000cwb.googleg roups.com>
        (comp.lang.php) "Chung Leong" <chernyshevsky@ hotmail.comwrot e:
        Charlie King wrote:
        I'm trying to send data to a remote script (a credit card processing
        third party) from my site using POST.
        >
        From the defunct clp FAQ:
        Thanks for this info... it's a shame the FAQ is defunct, it sounds
        like a potentially useful source of info!
        Q. How do I post a form to another site?
        A. Use stream_context_ create() to create a HTTP POST context, then open
        a connection to the site in question with fopen(), passing the context
        as the fourth parameter. Use fread() to read the result from the form
        submission.
        >
        Example:
        [...snip]

        Thanks for that, although it seems to have a similar effect to the
        code that I posted, in that it returns the contents of the target
        site, in this case to a file stream, in my case to an array.

        What I want to do is to have the browser go to the target page, with
        my variables, by POST. I.e. exactly as if the user had clicked on a
        [submit] button in a form whose action was POST and url was the target
        site.
        $f = fopen('http://localhost/test.php', 'r', false, $context);
        Now that I have a filestream with the remote site's contents in it,
        what do I do with it?
        In PHP 5, a context can also be passed to file_get_conten ts() and
        file().
        Unfortunately, I have to work in PHP 4.4.0 without cURL.
        Although stream_context_ create() is available since PHP 4.3.0, support
        for HTTP POST has only become available in 4.3.5. If you are using an
        older version, you would need the cURL functions or use fsockopen() to
        open the connection and send the request with fputs().
        So I have everything I need to create a stream context with POST
        support. Good, now what do I do with it?
        --
        Charlie

        Comment

        • Charlie King

          #5
          Re: POSTing data to a page without using a form submit.

          On Wed, 16 Aug 2006 17:14:21 +0200, in <ebvco8$5pm$1@s s408.t-com.hr>
          (comp.lang.php) "B.r.K.o.N. j.A" <thebrkonja@ine t.hrwrote:
          Charlie King wrote:
          I'm trying to send data to a remote script (a credit card processing
          third party) from my site using POST.

          Currently, I'm doing it using the ususal form dynamically built with
          my values.

          This is less than ideal because I want to perform local processing
          (updating order status in the local database) and then to proceed to
          the remote script, with resultant values, without relying on the user
          pressing a [submit] button.
          Did you tried to print the result of fsock function? if you get html as
          a result you can simply print it and user would get the desired page
          content and all the links there should work (if they are absolute ones,
          if not, you can tweak them a bit ... :))
          I did, and I could print it, but, as you point out, all the relative
          links are broken.

          What I want to achieve is to actually send the user to the remote
          site, exactly as though they had hit [submit] on a POST form.

          The only problem with using a form is that I want to do some local
          processing that is coincidental with their being sent off to the
          payment site. I'd rather not have the possibility of loose ends
          caused by their deciding not to press the button, and go play football
          instead.
          Thing is, if you need to redirect to some outside page that displays
          something and let's user do something then I wouldn't update anything
          before user comes back from that particular page (what happens with the
          order if he changes his mind while he's away on that particular page).
          In the case of this particular page, I get the user returned with a
          'cancelled' or 'timed out' flag that I can process.
          >I
          usually log that user xy has arrived on summary page (one before CC
          processing) and then when user successfully returns from third party,
          then I do the DB stuff.
          If what I want to do turns out to be as difficult as it is looking,
          then I may well have to do that :)
          Hope it helps a bit... :)
          It does, thank you.
          --
          Charlie

          Comment

          • Chung Leong

            #6
            Re: POSTing data to a page without using a form submit.

            Charlie King wrote:
            What I want to do is to have the browser go to the target page, with
            my variables, by POST. I.e. exactly as if the user had clicked on a
            [submit] button in a form whose action was POST and url was the target
            site.
            Oops. Shouldn't have responded having read only half the post.

            Try calling stream_get_meta _data() on the resource returned by fopen
            and see what it contains. Often times an application would immediately
            redirect to a GET page after a POST. If you redirect the user to that
            link, then it'd work.

            You can redirect a POST using the HTTP status code 307. Non-IE browsers
            would show an alert box however.

            Comment

            Working...