Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

forcing a 'post' response?

2 views
Skip to first unread message

Alan (Ursus Major)

unread,
Dec 17, 2002, 6:23:13 PM12/17/02
to
Folks:
I need to respond programmatically to a site expecting a 'post' response.
The following code provides a 'GET' response. Any idea how I can
use a canned 'POST' response?
----------------------
Should be named 'test.php':

<?
if (!$doit){
header("Location: test.php?doit=yes");
exit;
} else {
?>
<html><body><br>
<h3> Results </h3><br>
<?
echo "$" . "_GET = ";
print_r($_GET);
echo "<br>$" ."_POST = ";
print_r($_POST);
echo "<br>";
?>
<br>
</body>
</html>
<?
}
?>

----------------------

Alan Walkington
United Defense, San Jose


Datura

unread,
Dec 17, 2002, 10:30:21 PM12/17/02
to
Alan ..

You should read my post to you under your last question .. this is a
somewhat complex thing you're trying to do. Not really difficult, but there
are a few things you've got to get just right ..

First off- are you trying to do this through a web browser, or through a
script to be run at a command line. If at command line, what platform re
you on? If not at command line, how do you intend to automate this script
once you have it written?

Second: To fake a browser POST request does include the use of some headers-
but the PHP header function is never going to get you there, as I think
you're finding out.

To spell it out in another way- what you have to do is to write a php
program that EMULATES a browser request to a web server. It sounds
complicated, but it is surprisingly easy. The only hard part is calculating
the length of your POST string and creating the correct header ..

For example, here is what an HTTP post request from a browser to a webserver
looks like:

POST /cgi-bin/phone_book.cgi HTTP/1.0
Referer: http://www.somedomain.com/Direcory/file.html
User-Agent: Mozilla/1.22 (Windows: I: 32bit)
Accept: */*
Content-type: application/x-www-form-urlencoded
Content-length: 29

name=Selena+Sol&phone=7700404

create a string with the above info- this is what you have to send to the
webserver after you open up a socket!

$post_data = "POST /cgi-bin/phone_book.cgi HTTP/1.0\r\nReferer:
http://www.somedomain.com/Direcory/file.html\r\nUser-Agent: Mozilla/1.22
(Windows: I: 32bit)\r\n
Accept: */*\r\nContent-type:
application/x-www-form-urlencoded\r\nContent-length: 29\r\n\r\n
name=Selena+Sol&phone=7700404\r\n\r\n"

Be sure there are TWO carriage return linefeeds before and after the POST
data (thats the last line- theone with your variables") Also- the content
length has to match the length of the data line (again, the last line)

Read carefully the post I sent you earlier. Use this information- look up
how to open a socket on php- its easy. Open up a socket (try the fopen()
function) to your target
server on port 80. Use the fputs() function to pass the $post_data string -
read the return data with fgets()

I hope this helps you out. If you need help with sockets let me know.
Please respond with specific questions.

Bryan Ingram


"Alan (Ursus Major)" <alan[REMOVE]@walkington.org> wrote in message
news:BlOL9.1179806$6N5.1...@post-03.news.easynews.com...

Alan (Ursus Major)

unread,
Dec 17, 2002, 11:10:10 PM12/17/02
to
Datura:

Sigh ...

You are right. I was trying to force a solution the 'easy' way. And that,
of course, seldom works.

Ok, off to research 'fopen()'.

Thanks very much for you patience.

Alan Walkington

"Datura" <nos...@stopspam.com> wrote in message
news:hZRL9.1187199$H65.1...@post-02.news.easynews.com...

Alan (Ursus Major)

unread,
Dec 18, 2002, 10:55:03 PM12/18/02
to
Datura:
A couple of questions:

1) I will replace the necessary characters in the query_string as follows:
-----------------------
$R=array(" ", "/", ".", "*");
for($i=0; $i < 4 - 1; $i++){
$this->qs=str_replace($R[$i], "%" .sprintf("%2X", ord($R[$i])),
$this->qs);
}
-----------------------
(imho, php regexp implementation is still not very good (ie., it sucks. In
perl, ruby, grep awk, etc. I can do this all in a single regex =~
statement).

What characters do I need to replace in the query string?

2) Do I count the number of characters before or after I do the
replacements. I am guessing /after/, but I don't know when the string is
parsed and the %xx substrings are replaced.

