View Full Version : Sending Post data with fsockopen

03-04-2009, 01:00 PM
hey guys, having major problems with this and its beggining to annoy me now.

I need to send post data using php to our server and handle the response. At present the code i have is

$postdata = 'username=test&key=gdfghsjkhtr234ir';

$fp = fsockopen("test.servername.co.uk", 80, $errno, $errstr, 30);
fputs($fp, "POST /testapisystem2.php HTTP/1.0\r\n");
fputs($fp, "Host: test.servername.co.uk\r\n");
fputs($fp, "Content-type: application/x-www-form- urlencoded\r\n");
fputs($fp, "Content-length: " . strlen($data) . "\r\n");
fputs($fp, "User-agent: Mozilla/4.0 (compatible: MSIE 7.0; Windows NT 6.0)\r\n");
fputs($fp, "Connection: close\r\n\r\n");
fputs($fp, $postdata);
while (!feof($fp)) {
$buf .= fgets($fp,128);

echo $buf;

The request is received by the script as shown by the logs for the server however there are no post variables received!

on the receiving script I have performed a print_r($_POST) and there are no post variables at all!!

I cant use cURL as not all servers have this enabled and I need to provide the php framework to other people to use.

so does anyone have any ideas why the post vars are not getting to the file?

I have also tried the script on a different server (different OS as well as configuration) and still get the same problem

03-04-2009, 02:30 PM
I finally resolved this, the data wasn't in the $_POST[] variables but it was in the $GLOBALS['HTTP_RAW_POST_DATA'] variable, then it is a case of exploding the values with a & seperator

03-04-2009, 04:46 PM
You could also use:

$raw_data = file_get_contents( 'php://input' );

...which is the same as:


Then, instead of exploding on the ampersands (hacky), use parse_str():

parse_str( $raw_data, $_POST );

print_r( $_POST );

03-04-2009, 06:00 PM
thanks for that, my next problem is doing an sftp call with curl :( but i'll post a seperate thread for that

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum