PHP

Get Content of URL Into String/Variable

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.

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

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

yeah, as easy as that ^_*

&nbsp;

2 Comments

  1. 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

    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 Reply