problems with fgets()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stirfie
    New Member
    • Sep 2007
    • 1

    #1

    problems with fgets()

    I am having trouble with the following script:-
    [code=php]
    <?php
    //function to send back to view
    function post($host, $path, $data) {
    $http_response = '';
    $content_length = strlen($data);
    $fp = fsockopen($host , 80);
    fputs($fp, "POST $path HTTP/1.1\r\n");
    fputs($fp, "Host: $host\r\n");
    fputs($fp, "Content-Type: application/x-www-form-urlencoded\r\n" );
    fputs($fp, "Content-Length: $content_length \r\n");
    fputs($fp, "Connection : close\r\n\r\n") ;
    fputs($fp, $data);
    while (!feof($fp)) $http_response .= fgets($fp, 1024);
    fclose($fp);
    return $http_response;
    }

    // get all post data
    $postdata = 'foo=bar';
    foreach($_POST as $key => $val) $postdata .= '&'.$key.'='.$v al;

    // send post data to function post()
    post('www.stirf ie.com', '/view.php', $postdata);

    ?>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitl ed Document</title>
    </head>
    <body>
    <?php
    echo "$http_response ";
    ?>
    </body>
    </html>

    the page view.php is :-

    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>test post</title>
    </head>

    <body>
    <?php
    $info = $HTTP_POST_VARS ;

    foreach( $info as $key => $value){
    echo $key . ':' . " \t$value<br>";
    }


    ?>
    </body>
    </html>
    [/code]
    the fgets is not retreiving the form data from view.php, can any one tell me where I am going wrong. I would eventually like to write my own ipn, but at the moment I am just experimenting with sockets.

    Regards
    Stirfie
    Last edited by Atli; Sep 9 '07, 04:22 PM. Reason: Added [code] tags
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi Stirfie. Welcome to TSDN!

    Please enclose your posted code in [code] tags (See How to Ask a Question).

    This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

    Please use [code] tags in future.

    Moderator

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      I think you should check out the socket examples in the sockets section of php.net
      (The second code example)

      Specifically the socket_read() and the socket_write() functions.

      Comment

      Working...