fopen() unable to open some URLs?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Ferrier

    fopen() unable to open some URLs?

    Hi,

    I've used fopen() extensively to open web pages. I've found that there is a
    small minority
    of web pages that open fine in a browser, but are inaccessible using
    fopen(). Here are
    two such URLs:

    See why Homes.com is the #1 fastest growing real estate search site. Use our new neighborhood & school search to find your perfect home.



    For example, using the code below, almost every web page that will open in a
    browser
    will be read correctly by this code. There are few exceptions, such as the
    above URLs.
    I'm wondering why/how could a page be accessible to a browser but not to
    fopen()?

    Here's the code:

    <?php

    $page = "";

    if (isset($url))
    {
    $fp = fopen($url, 'r');

    if ($fp != false)
    {
    while (feof($fp) == false)
    {
    $page .= fread($fp, 10000);
    }

    fclose($fp);

    echo "URL: $url =============== =============== =======<br><br> ";
    echo $page;
    }
    }

    ?>

    I have this code running with a simple form interface here:



    Any ideas?

    Thank you,

    -Michael






  • Janwillem Borleffs

    #2
    Re: fopen() unable to open some URLs?

    Michael Ferrier wrote:[color=blue]
    > For example, using the code below, almost every web page that will
    > open in a browser
    > will be read correctly by this code. There are few exceptions, such
    > as the above URLs.
    > I'm wondering why/how could a page be accessible to a browser but not
    > to fopen()?
    >[/color]

    Some websites require an explicit user-agent header in order to return a
    response, as is the case with homes.com

    There are several ways to include the user agent, of which the easiest is to
    apply the ini_set function as follows:

    ini_set('user_a gent','Mozilla' );
    $fp = fopen('http://www.homes.com/','r');
    fpassthru($fp);

    When you run this code, you will see it works fine for homes.com. Bare in
    mind that this doesn't work for sites which require more then a user agent
    string to operate.

    JW



    Comment

    Working...