[ back to toc ]

data exchange

Date: 2002/02/27 08:51

Q:
Hi Peter:

I am trying to send data to a script which is located outside out the
current server.
For example, http://mydomain.com/cgi-bin/myscript.cgi want to send "hello"
to http://yourdomain.com/cgi-bin/yourscript.cgi.
Ordinarily, I would just use "Location:
http://yourdomain.com/cgi-bin/yourscript.cgi?hello" to do that. But if I
do that, then the controll will be released from myscript.cgi to
yourscript.cgi.

Bascially the structure for myscript.cgi is

myscript.cgi
#!/usr/bin/perl

#Get variable
...

#save variable
...

#send data to yourscript.cgi
.....

#display confirmation

print "Thank you for your submission.";

So, my question is Is there a way to send data to another
domain, and after that
the script can continue to run and finish the rest of the script? Because
once I use "Location" the script will be terminiated regardlessly, and
whatever comes after "Location" will not be
executed.

Any kind help you can offer is greatly appreciated!

Thank you very much!

*NAME-DELETED*

A:
Dear *NAME-DELETED*,

If you send the header Location: along with the "Status: 302 Permanently
Moved" to the client the client will visit the other site. This is like
two experts standing in a line named Tom and John, and when you ask Joh a
question "A" he says: I do not know, you have to ask Tom the question "B".

This is not what you need. What you need is to ask the question "A" from
John and then John should ask the question "B" from Tom and tell you the
answer. Is it what you need, am I right?

In CGI world this means that the script running on mydomain should fetch
the result of the script on the machine yourdomain and then send to the
client the result it fetched.

To do this, to fetch HTML content of another page you can use the module
libwww. Look for this module.

You can also look at the program

http://peter.verhas.com/progs/perl/webmirror

to see a program, which fetches a web page without any external module.
This is not a CGI program, but a CGI program should do the same.

Regards,
Peter
Q:
Hi Peter:

Thanks for your help. Your suggestion is very helpful. I have another
question for you if it's okay. I found this small perl script which
claims can do the web fetch thing. But I cannot seem to get it to work.
Maybe you could give me some pointers?

#!/usr/bin/perl

use IO::Socket;

if ($#ARGV < 1) { die "perl webgrap.pl <server> <document>\n" }

$server = $ARGV[0];
$document = $ARGV[1];

$remote = IO::Socket::INET->new(Proto=>"tcp", PeerAddr=>$server,
PeerPort=>"80", Reuse=>1)
or die "Can't connect to \"$server\"\n";
# set buffering off
$remote->autoflush(1);
# write HTTP request to server
print $remote "GET $document HTTP/1.0\n\n";
# recieve everything the server sends, and print it to the screen
while (<$remote>) { print }
close $remote;

Of course I replace the ARGV with values.
Seems like the script stops at "$remote = IO::Socket::INET->new ... ",
and when I check the error message, it says "No such file or directory".
I execute the script under shell and it says "some like file cannot be
found". I can only guess that the value I assigned to $server or
$document
are in the wrong format. But I checked other examples, and they used
the same format as I did. Basically, I do not know what else I did wrong.

I know it's maybe too much to ask, but please could you help me again.
Just this little bit. I feel like I am close to the answer. Thanks
again!

*NAME-DELETED*

A:
Dear *NAME-DELETED*,

unfortunately I have never used the INET package and by the time I have
none to learn it for your question.

I can see one point in your code, which should not ever cause any problem,
but is certainly non-standard:

print $remote "GET $document HTTP/1.0\n\n";

should have been

print $remote "GET $document HTTP/1.0\r\n\r\n";

according to the standard: line separation in HTTP is carriage return and
line feed. However most of the servers work well with the line feed.

Regards,
Peter

[ back to toc ]