Can you help me with either of these question?

Thanks,


Alan Walkington
United Defense, San Jose

"Datura" <nos...@stopspam.com> wrote in message
news:hZRL9.1187199$H65.1...@post-02.news.easynews.com...


> Alan ..
>
> You should read my post to you under your last question .. this is a
> somewhat complex thing you're trying to do. Not really difficult, but
there
> are a few things you've got to get just right ..
>

<snip>

Daniel Tryba

unread,
Dec 19, 2002, 2:07:54 AM12/19/02
to
"Alan \(Ursus Major\)" <alan[REMOVE]@walkington.org> wrote:
> Datura:
> A couple of questions:
>
> 1) I will replace the necessary characters in the query_string as follows:
[snip 10 lines]

>
> What characters do I need to replace in the query string?

http://php.net/htmlentities :)

> 2) Do I count the number of characters before or after I do the
> replacements. I am guessing /after/, but I don't know when the string is
> parsed and the %xx substrings are replaced.

header('Content-Length: '.strlen(urlencode($str)));


BTW I use the following script to do what you appear to need, it's not
completly tested yet but it appears to suit my needs:

<?php
$post='';
foreach($_GET as $key => $value)
{
if($post)
{
$post.='&';
}

$post.=$key.'='.htmlentities($value);
}

if($fp=fsockopen($host,80))
{
fwrite($fp,"POST $url HTTP/1.1\r\n");
fwrite($fp,"Host: $host\r\n");
fwrite($fp,"Content-Type: application/x-www-form-urlencoded\r\n");
fwrite($fp,"Content-Length: ".strlen($post)."\r\n");
fwrite($fp,"Connection: close\r\n");
fwrite($fp,"\r\n");
fwrite($fp,"$post\r\n");


$chunks=false;
while(!feof($fp))
{
do
{
$str=trim(fgets($fp,1024));

if(strpos($str,'Transfer-Encoding: chunked')===0)
{
$chunks=true;
}
}
while(strlen($str)>0);

if($chunks)
{
while(!feof($fp))
{
$size=hexdec(fgets($fp,32));

if($size>0)
{
$str='';
while((strlen($str)<$size) && (!feof($fp)))
{
$get=$size-strlen($str);
$str.=fread($fp,$get);
}
echo $str;
}
}
}
else
{
while(!feof($fp))
{
echo fread($fp,65535);
}
}
}

fclose($fp);
}
else
{
echo "Fopen failed";
}
?>

--

Daniel Tryba

Alan (Ursus Major)

unread,
Dec 19, 2002, 3:24:44 AM12/19/02
to
many thanks, Daniel.

Alan

"Daniel Tryba" <news_comp...@canopus.nl> wrote in message
news:atrr8a$q7s$2...@news.tue.nl...


André Næss

unread,
Dec 19, 2002, 6:15:03 AM12/19/02
to
Alan (Ursus Major) wrote:

> Folks:
> I need to respond programmatically to a site expecting a 'post' response.
> The following code provides a 'GET' response. Any idea how I can
> use a canned 'POST' response?

This should help:
http://snoopy.sourceforge.net/

:)

André Næss

Alan (Ursus Major)

unread,
Dec 19, 2002, 8:20:42 PM12/19/02
to
Thanks:

With your help and Andre's, I've got it working.

I'm on a DSL connection, so when I power up, I now automatically update my
domain-forwarding service with the new IP.

If I am on line when they change my IP, as has happened sometimes, I check
it every hour and update the service if it has changed.

Alan Walkington

"Daniel Tryba" <news_comp...@canopus.nl> wrote in message
news:atrr8a$q7s$2...@news.tue.nl...

Alan (Ursus Major)

unread,
Dec 19, 2002, 8:22:06 PM12/19/02
to
Thanks:

With your help and Daniels, I've got it working.

I'm on a DSL connection, so when I power up, I now automatically update my
domain-forwarding service with the new IP.

If I am on line when they change my IP, as has happened sometimes, I check

it every hour and update the forwarding service if it has changed.

Alan Walkington

"André Næss" <andre.ha...@ifi.uio.no> wrote in message
news:ats677$4ai$2...@maud.ifi.uio.no...

0 new messages