Page 1 of 1

trying to read a url index page with fopen.getting warnings

Posted: Sat Apr 24, 2010 9:52 am
by mayanktalwar1988

Code: Select all

<?php
$url = "http://www.facebook.com/index.php";
$fh = fopen($url, 'r');
$theData = fread($fh, filesize($url));
fclose($fh);
echo $theData;
?>
i am trying to read page source but above code give warnings and display nothing from page source

Warning: filesize() [function.filesize]: stat failed for http://www.facebook.com/index.php in C:\xampp\htdocs\te.php on line 5

Warning: fread() [function.fread]: Length parameter must be greater than 0 in C:\xampp\htdocs\te.php on line 5

so what the prob with above?

Re: trying to read a url index page with fopen.getting warni

Posted: Sat Apr 24, 2010 10:39 am
by Zyxist
HTTP stream wrapper does not support obtaining the file length. See:

http://docs.php.net/manual/en/wrappers.http.php

As you can see, in the row "Stat" (file information) there is "No", because such information is not accessible through the HTTP protocol.

Re: trying to read a url index page with fopen.getting warni

Posted: Sat Apr 24, 2010 10:42 am
by mayanktalwar1988
so how should i approch to get page source out?

Re: trying to read a url index page with fopen.getting warni

Posted: Sat Apr 24, 2010 10:50 am
by Zyxist
You do not need the length to get the file contents:

Code: Select all

echo file_get_contents('http://www.example.com/index.php');
Reading in 1024-byte chunks:

Code: Select all

while(!feof($f))
{
  echo fread($f, 1024);
}