开发者

Read file from a different website using a php script

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


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>";


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:password@www.example.com/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:password@ssh.example.com/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:password@ftp.example.com/path/to/file');

// FTPS with username and password:
$file = file_get_contents('ftps://user:password@ftp.example.com/path/to/file');


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