[FAQ] How do I retrieve a page from a web site?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chung Leong

    [FAQ] How do I retrieve a page from a web site?

    My contribution to the comp.lang.php FAQ:
    -------------------------------------------------------------

    Q: How do I retrieve a page from a web site?
    A: Pass a URL to file() or file_get_conten ts(). The former returns the
    contents as an array of lines. The latter returns the same as string.

    Example:

    $html = file_get_conten ts("http://www.cnn.com");


    Q: How do I retrieve a page from a web site that does browser detection?
    A: Use ini_set() to change the configuration option "user_agent ." This sets
    the User-Agent header sent by PHP.

    Example:

    ini_set('user_a gent', "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
    $html = file_get_conten ts("http://www.msn.com");


    Q: How do I retrieve a page from a web site that requires a cookie?
    A: Use stream_context_ create() to create a HTTP context with Cookie as one
    of the headers. Then, if you are coding in PHP 5, pass the context to file()
    or file_get_conten ts() as the third parameter. In PHP 4 either function
    accepts a context, so you need to open the URL with fopen() and retrieve the
    data a chunk at a time with fread().

    Example:

    $opts = array(
    'http'=>array(
    'method'=>"GET" ,
    'header'=>
    "Accept-language: en\r\n" .
    "Cookie: foo=bar\r\n"
    )
    );

    $context = stream_context_ create($opts);
    $f = fopen($url, "rb", false, $context);
    while($data = fread($f, 1024)) {
    echo $data;
    }

    stream_context_ create() is available in PHP 4.3.0 and above. If you are
    using an older version, you would need the cURL functions.

    [cURL example here]


Working...