Page 1 of 1

Read webpage source from fopen

Posted: Tue Dec 16, 2008 8:41 am
by zerandib
Hello

when read the site content of http://www.google.com by using fopen it works fine

<?php
$fh = fopen("http://www.google.com/", "r");

while(!feof($fh))
{
$output = htmlspecialchars(fgets($fh, 1024));
echo ("$output<br />");
}
fclose($fh);
?>


But
when read the site content of http://www.youtube.com, it fails; gives error
why is that!
How to read the youtube.com source by using fopen (Or any other method)

<?php
$fh = fopen("http://www.youtube.com/", "r");

while(!feof($fh))
{
$output = htmlspecialchars(fgets($fh, 1024));
echo ("$output<br />");
}

fclose($fh);


?>



Thanks
Need help!

Re: Read webpage source from fopen

Posted: Tue Dec 16, 2008 12:48 pm
by John Cartwright
They are likely blocking your request because your user agent is that of your servers, and not the browser.

You can emulate the user agent of the request by using cURL.

Code: Select all

$url = 'http://some_page_you_want_to_visit.com';
 
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //retrive output and do not display it
 
$content = curl_exec($ch);
 
However, this is probably against their terms of use.

Re: Read webpage source from fopen

Posted: Tue Dec 16, 2008 9:42 pm
by zerandib
Hello,

how to display the source then!
i hv tried like;
echo $content;

but gives nothing!

Re: Read webpage source from fopen

Posted: Tue Dec 16, 2008 9:45 pm
by John Cartwright
Do you have cURL lib installed? You can check using phpinfo().

Also, have to tried a different website?

More info please.