?
Solved

How to save web-pages using php

Posted on 2004-11-08
1
Medium Priority
?
261 Views
Last Modified: 2008-03-06
For example i have text input, and i want once user type any url there my script to save this website in to "cache" directory on my sarver and then show it in the iframe in my script. Does any one got any clue how can I do this. Some code examples will be great...
0
Comment
Question by:g00r00
1 Comment
 
LVL 7

Accepted Solution

by:
jdpipe earned 200 total points
ID: 12530415
You can use the 'file' function both ways:

TO capture the web page, use

$lines=file("http://google.com");
$str=join("",$lines);

To write the stuff to a file,
$fn=tempnam("/tmp","cache-");
$fh=fopen($fn,'w');
fwrite($fh,$str);
fclose($fh);

$filecode=preg_match("/cache-(.*)$",$fn);
print("<iframe src=\"show.php?fn=".urlencode($filecode)."\">");

To display the text in an IFRAME, use the follow file as 'show.php':
$fn=$_GET['fn'];
preg_replace("#/\. #","",$fn);
$lines=file("/tmp/cache-".$fn);
$str=join("",$lines);
print($str);

Note that if there are relative URLs in the HTML file that you have thus downloaded then they might not display properly. You can play around with the <BASE> tag in HTML or else use some regexps to fix up the locations of the images. But even then some servers will not allow images to be loaded unless being requested from a local referrer. So you might have some problems possibly.

The full-blown approach is to use a class which emulates a browser. I have found the following to be really solid:
http://www.phpclasses.org/browse/package/576.html

Hope that helps

JP
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month15 days, 11 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question