Page 1 of 1

Passing POST request to another server

Posted: Fri Nov 26, 2004 12:21 pm
by nyk
I now have a php script that works as a gateway to a perl script, passing the requests from the user though to the other server.
The code looks like this:

Code: Select all

foreach ($_REQUEST as $key => $value) {if ($key) {$p="$p&$key=$value";}}
$p=preg_replace("/^\&/","?",$p);
$html=implode('',file("http:/server.domain.ch/cgi-bin/tor.cgi$p"));
print "$html";
This works fine for GET request (passing short variables), but with POST request that contain things like data from a big textarea or an image, I always get this error:

Code: Select all

Warning: file("http://server.domain.ch/cgi-bin/tor.cgi?login=xxx&pw=xxxx&title=Das Höhere Lehramt&subtitle=&eid=91&value=Das höhere Lehramt (HLA) ist die Ausbildung, die absolviert 
(..............much more text....)
 in /du1v2/users/sub/www/index.php on line 3

Warning: Bad arguments to implode() in /du1v2/users/sub/www/index.php on line 65
It say "warning", but it's actually an error, because it doesn't work like this. Of course the same operation (updating text entry to DB) works, when I do it directly to the target tor.cgi on the other server.
How could I make this work, even with such long arguments (or images)?

Posted: Fri Nov 26, 2004 5:17 pm
by rehfeld
instead of doing implode(file())

use file_get_contents()
its faster

also, looks like you forgot the ?

http:/server.domain.ch/cgi-bin/tor.cgi?$p

have you tried echoing out the url to see if its valid? tryied the url manually in a browser and know it works?


if you need to POST it to the other server, you cant do it that way. you need to use fsockopen() but your method will work if the other server will accept the data as GET

Posted: Fri Nov 26, 2004 5:55 pm
by nyk
no, its not because of the ? in the URL (its added by the regex), I tested this and it works. Only POST doesen't work and I don't know how to do it correctly with fsocket. Itried, but I always get garbage stuff in the data from fgetc! and besides I don't know how to put the POST data in the header... is it just with
Param=4\n
param2=4
and such, below the usual HTTP header?

Posted: Fri Nov 26, 2004 5:59 pm
by rehfeld

Posted: Fri Nov 26, 2004 6:07 pm
by nyk
Ok, it might work with sending the POST data, but it fails at receiving the reply.
The problem is, that there are numbers messing up the reply of the other server:

Code: Select all

<in
1000
put type=hidden name=pw value=xxxr>
which was originally:

Code: Select all

<input type=hidden name=pw value=xxxr>
I have such a "1000" after about each page of HTML, and before the last it's a 99b.
VERY STRANGE! What could cause all these problems?

This is the HTTP header I get back, maybe this tells the reason:

Code: Select all

HTTP/1.1 200 OK Date: Sat, 27 Nov 2004 04:06:50 GMT Server: Apache/1.3.29 (Unix) PHP/4.3.4 Connection: close Transfer-Encoding: chunked Content-Type: text/html; charset=ISO-8859-1 fd1
I also tried the code from here: http://www.faqts.com/knowledge_base/vie ... 05/fid/342
This fixed my first problem that the script was waiting for ever for the server before showing the result. (Because of Connection: close?)

But the "1000" string is still there messing up the HTML reply.

(I can't just filter it out, it depens somehow on the reply length...)

Posted: Sat Nov 27, 2004 7:00 pm
by nyk
The solution is to use HTTP 1.0 instead of 1.1!

Now I have another question: How do I generate I HTTP POST request (as in the above examples in the links) with multipart form data? I mean in order to send files in the request.