Get Content of URL Into String/Variable

October 10, 2006 | PHP

Here is a simple code to get a content of URL (HTML file) and put it into a variable/string. This is useful for content grabbing, for example.

function getContentOfURL($url){
$file = fopen($url, "r");
if($file){
while (!feof ($file)) {
$line .= fread ($file, 1024*50);
}
return $line;
}
}
?>

Usage:
$string = getContentOfURL("http://sitename.com/index.htm");
?>

yeah, as easy as that ^_*

 

2 comments

This post has 2 comments, amazing!

  1. andiwijaya 18/11/2006 at 07:59

    Dengan code diatas apakah halaman web yang dimakasud bisa di grab semua berikut gambarnya ? bagaimana jika kita hanya ingin menampilkan contentnya saja dihalaman website kita, sehingga tidak akan merubah tampilan desain.

    tentu saja tidak bisa, karena script di atas hanya memasukkan isi file HTML ke dalam suatu variable untuk diolah lebih lanjut.

  2. simao 26/12/2006 at 08:35

    Hi, this method is very easy.But the url requested can’t be with the same session.

    sory, my english is poor.:)

    yupe, you’re right simao. this script might download same content when the page has different SESSION ID. So we need to check it first.

 

Leave a comment

Allowed tags are: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>