470,833 Members | 1,397 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,833 developers. It's quick & easy.

Post-problem

I have a client-application that uses a socket to post some data to a
server-application. The following piece of code in the client takes care of
the posting:

$sock = fsockopen("www.majstro.com", 80, $errno, $errstr, 30);
if ($sock)
{
$data = 'a=0&b=1';
fputs($sock, "POST /Web/Majstro/SD_Server.php HTTP/1.0\r\n");
fputs($sock, "Host: www.majstro.com\r\n");
fputs($sock, "Accept: */*\r\n");
fputs($sock, "Content-type: application/x-www-url-encoded\r\n");
fputs($sock, "Content-length: " . strlen($data) . "\r\n\r\n");
fputs($sock, "$data\r\n");
$headers = "";
while ($str = trim(fgets($sock, 4096)))
$headers .= "$str\n";
$body = "";
while (!feof($sock))
$body .= fgets($sock, 4096);
fclose($sock);
return $body;
}

My problem is that SD_Server.php is indeed called, but that it is unable to
extract the parameters from the HTTP-header. They are not present in $_POST,
and count($HTTP_POST_VARS) returns the value 0. On the other hand,
$_SERVER['CONTENT_LENGTH'] contains the value 7, which is exactly the string
length of "a=0&b=1". So, it seems that the data is present in the header,
but for some reason PHP just cannot parse it.

I have run out of ideas to make this work. Has anybody here a suggestion? It
might work if I use the get method instead of the post method, but I very
much like to know what is wrong with this code.
Gerard van Wilgen
--
www.majstro.com (On-line translation dictionary / Enreta tradukvortaro)
www.travlang.com/Ergane (Free translation dictionary for Windows / Senpaga
tradukvortaro por Windows)

Jul 17 '05 #1
8 4227
> fputs($sock, "Content-length: " . strlen($data) . "\r\n\r\n");

too many \r\n\r\n

--
Mike Bradley
http://www.gzentools.com -- free online php tools
"Gerard van Wilgen" <gv********@planet.nl> wrote in message
news:bv**********@reader10.wxs.nl...
I have a client-application that uses a socket to post some data to a
server-application. The following piece of code in the client takes care of the posting:

$sock = fsockopen("www.majstro.com", 80, $errno, $errstr, 30);
if ($sock)
{
$data = 'a=0&b=1';
fputs($sock, "POST /Web/Majstro/SD_Server.php HTTP/1.0\r\n");
fputs($sock, "Host: www.majstro.com\r\n");
fputs($sock, "Accept: */*\r\n");
fputs($sock, "Content-type: application/x-www-url-encoded\r\n");
fputs($sock, "Content-length: " . strlen($data) . "\r\n\r\n");
fputs($sock, "$data\r\n");
$headers = "";
while ($str = trim(fgets($sock, 4096)))
$headers .= "$str\n";
$body = "";
while (!feof($sock))
$body .= fgets($sock, 4096);
fclose($sock);
return $body;
}

My problem is that SD_Server.php is indeed called, but that it is unable to extract the parameters from the HTTP-header. They are not present in $_POST, and count($HTTP_POST_VARS) returns the value 0. On the other hand,
$_SERVER['CONTENT_LENGTH'] contains the value 7, which is exactly the string length of "a=0&b=1". So, it seems that the data is present in the header,
but for some reason PHP just cannot parse it.

I have run out of ideas to make this work. Has anybody here a suggestion? It might work if I use the get method instead of the post method, but I very
much like to know what is wrong with this code.
Gerard van Wilgen
--
www.majstro.com (On-line translation dictionary / Enreta tradukvortaro)
www.travlang.com/Ergane (Free translation dictionary for Windows / Senpaga
tradukvortaro por Windows)

Jul 17 '05 #2

"CountScubula" <me@scantek.hotmail.com> wrote in message
news:UU*****************@newssvr29.news.prodigy.co m...
fputs($sock, "Content-length: " . strlen($data) . "\r\n\r\n");


too many \r\n\r\n


I do not think so. Every example that I have seen had an empty line between
the headers and the data, as is required by the HTTP-specs. Anyway, I have
tried it without an empty line, just to be sure. and that only resulted in
an "invalid header" message.

