2

Can anybody help me to sort out fsockopen issue in localhost.

I created fsock.php to post a variable to test121.php in the same folder. http://localhost/ftp/fsock.php

<?php
$fp = fsockopen('localhost/ftp');

$vars = array(
    'hello' => 'world'
);
$content = http_build_query($vars);

fwrite($fp, "POST /test121.php HTTP/1.1\r\n");
fwrite($fp, "Host: localhost/ftp \r\n");
fwrite($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
fwrite($fp, "Content-Length: ".strlen($content)."\r\n");
fwrite($fp, "Connection: close\r\n");
fwrite($fp, "\r\n");

fwrite($fp, $content);

header('Content-type: text/plain');
while (!feof($fp)) {
    echo fgets($fp, 1024);
}?>

This was the code in test121.php http://localhost/ftp/test121.php

<?php
echo "<br><br>";    
$raw_data = $GLOBALS['HTTP_RAW_POST_DATA'];  
 parse_str( $raw_data, $_POST );

//test 1
var_dump($raw_data);
echo "<br><br>";
//test 2
print_r( $_POST );  
?>

I am getting Failed to parse address error in fsock.php. Can anybody explain me how to sortout this issue in localhost. Thank you in advance.

chandoo
  • 1,276
  • 2
  • 21
  • 32

1 Answers1

3

fsock.php

$fp = fsockopen("localhost", 80, $errno, $errstr, 30);

$vars = array(
    'hello' => 'world'
);
$content = http_build_query($vars);

fwrite($fp, "POST http://localhost/ftp/test121.php HTTP/1.1\r\n");
fwrite($fp, "Host: localhost \r\n");
fwrite($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
fwrite($fp, "Content-Length: ".strlen($content)."\r\n");
fwrite($fp, "Connection: close\r\n");
fwrite($fp, "\r\n");

fwrite($fp, $content);

header('Content-type: text/plain');
while (!feof($fp)) {
    echo fgets($fp, 1024);
}?>

And you might also want to change test121.php and remove $GLOBALS (Not sure why you using this):

<?php
echo "<br><br>";    
$raw_data = $_POST;

//test 1
var_dump($raw_data);
echo "<br><br>";
//test 2
print_r( $_POST );  
?>
Markel Mairs
  • 742
  • 2
  • 10
  • 28