doing an http post

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gary Petersen

    doing an http post

    For the benefit of others, I want to show how to do
    an HTTP POST request using fsockopen(). I banged my
    head against a wall for two days trying to figure
    this out. I even went to http://php.net/ to find
    out how to do this, but it didn't help because
    my mind automagically converted the "Content-Length"
    header into "Length."

    I even went to http://www.ietf.org/ to find out
    the specific protocol for HTTP POSTs, but I didn't
    find it there. RFC 2616 (HTTP) does not give the
    exact format for POST requests.

    Question: Where is the format for POST requests defined?

    ---------------- send_post.php ---------------
    <?php
    // file: send_post.php
    error_reporting (E_ALL & ~E_NOTICE);
    $eol = "\r\n";
    $errno = 0;
    $errstr = '';
    $data = 'username=georg e&password=i81b pz';
    $fid = fsockopen('loca lhost', 8080, &$errno, &$errstr, 30);
    if ($fid) {
    fputs ($fid, "POST /post/show_post.php HTTP/1.1$eol");
    fputs ($fid, "HOST: localhost$eol") ;
    fputs ($fid, "Connection : close$eol");
    fputs ($fid, "Content-Type: application/x-www-form-urlencoded$eol" );

    // Use 'Content-Length' NOT 'Length' !
    fputs ($fid, 'Content-Length: ' . strlen($data) . $eol);
    fputs ($fid, $eol);
    fputs ($fid, $data);
    fputs ($fid, $eol);
    fpassthru($fid) ;
    }

    ?>

    ------------- show_post.php -----------------
    <?php
    // file: show_post.php
    error_reporting (E_ALL & ~E_NOTICE);

    echo "<pre>\n";
    echo "------------ POST VARIABLES ----------\n";
    print_r($HTTP_P OST_VARS);
    echo "------------ SERVER VARIABLES ----------\n";
    print_r($HTTP_S ERVER_VARS);
    echo "</pre>\n";
    ?>
  • Rasmus Christian Kaae

    #2
    Re: doing an http post

    "Gary Petersen" <garyp1492@REMO VE.MEearthlink. INVALID> skrev i en meddelelse
    news:pan.2003.0 8.23.05.18.48.3 18730.493@REMOV E.MEearthlink.I NVALID...[color=blue]
    > For the benefit of others, I want to show how to do
    > an HTTP POST request using fsockopen(). I banged my
    > head against a wall for two days trying to figure
    > this out. I even went to http://php.net/ to find
    > out how to do this, but it didn't help because
    > my mind automagically converted the "Content-Length"
    > header into "Length."[/color]


    Use curl instead.

    ---
    Rasmus Christian Kaae
    www.3kings.dk | www.hestebasen.com


    Comment

    • Phil Roberts

      #3
      Re: doing an http post

      With total disregard for any kind of safety measures "Rasmus
      Christian Kaae" <rasmusGODDAW@3 kings.dk> leapt forth and uttered:
      [color=blue][color=green]
      >> For the benefit of others, I want to show how to do
      >> an HTTP POST request using fsockopen(). I banged my
      >> head against a wall for two days trying to figure
      >> this out. I even went to http://php.net/ to find
      >> out how to do this, but it didn't help because
      >> my mind automagically converted the "Content-Length"
      >> header into "Length."[/color]
      >
      >
      > Use curl instead.
      >[/color]

      What an amazingly helpful answer.

      Try something like this? :

      function PostToHost($hos t, $path, $data_to_send) {
      $fp = fsockopen($host ,80);
      fputs($fp, "POST $path HTTP/1.1\n" );
      fputs($fp, "Host: $host\n" );
      fputs($fp, "Content-type: application/x-www-form-urlencoded\n" );
      fputs($fp, "Content-length: ".strlen($data_ to_send)."\n" );
      fputs($fp, "Connection : close\n\n" );
      fputs($fp, $data_to_send);
      fclose($fp);
      }

      --
      There is no signature.....

      Comment

      • MeerKat

        #4
        Re: doing an http post

        Gary Petersen wrote:[color=blue]
        > Question: Where is the format for POST requests defined?[/color]

        Try:


        The RFC in question is RFC2616, which is the HTTP/1.1 specification and
        can be found here:
        ftp://ftp.isi.edu/in-notes/rfc2616.txt

        --
        MeerKat

        Comment

        • Gary Petersen

          #5
          Re: doing an http post

          On Sat, 23 Aug 2003 05:38:35 -0500, in message
          <Xns93E07675A24 BFphilroberts@2 16.196.97.132>, the AI program named "Phil
          Roberts" <philrob@holyfl atnetshit.net> randomly printed:
          [color=blue]
          > fputs($fp, "POST $path HTTP/1.1\n" );
          > fputs($fp, "Host: $host\n" );[/color]

          I thought that the end of line sequence was
          \r\n not just \n.

          Comment

          Working...