Gerard van Wilgen
--
www.majstro.com (On-line translation dictionary / Enreta tradukvortaro)
www.travlang.com/Ergane (Free translation dictionary for Windows / Senpaga
tradukvortaro por Windows)


Jul 17 '05 #3
"Gerard van Wilgen" <gv********@planet.nl> wrote in message
news:bv**********@reader08.wxs.nl...

"CountScubula" <me@scantek.hotmail.com> wrote in message
news:UU*****************@newssvr29.news.prodigy.co m...
fputs($sock, "Content-length: " . strlen($data) . "\r\n\r\n");
too many \r\n\r\n


I do not think so. Every example that I have seen had an empty line

between the headers and the data, as is required by the HTTP-specs. Anyway, I have
tried it without an empty line, just to be sure. and that only resulted in
an "invalid header" message.

Gerard van Wilgen
--
www.majstro.com (On-line translation dictionary / Enreta tradukvortaro)
www.travlang.com/Ergane (Free translation dictionary for Windows / Senpaga
tradukvortaro por Windows)


Ok, no problem, I do not know if your posting vars or data, to do vars,
there is no blank line, to post data, yes there is.

data: content-length + blank line + data
vars: no content-length + no blank line + vars + then blanks lines

read a little closer, and grab a packet sniffer and watch the packets before
you try and create a packet without even knowing what it looks like.

here is a super simple one:
http://www.gzentools.com/sockview.php

--
Mike Bradley
http://www.gzentools.com -- free online php tools
Jul 17 '05 #4
You got an extra dash in the content type. It's
"application/x-www-form-urlencoded"

:-)

Uzytkownik "Gerard van Wilgen" <gv********@planet.nl> napisal w wiadomosci
news:bv**********@reader10.wxs.nl...
I have a client-application that uses a socket to post some data to a
server-application. The following piece of code in the client takes care of the posting:

$sock = fsockopen("www.majstro.com", 80, $errno, $errstr, 30);
if ($sock)
{
$data = 'a=0&b=1';
fputs($sock, "POST /Web/Majstro/SD_Server.php HTTP/1.0\r\n");
fputs($sock, "Host: www.majstro.com\r\n");
fputs($sock, "Accept: */*\r\n");
fputs($sock, "Content-type: application/x-www-url-encoded\r\n");
fputs($sock, "Content-length: " . strlen($data) . "\r\n\r\n");
fputs($sock, "$data\r\n");
$headers = "";
while ($str = trim(fgets($sock, 4096)))
$headers .= "$str\n";
$body = "";
while (!feof($sock))
$body .= fgets($sock, 4096);
fclose($sock);
return $body;
}

My problem is that SD_Server.php is indeed called, but that it is unable to extract the parameters from the HTTP-header. They are not present in $_POST, and count($HTTP_POST_VARS) returns the value 0. On the other hand,
$_SERVER['CONTENT_LENGTH'] contains the value 7, which is exactly the string length of "a=0&b=1". So, it seems that the data is present in the header,
but for some reason PHP just cannot parse it.

I have run out of ideas to make this work. Has anybody here a suggestion? It might work if I use the get method instead of the post method, but I very
much like to know what is wrong with this code.
Gerard van Wilgen
--
www.majstro.com (On-line translation dictionary / Enreta tradukvortaro)
www.travlang.com/Ergane (Free translation dictionary for Windows / Senpaga
tradukvortaro por Windows)

Jul 17 '05 #5
"Chung Leong" <ch***********@hotmail.com> wrote in message news:<0P********************@comcast.com>...
You got an extra dash in the content type. It's
"application/x-www-form-urlencoded"

:-)


Right, I think modifying content-type will solve your problem.
Jul 17 '05 #6

"Rahul Anand" <ra************@rediffmail.com> wrote in message
news:62**************************@posting.google.c om...
"Chung Leong" <ch***********@hotmail.com> wrote in message

news:<0P********************@comcast.com>...
You got an extra dash in the content type. It's
"application/x-www-form-urlencoded"

:-)


Right, I think modifying content-type will solve your problem.


