Forum Moderators: coopster

Message Too Old, No Replies

Read remote webpage and save in file

Want read a webpage in another website and archive in another server!

         

Bartol

5:26 pm on Mar 17, 2004 (gmt 0)

10+ Year Member



Hi all,

My first topic, and if can anyone help me I thanks very much.

The cenario:
I want read a webpage and then archive (save) the content in a file and show the content of file. I verify if the file is older or recent before save.

And the code I have:

function filecopy($urlsrc,$localdst) {
$fp_urlsrc = fopen($urlsrc,"r");
$del_File = fopen($localdst,"w");
fwrite($del_File,"");
fclose ($del_File);
while (!feof($fp_urlsrc)) {
$fp_localdst = fopen($localdst,"a");
fwrite ($fp_localdst, fread($fp_urlsrc, 1));
fclose ($fp_localdst);
}
fclose($fp_urlsrc);
}

function archive(){
$source = "http://www.example.com/cgi-bin/por_cidade.pl?CIDADE=VISEU&imprimir=1";
$dest = "/home/ftp/incoming/web/public_html/Cinema/informa.cin";

$fd = fopen("$source", "r" ) or die("<center>Unable to access source page</center>");
$pagina = fread($fd, 200000);
echo "pagina=$pagina";
fclose( $fd );

if (file_exists($dest)) {
if (intval(date("Ymd", filectime($dest)))< intval(date("Ymd"))) { filecopy($source,$dest); }
} else {
filecopy($source,$dest);
}

}

/*** CALL FUNCTION ***/
archive();

I receive this message:


Warning: file(http://www.example.com/cgi-bin/por_cidade.pl?CIDADE=VISEU&imprimir=1): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in "/home/ftp/incoming/web/public_html/Cinema/lercinema.php on line 133

Note: the file "informa.cin" have CHMOD 666.

Anyone can understand why I can "see" the webpage, but can read/save the content?!

Regards,
Bart
________
PS: Sorry my poor english :(

[edited by: jatar_k at 6:15 pm (utc) on Mar. 17, 2004]
[edit reason] no personal urls thanks [/edit]

mykel79

5:48 pm on Mar 17, 2004 (gmt 0)

10+ Year Member



Is this all of the code? There aren't 133 lines of code, and the error is on line 133.

Bartol

6:04 pm on Mar 17, 2004 (gmt 0)

10+ Year Member



Hi,

The rest of code is not for this option (read/save webpage), I write only the code important for this option.

:)

Regards,
~ Bart ~

Timotheos

6:56 pm on Mar 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What is on line 133?

Bartol

7:05 pm on Mar 17, 2004 (gmt 0)

10+ Year Member



Hi,

Line 133:

$fd = fopen("$source", "r" ) or die("<center>Unable to access source page</center>");

Regards,
~ Bart ~

brotherhood of LAN

7:12 pm on Mar 17, 2004 (gmt 0)

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



Hey Bartel, welcome to WW.

Just an alternative, I think cURL is enabled by default with PHP, and you might find it useful, you can grab a URL and save it to file quite easily with CURL in around 10 lines of code.

[php.net...]

Bartol

7:16 pm on Mar 18, 2004 (gmt 0)

10+ Year Member



Hi,

Thanks a lot -brotherhood of LAN-, great tip.

I test and I can get the webpage without problem :D

Thanks again.

I post here a function to load the webpage:


(...)

function GetCurlPage($pageSpec) {
if (function_exists('curl_init')){
$agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)";
$ref = "http://www.google.com/";

$ch = curl_init($pageSpec);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_REFERER, $ref);

$tmp = curl_exec ($ch);
curl_close ($ch);
$ret = $tmp;
}else{
$ret = @implode('', @file ($pageSpec));
}
return $ret;
}

(...)
$stringpage = GetCurlPage($source);
(...)

Regards,
~ Bart ~