2010-09-01 5 views

Répondre

4

Vous pouvez utiliser file_get_contents ou cURL.

L'exemple suivant télécharge le code HTML de la page d'accueil de google.com et le montre à l'écran.

file_get_contents façon:

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

façon cURL:

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

Vous pouvez utiliser boucle

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

Il y a plusieurs approches que je vous suggère:

HTTP:
Si possible, utilisez les fonctions de flux de fichiers de PHP intégré (comme file_get_contents()) ou cURL pour télécharger le fichier à partir du serveur via la normale HTTP requests. Si vous voulez télécharger la source d'un fichier PHP, cependant, cela ne fonctionnerait pas (vous en obtiendriez plutôt la sortie). Un exemple:

<?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:
Si vous avez l'extension SSH2 installé, et vous avez un accès SSH au serveur, vous pouvez télécharger le fichier via SFTP (SSH protocole de transfert de fichiers):

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

FTP:
Si le serveur dispose d'un serveur FTP, vous avez accès, vous pouvez utiliser FTP ou FTPS (FTP sécurisé, si pris en charge) pour télécharger le fichier:

<?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'); 
Questions connexes