Forum Moderators: coopster & phranque

Message Too Old, No Replies

opening & reading external files

files from another server

         

agradation

11:23 pm on Aug 7, 2005 (gmt 0)

10+ Year Member



Hi,

To open files, we use e.g. this:

open (NNN, "path/filename.ext")

The file being opened is, however, on the same server as the script. My question is, how can I open a file on another server? Simply changing "path/filename.ext" into "http://www.server.com/path/filename.ext" does not work. Then, how?

Tnx for help.

rainborick

5:09 pm on Aug 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You can't "open" a file on another server in your scripts. You can only retrieve such files via HTTP using GET or a module like LWP::UserAgent. Good luck!

agradation

10:29 pm on Aug 8, 2005 (gmt 0)

10+ Year Member



Oh, correct, that's what I mean - retrieve. What I want to do is to read the contents of an external HTML file into a variable.

But as soon as I add the stroke
use LWP::UserAgent;
into my script, it starts outputting Internal Server Error.

I tried also LWP::Simple, the result was the same.

Any ideas? Tnx.

balam

8:39 pm on Aug 9, 2005 (gmt 0)

10+ Year Member



agradation, welcome to WebmasterWorld!

Can you FTP to this external server? If so, you may want to bypass LWP and take a look at Net::FTP.


use Net::FTP;

# Connect to server
$ftp = Net::FTP->new('ftp.example.com');

# Log into server
$ftp->login('anonymous','root@example.com');

# Change directory on server
$ftp->cwd('/public_html/subdir');

# Set ASCII transfer mode
$ftp->ascii;

# Fetch remote file, store it locally
$ftp->get('remote_file.html','local_file.html');

# Close connection to server
$ftp->quit;

# Slurp whole file into one var
{
local (*INPUT, $/);
open (INPUT, 'local_file.html');
$whole_html_file_in_one_variable = <INPUT>;
}

# Delete local file if necessary
# or desired; add your own code to
# do as you will; season to taste!

agradation

3:30 am on Aug 10, 2005 (gmt 0)

10+ Year Member



Unfortunately, I can't FTP. It's an internet auction site in Poland. Unlike eBay, they don't have WAP service, and I want to follow price movements while travelling. So I want to grab the contents of the item page, extract the price, and display it to my phone on a WAP page. I know everything except how to have the page read by my CGI script.

balam

3:48 am on Aug 10, 2005 (gmt 0)

10+ Year Member



Yah, my idea is only good if you have that option.

LWP should do the trick for you; I use it a bunch myself, for things I can't FTP. :) You've tried it, but it didn't work out, so howzabout posting the relevant section of your code for debugging? If that's uncomfortable, you can Sticky me.

Or not. :)

agradation

4:45 pm on Aug 10, 2005 (gmt 0)

10+ Year Member



Thanks for the help. The code that I wrote so far is:

--------------------------------------
#!/usr/bin/perl

print "Content-Type: text/html\n\n";

use strict;
use LWP::UserAgent;
--------------------------------------

... and it results in Internal Server Error (Apache/2.0.52 (Unix) PHP/4.3.10 Server).

It seems that my ISP has no installed LWP::UserAgent.

I think I could go around this obstacle by putting the necessary part of the UserAgent module code, directly into my CGI script. The problem is that I am not familiar with such procedure and I don't know which is the necessary part in the module.

agradation

4:46 pm on Aug 10, 2005 (gmt 0)

10+ Year Member



And, the page I want to retrieve is
http://www.example.pl/show_item.php?item=60070993

[edited by: jatar_k at 9:12 pm (utc) on Aug. 10, 2005]
[edit reason] no urls thanks [/edit]