Connecting Tech Pros Worldwide Help | Site Map

sending http post requests

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 12:15 PM
yawnmoth
Guest
 
Posts: n/a
Default sending http post requests

i'm trying to send an http post request and see if the server got it
correctly. i'm sending the http post request with this script:

<?
$address = 'domain.tld';
$port = 80;

$proxy = fsockopen("tcp://$address", (int) $port, $errno, $errstr, 1);

$extra = "extra=blah&this=test";

fputs($proxy,"POST /test.php HTTP/1.0\r\n");
fputs($proxy,"Host: $address\r\n");
fputs($proxy,"Content-Length: ".strlen($extra)."\r\n\r\n");
fputs($proxy,"$extra\r\n");

while (!feof($proxy))
print fgets($proxy);
fclose($proxy);
?>

test.php's contents are as follows:

<?
print_r($_POST);
?>

this is what i want to see:

Array
(
[extra] => blah
[this] => test
)

this is what i am seeing:

Array
(
)

any ideas as to what i'm doing wrong and how i can fix it? thanks!


  #2  
Old July 17th, 2005, 12:15 PM
Colin McKinnon
Guest
 
Posts: n/a
Default Re: sending http post requests

yawnmoth wrote:
[color=blue]
> i'm trying to send an http post request and see if the server got it
> correctly. i'm sending the http post request with this script:
>[/color]

My script looks almost exactly the same (but works ;). A couple of
suggestions:
[color=blue]
> <?
> $address = 'domain.tld';
> $port = 80;
>
> $proxy = fsockopen("tcp://$address", (int) $port, $errno, $errstr, 1);
>[/color]

Timeout seems a bit short - but OK for testing locally. The 'tcp://' was new
to me - according to the manual it's valid. Might be worth trying without.
[color=blue]
> $extra = "extra=blah&this=test";
>
> fputs($proxy,"POST /test.php HTTP/1.0\r\n");
> fputs($proxy,"Host: $address\r\n");[/color]

** maybe add : "Content-type: application/x-www-form-urlencoded\r\n";
[color=blue]
> fputs($proxy,"Content-Length: ".strlen($extra)."\r\n\r\n");
> fputs($proxy,"$extra\r\n");
>[/color]

literal paste from my script:
$p.="Content-length: $len\n\n";
$p.= "$str\n";
$p.= "\r\n";

It was kind of a long tiime ago I wrote it, and can't recall why the first
two lines didn't get a '\r'.

HTH

C.
  #3  
Old July 17th, 2005, 12:16 PM
yawnmoth
Guest
 
Posts: n/a
Default Re: sending http post requests


Colin McKinnon wrote:[color=blue]
> yawnmoth wrote:
>[color=green]
> > $extra = "extra=blah&this=test";
> >
> > fputs($proxy,"POST /test.php HTTP/1.0\r\n");
> > fputs($proxy,"Host: $address\r\n");[/color]
>
> ** maybe add : "Content-type: application/x-www-form-urlencoded\r\n";[/color]

That fixed it - thanks! :)

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.