Can't read HTML from some urls !

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
voltmachine
Forum Newbie
Posts: 7
Joined: Tue Aug 26, 2008 1:30 am

Can't read HTML from some urls !

Post by voltmachine »

Hi

I try to open URL: http://911.bg . I see it in browser, but can't read HTML with PHP functions:

file() - return empty array
file_get_contents() - return empty string
fopen() - return empty string
I tried also curl - the same result.

I don't receive any errors from functions - only empty data. All other sites I tried work fine :roll: .

Can you help me read HTML from this site ?

Code: Select all

error_reporting(E_ALL);
 
echo $url = 'http://911.bg/a2/large.asp?parent=1010&act=0&loc=0&m=32';
echo '<br/>';
print_r(file($url));
 
echo(file_get_contents($url));
 
$fd = fopen($url, "r");
while (!feof($fd))  echo $buffer = fgets($fd, 4096);
fclose($fd);
 
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Re: Can't read HTML from some urls !

Post by panic! »

Another thing is the site you're trying to grab seems very slow, you may want to change 'default_socket_timeout' in your php.ini to a higher number, you may want to research other things that may be causing the socket to be timing out.
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: Can't read HTML from some urls !

Post by Ziq »

This site check User-agent (HTTP header) information. You must use socket to read content.

For example:

Code: Select all

 
<?php
 
define('N', "\r\n");
 
function get_contetn($hostname, $path)
{
    $line = "";
    
    $fp = fsockopen($hostname, 80, $errno, $errstr, 30);
    
    if (!$fp) echo "$errstr ($errno) <br>\r\n";
    else 
    {
        $headers = "GET $path HTTP/1.1".N;
        $headers .= "Host: $hostname".N;
        //  Mozilla Firefox 3
        $headers .= "User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; ru-RU) AppleWebKit/525.19 (KHTML, like Gecko) Version/3.1.2 Safari/525.21;".N;
        $headers .= "Connection: Close".N.N;
        
        fwrite($fp, $headers);
        
        while (!feof($fp))
        {
            $line .= fgets($fp, 1024);
        }
        fclose($fp);
    }
    return $line;
}
 
echo get_contetn('911.bg', '/a2/large.asp');
?>
 
Look at line 18.
voltmachine
Forum Newbie
Posts: 7
Joined: Tue Aug 26, 2008 1:30 am

Re: Can't read HTML from some urls !

Post by voltmachine »

Thanks Ziq :D , it works perfect :bow:
Post Reply