http request using fsockopen

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default http request using fsockopen

    Hello all,

    I have a form on page A which is submitted trough a POST method to page B.
    On the start of page B I check var $foo. If true I want to "forward" the
    request to
    page C. Sending the POST data also though the POST method.

    I manually create a HTTP request to page C and use the fsockopen method to
    sent the request.

    As a result I get the requested page C (status code "200 OK" is returned)
    but the
    POST data is not received/parsed(?). I mean the $_POST array is empty in
    page C. All other PHP scripts in page C is executed as expected though.

    My questions:
    1 Is what I am doing possible in the first place?
    2 If so, what is the correct syntax to sent the
    POST data ($query) in the entity body of the request?

    My configuration:
    PHP 4.8
    Apache 2.1
    Windows XP SP2

    My code:

    // In Page B from Page A
    if ($foo){

    $url=URL_BASE; // local constant containing the current domain

    if (!preg_match("/http/", $url)) $url = "http://".$url;
    $urlParts = parse_url($url);
    $host = isset($urlParts['host'])?$urlParts['host']:'localhost';
    $port = isset($urlParts['port'])?$urlParts['port']:80;
    $path = isset($urlParts['path'])?$urlParts['path']:"/";

    $errno=null;
    $errstr='';
    $timeout=30;

    $fp=fsockopen($host,$port,$errno,$errstr,$timeout) ;

    if (!$fp){
    echo "<br/>\n $errstr ($errno) <br/>\n";
    } else {

    $query='';
    foreach($_POST as $i=>$v){
    $query.="$i=$v\r\n";
    }

    // headers
    $out ="POST {$path}page_c.php HTTP/1.1 \r\n";
    $out.="Host: $host \r\n";
    $out.="Content-length: ".strlen($query)." \r\n";
    $out.="Content-type: text/html \r\n";
    $out.="Connection: Close \r\n\r\n";
    $out.=$query." \r\n";

    // send the post data
    fwrite($fp,$out);

    // write page C (results)
    while(!feof($fp)) {
    echo fgets($fp,128);
    }
    fclose($fp);
    }
    }


    // end Code...

    Thanks in advance Rob



    Rob Guest

  2. Similar Questions and Discussions

    1. Send Basic HTTP authentication credential in the first HTTP request
      Hello, How can I make the web service proxy class send basic authentication information in the HTTP header of the first request? My...
    2. HTTP Request
      Hey peoples, Just a quick one as i dont know where to start, i have a friends website that returns results and only results on request... so if i...
    3. GET Request w/fsockopen()
      The following function retreives a GET request. It works great with many sites (exp. www.msn.com), but others just hang (exp. www.ebay.com) or...
    4. HTTP 400 BAD REQUEST
      Hi, I am trying to upload a file to the Server using the System.net.Webclient and the uploadfile method. The Upload consistenly happens for file...
    5. HTTP::Request failed on HTTP/1.1 and Connection: Keep-Alive
      Hi Abigail, I know that the error comes from the Java program, the nullpointer error. But what it confused me is: if I'm using an Internet Explorer...
  3. #2

    Default Re: http request using fsockopen

    Hello,

    On 10/25/2004 09:46 AM, Rob wrote: 

    Sure.

     

    It is hard to tell but you may not be sending the request data correctly.

    You may want to try this HTTP client class that you can be sure that it
    can compose and send your POST requests properly.

    http://www.phpclasses.org/httpclient


    --

    Regards,
    Manuel Lemos

    PHP Classes - Free ready to use OOP components written in PHP
    http://www.phpclasses.org/

    PHP Reviews - Reviews of PHP books and other products
    http://www.phpclasses.org/reviews/

    Metastorage - Data object relational mapping layer generator
    http://www.meta-language.net/metastorage.html
    Manuel Guest

  4. #3

    Default Re: http request using fsockopen

    Rob spilled the following:
     
    <snip> 

    I think you might want to add a:
    Content-type: application/x-www-form-urlencoded
    here. Also you should urlencode the RHS of the post variables.
     

    Eh? I don't use this myself and it works for me.

    HTH

    C.
    Colin Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139