2

How can I read the content of a file from a completely different server, then display the content. I will later change the code to use the returned information in th proper manner.

1
  • Is this file protected in some way or can you just make an http request to it? Commented Sep 1, 2010 at 17:44

3 Answers 3

4

You can use file_get_contents or cURL.

Following example downloads the HTML of home page of google.com and shows it on screen.

file_get_contents way:

$data = file_get_contents("http://www.google.com/");
echo "<pre>" . $data . "</pre>";

cURL way:

function get_web_page( $url )
{
    $options = array(
        CURLOPT_RETURNTRANSFER => true,     // return web page
        CURLOPT_HEADER         => false,    // don't return headers
        CURLOPT_FOLLOWLOCATION => true,     // follow redirects
        CURLOPT_ENCODING       => "",       // handle all encodings
        CURLOPT_AUTOREFERER    => true,     // set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
        CURLOPT_TIMEOUT        => 120,      // timeout on response
        CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
    );

    $ch      = curl_init( $url );
    curl_setopt_array( $ch, $options );
    $content = curl_exec( $ch );
    $err     = curl_errno( $ch );
    $errmsg  = curl_error( $ch );
    $header  = curl_getinfo( $ch );
    curl_close( $ch );

    $header['errno']   = $err;
    $header['errmsg']  = $errmsg;
    $header['content'] = $content;
    return $header;
}

//Now get the webpage
$data = get_web_page( "https://www.google.com/" );

//Display the data (optional)
echo "<pre>" . $data['content'] . "</pre>";
Sign up to request clarification or add additional context in comments.

Comments

1

There are several approaches I'd suggest:

HTTP:
If possible, use either PHP's built-in file stream functions (such as file_get_contents()) or cURL to download the file from the server through normal HTTP requests. If you want to download the source of a PHP file, however, this would not work (you would instead get the output of it). An example:

<?php
// Most basic HTTP request
$file = file_get_contents('http://www.example.com/path/to/file');
// HTTP request with a username and password
$file = file_get_contents('http://user:[email protected]/path/to/file');
// HTTPS request
$file = file_get_contents('https://www.example.com/path/to/file');

SSH:
If you have the SSH2 extension installed, and you have SSH access to the server, you might want to download the file through SFTP (SSH file transfer protocol):

<?php
// Use the SFTP stream wrapper to download files through SFTP:
$file = file_get_contents('ssh2.sftp://user:[email protected]/path/to/file');

FTP:
If the server has a FTP server you have access to, you might want to use FTP or FTPS (secure FTP, if supported) to download the file:

<?php
// Use the FTP stream wrapper to download files through FTP or SFTP

// Anonymous FTP:
$file = file_get_contents('ftp://ftp.example.com/path/to/file');

// FTP with username and password:
$file = file_get_contents('ftp://user:[email protected]/path/to/file');

// FTPS with username and password:
$file = file_get_contents('ftps://user:[email protected]/path/to/file');

Comments

0

You can use curl

$ch = curl_init("http://www.google.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content_of_page = curl_exec($ch);
curl_close($ch);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.