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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mayanktalwar1988
Forum Contributor
Posts: 133
Joined: Wed Jul 08, 2009 2:44 am

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

Post 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?
User avatar
Zyxist
Forum Contributor
Posts: 104
Joined: Sun Jan 14, 2007 10:44 am
Location: Cracow, Poland

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

Post 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.
mayanktalwar1988
Forum Contributor
Posts: 133
Joined: Wed Jul 08, 2009 2:44 am

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

Post by mayanktalwar1988 »

so how should i approch to get page source out?
User avatar
Zyxist
Forum Contributor
Posts: 104
Joined: Sun Jan 14, 2007 10:44 am
Location: Cracow, Poland

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

Post 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);
}
Post Reply