Forum Moderators: coopster

Message Too Old, No Replies

HTTP POST help :(

         

netfiends

3:22 pm on Jun 7, 2005 (gmt 0)

10+ Year Member



I'm trying to POST data to a remote file > Have the remote file post back results... To access the variables I post to the remote file would I just treat them like a normal form post? i.e. $_POST['var']? My next question is how would I send data back to the file that sent the original post?

$fp = @fsockopen("www.site.com", 80, $errno, $errstr, 5);

$header = "POST test_post.php HTTP/1.0\r\n";
$header .= "Host: www.site.com \r\n";
$header .= "Accept: */*\r\n";
$header .= "Content-Length: ". strlen($querystring) ."\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Connection: Close\r\n";
$header .= $querystring;

fwrite($fp, $header);
$data = "";
while(!feof($fp)){
$data .= @fgets($fp, 1024);
}
fclose($fp);

mcibor

3:37 pm on Jun 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



you can post the url from which you are sending the data as well. Regarding the variables the $_POST["var"] will do allright.

After you send the data to that script you can redirect and send the data back. Otherwise I don't know how to do that.

best regards
Michal Cibor

netfiends

4:33 pm on Jun 7, 2005 (gmt 0)

10+ Year Member



Okay, below is what I have and it doesn't seem to work for me... It seems to just get stuck with a 400 Bad Request error. Could this be happening because I'm running it on the same site?

Page: test_post.php on www.site2.com:
$fp = @fsockopen("www.site.com", 80, $errno, $errstr, 5);
$querystring = "asdf=". urlencode("test var");

$header = "POST verify_post.php HTTP/1.0\r\n";
$header .= "Host: www.site.com \r\n";
$header .= "Accept: */*\r\n";
$header .= "Content-Length: ". strlen($querystring) ."\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Connection: Close\r\n";
$header .= $querystring;

fwrite($fp, $header);
$data = "";
while(!feof($fp)){
$data .= @fgets($fp, 1024);
}
fclose($fp);

Page: verify_post.php on www.site.com:
$fp = @fsockopen("www.site2.com", 80, $errno, $errstr, 5);

if($_POST['asdf']){
$querystring = "asdf=". urlencode("it works");
}

$header = "POST test_post.php HTTP/1.0\r\n";
$header .= "Host: www.site2.com \r\n";
$header .= "Accept: */*\r\n";
$header .= "Content-Length: ". strlen($querystring) ."\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Connection: Close\r\n";
$header .= $querystring;

fwrite($fp, $header);
fclose($fp);

mcibor

9:11 pm on Jun 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It should be something like this:
Page: test_post.php on www.site2.com:

if(!isset($_POST)){
$fp = @fsockopen("www.site.com", 80, $errno, $errstr, 5);
$querystring = "asdf=". urlencode("test var");

$header = "POST verify_post.php HTTP/1.0\r\n";
$header .= "Host: www.site.com \r\n";
$header .= "Accept: */*\r\n";
$header .= "Content-Length: ". strlen($querystring) ."\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Connection: Close\r\n";
$header .= $querystring;

fwrite($fp, $header);
fclose($fp);
}else{
$querystring = "asdf=". urlencode("it works");
}

Page: verify_post.php on www.site.com:

$fp = @fsockopen("www.site2.com", 80, $errno, $errstr, 5);

if($_POST['asdf']){
$querystring = "asdf=". urlencode("it works");
}

$header = "POST test_post.php HTTP/1.0\r\n";
$header .= "Host: www.site2.com \r\n";
$header .= "Accept: */*\r\n";
$header .= "Content-Length: ". strlen($querystring) ."\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Connection: Close\r\n";
$header .= $querystring;

fwrite($fp, $header);
fclose($fp);


You forgot to check the first page for the POST. The algorythm is such: site1 sends post to site2, site2 sends post to side1, if site1 receives post it does sth.
Best regards
Michal Cibor

netfiends

9:39 pm on Jun 7, 2005 (gmt 0)

10+ Year Member



Still doesn't seem to work... In regards to what we've talked, I was looking at a paypal ipn script and it doesn't seem to have a check for $_POST...

// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';

foreach($_POST as $key => $value){
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}

// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30);

if(!$fp){
// HTTP ERROR
}else{
fputs($fp, $header . $req);
while(!feof($fp)){
$res = fgets ($fp, 1024);
}
}
fclose ($fp);

Dino1

7:38 am on Jun 11, 2005 (gmt 0)

10+ Year Member



I get exactly the same problem:

This is the post string:

POST /cgi-bin/search.pl?hal&fo=s HTTP/1.1 Host: www.blahblah.org.uk User-Agent: PostIt Content-Type: application/x-www-form-urlencoded Content-Length: 92
sales_rental=S&type=&num_beds=&price_low=&price_high=&keywords=&search_request_button=Search

And this the response i always get:

HTTP/1.1 400 Bad Request Content-Type: text/html Date: Fri, 10 Jun 2005 16:18:25 GMT Connection: close
Content-Length: 20
Bad Request

Have no idea why the receiving script thinks content-type is text/html and length 20. HTTP/1.0 also gives same results. Yet I can create a form to post to the same script fine?

Dino1

9:26 am on Jun 11, 2005 (gmt 0)

10+ Year Member



Ok fixed my problem, found there was a \n at the end of my host line, rpelaced with \r\n and all now works fine.

coopster

12:48 pm on Jun 11, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Yep, you have to have your end of line markers correct.

Welcome to WebmasterWorld, Dino1.

netfiends

1:28 pm on Jun 11, 2005 (gmt 0)

10+ Year Member



Dino1,
Do you have it POST to script B then SCRIPT B does it stuff then POSTs to SCRIPT A? Would it be possible to see what you have?