PHP Post data with Fsockopen
Solution 1
There are many small errors in your code. Here's a snippet which is tested and works.
<?php
$fp = fsockopen('example.com', 80);
$vars = array(
'hello' => 'world'
);
$content = http_build_query($vars);
fwrite($fp, "POST /reposter.php HTTP/1.1\r\n");
fwrite($fp, "Host: example.com\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 then at example.com/reposter.php put this
<?php print_r($_POST);
When run you should get output something like
HTTP/1.1 200 OK
Date: Wed, 05 Jan 2011 21:24:07 GMT
Server: Apache
X-Powered-By: PHP/5.2.9
Vary: Host
Content-Type: text/html
Connection: close
1f
Array
(
[hello] => world
)
0
Solution 2
Try this instead
$out .= 'Content-Length: ' . strlen($data) . '\r\n';
$out .= "Connection: Close\r\n\r\n";
$out .= $data;
Solution 3
Try this:
<?php
$data="stuff=hoorah\r\n";
$data=urlencode($data);
$fp = fsockopen("www.website.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "POST /script.php HTTP/1.0\r\n";
$out .= "Host: www.webste.com\r\n";
$out .= "Content-Type: application/x-www-form-urlencoded\r\n";
$out .= 'Content-Length: ' . strlen($data) . "\r\n\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
fwrite($fp, $data);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
?>
Some character escapes such as \n
do not work in single quotes.
Solution 4
At no point is $data
being written to the socket. You want to add something like:
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
fwrite($fp, $data);
Joseph Robidoux
Updated on December 20, 2020Comments
-
Joseph Robidoux over 3 years
I am attempting to post data using fsockopen, and then returning the result. Here is my current code:
<?php $data="stuff=hoorah\r\n"; $data=urlencode($data); $fp = fsockopen("www.website.com", 80, $errno, $errstr, 30); if (!$fp) { echo "$errstr ($errno)<br />\n"; } else { $out = "POST /script.php HTTP/1.0\r\n"; $out .= "Host: www.webste.com\r\n"; $out .= 'Content-Type: application/x-www-form-urlencoded\r\n'; $out .= 'Content-Length: ' . strlen($data) . '\r\n\r\n'; $out .= "Connection: Close\r\n\r\n"; fwrite($fp, $out); while (!feof($fp)) { echo fgets($fp, 128); } fclose($fp); } ?>
It is supposed to echo the page, and it is echoing the page, but here is the script for script.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 ); ?>
The outcome is:
HTTP/1.1 200 OK Date: Tue, 02 Mar 2010 22:40:46 GMT Server: Apache/2.2.3 (CentOS) X-Powered-By: PHP/5.2.6 Content-Length: 31 Connection: close Content-Type: text/html; charset=UTF-8 string(0) "" Array ( )
What do I have wrong? Why isn't the variable posting its data?
-
Joseph Robidoux about 14 yearsI did that and still no cigar.
-
Ben Rowe about 14 yearsTry installing a packet sniffer on your server and see what headers are actually being sent to the server. Are they the same as what you're sending in php?
-
carestad over 11 yearsWhy does it show "1f" and "0" at the end there? I get the same, but not quite sure.
-
Julian about 10 yearsit works but you have to change in fsockopen to fsockopen('www.example.com', 80); and in fwrite to fwrite($fp, "Host: www.example.com\r\n");