Read webpage source from fopen

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
zerandib
Forum Newbie
Posts: 16
Joined: Tue Dec 16, 2008 12:17 am

Read webpage source from fopen

Post 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!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Read webpage source from fopen

Post 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.
zerandib
Forum Newbie
Posts: 16
Joined: Tue Dec 16, 2008 12:17 am

Re: Read webpage source from fopen

Post by zerandib »

Hello,

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

but gives nothing!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Read webpage source from fopen

Post by John Cartwright »

Do you have cURL lib installed? You can check using phpinfo().

Also, have to tried a different website?

More info please.
Post Reply