The content-type was indeed incorrect. Unfortunately, changing it did not
change anything.

Gerard van Wilgen
--
www.majstro.com (On-line translation dictionary / Enreta tradukvortaro)
www.travlang.com/Ergane (Free translation dictionary for Windows / Senpaga
tradukvortaro por Windows)

Jul 17 '05 #7
"Gerard van Wilgen" <gv********@planet.nl> wrote in message
news:bv*********@reader11.wxs.nl...

"Rahul Anand" <ra************@rediffmail.com> wrote in message
news:62**************************@posting.google.c om...
"Chung Leong" <ch***********@hotmail.com> wrote in message

news:<0P********************@comcast.com>...
You got an extra dash in the content type. It's
"application/x-www-form-urlencoded"

:-)


Right, I think modifying content-type will solve your problem.


The content-type was indeed incorrect. Unfortunately, changing it did not
change anything.

Gerard van Wilgen
--
www.majstro.com (On-line translation dictionary / Enreta tradukvortaro)
www.travlang.com/Ergane (Free translation dictionary for Windows / Senpaga
tradukvortaro por Windows)

here is a capture from a packet sniffer, notice no blank line:

POST http://www-1.gzentools.com/browserchk.php HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword,
application/x-shockwave-flash, */*
Referer: http://www-1.gzentools.com/browserchk.php
Accept-Language: en-us
Content-Type: application/x-www-form-urlencoded
Connection: Close
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
Host: www-1.gzentools.com
Content-Length: 67
Pragma: no-cache
Cookie: sm=browser-server
field_textbox=test+post+data&field_hidden=This+is+ hidden+field+data

--
Mike Bradley
http://www.gzentools.com -- free online php tools
Jul 17 '05 #8

"CountScubula" <me@scantek.hotmail.com> wrote in message
news:2F*******************@newssvr27.news.prodigy. com...
"Gerard van Wilgen" <gv********@planet.nl> wrote in message
news:bv*********@reader11.wxs.nl...

"Rahul Anand" <ra************@rediffmail.com> wrote in message
news:62**************************@posting.google.c om...
"Chung Leong" <ch***********@hotmail.com> wrote in message news:<0P********************@comcast.com>...
> You got an extra dash in the content type. It's
> "application/x-www-form-urlencoded"
>
> :-)
>

Right, I think modifying content-type will solve your problem.


The content-type was indeed incorrect. Unfortunately, changing it did not change anything.

Gerard van Wilgen
--
www.majstro.com (On-line translation dictionary / Enreta tradukvortaro)
www.travlang.com/Ergane (Free translation dictionary for Windows / Senpaga tradukvortaro por Windows)

here is a capture from a packet sniffer, notice no blank line:

POST http://www-1.gzentools.com/browserchk.php HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
application/vnd.ms-powerpoint, application/vnd.ms-excel,

application/msword, application/x-shockwave-flash, */*
Referer: http://www-1.gzentools.com/browserchk.php
Accept-Language: en-us
Content-Type: application/x-www-form-urlencoded
Connection: Close
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
Host: www-1.gzentools.com
Content-Length: 67
Pragma: no-cache
Cookie: sm=browser-server
field_textbox=test+post+data&field_hidden=This+is+ hidden+field+data


I see, but perhaps the packet sniffer suppressed it, or maybe the server
application is smart enough to figure out that the last part must be the
data.

Anyway, I just found out that correcting the Content-Type solved the problem
after all (At first I accidentally used a wrong parameter string when
testing that solution).

Thanks to everyone in the newsgroup who helped me solving this!

Gerard van Wilgen
--
www.majstro.com (On-line translation dictionary / Enreta tradukvortaro)
www.travlang.com/Ergane (Free translation dictionary for Windows / Senpaga
tradukvortaro por Windows)

Jul 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Alec | last post: by
15 posts views Thread by Thomas Scheiderich | last post: by
10 posts views Thread by glenn | last post: by
9 posts views Thread by c676228 | last post: by
56 posts views Thread by UKuser | last post: by
3 posts views Thread by Jag | last post: by
9 posts views Thread by CindyH | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.