http request using fsockopen

This is a discussion on http request using fsockopen within the PHP General forums, part of the PHP Programming Forums category; Hello all, I have a form on page A which is submitted trough a POST method to page B. On ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-25-2004
Rob
 
Posts: n/a
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



Reply With Quote
  #2 (permalink)  
Old 10-25-2004
Colin McKinnon
 
Posts: n/a
Default Re: http request using fsockopen

Rob spilled the following:

>
> 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.
>

<snip>
> // 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";


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.

> $out.="Connection: Close \r\n\r\n";


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

HTH

C.
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 12:16 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0