1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| function recuperer_page_http($url, $timeout = 10, $nom_local = '', $post = NULL) {
if (extension_loaded('curl')) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
if ($nom_local) {
$fp = fopen($nom_local, 'w') or die("Le fichier '$nom_local' n'a pu être ouvert en écriture");
curl_setopt($ch, CURLOPT_FILE, $fp);
} else {
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
}
if ($post) {
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}
$ret = curl_exec($ch);
if ($nom_local) {
fclose($fp);
}
curl_close($ch);
if ($ret === FALSE) {
die("Une erreur a été rencontrée : " . curl_error());
}
return $ret;
} elseif (ini_get('allow_url_fopen') && in_array(parse_url($url, PHP_URL_SCHEME), stream_get_wrappers())) {
$infos = parse_url($url);
$contexte = NULL;
if (isset($infos['user']) || $post) {
$options = array(
'http' => array(
'method' => $post ? 'POST' : 'GET',
'header' => '',
'timeout' => $timeout // Effectif pour les versions 5.2.1 et supérieures
)
);
if (isset($infos['user'])) {
$options['http']['header'] .= 'Authorization: Basic ' . base64_encode($infos['user'] . ':' . $infos['pass']) . "\r\n";
}
if ($post) {
$data = http_build_query($post);
$options['http']['header'] .= "Content-type: application/x-www-form-urlencoded\r\n";
$options['http']['header'] .= 'Content-Length: ' . strlen($data) . "\r\n";
$options['http']['content'] = $data;
}
$contexte = stream_context_create($options);
}
if (!$nom_local) {
return file_get_contents($url, FALSE, $contexte);
} else {
$in = fopen($url, 'r', FALSE, $contexte);
stream_set_timeout($in, $timeout);
$out = fopen($nom_local, 'w') or die("Le fichier '$nom_local' n'a pu être ouvert en écriture");
while (!feof($in)) {
fwrite($out, fread($in, 1024));
}
fclose($in);
fclose($out);
}
} elseif (function_exists('fsockopen')) {
$infos = parse_url($url);
if ($nom_local) {
$out = fopen($nom_local, 'w') or die("Le fichier '$nom_local' n'a pu être ouvert en écriture");
}
if (empty($infos['path'])) {
$infos['path'] = '/';
}
if (!isset($infos['port'])) {
if ($infos['scheme'] == 'http') {
$infos['port'] = 80;
} elseif ($infos['scheme'] == 'https') {
$infos['port'] = 443;
}
}
@ $fp = fsockopen($infos['host'] == 'https' ? 'ssl://' . $infos['host'] : $infos['host'], $infos['port'], $errno, $errstr, $timeout);
if (!$fp) {
return FALSE;
}
stream_set_timeout($fp, $timeout);
$requete = ($post ? 'POST ' : 'GET ') . $infos['path'] . '?' . (isset($infos['query']) ? $infos['query'] : '') . " HTTP/1.1\r\n";
$requete .= 'Host: ' . $infos['host'] . ':' . (isset($infos['port']) ? $infos['port'] : '80') . "\r\n";
if (isset($infos['user'])) {
$requete .= 'Authorization: Basic ' . base64_encode($infos['user'] . ':' . $infos['pass']) . "\r\n";
}
if ($post) {
$data = http_build_query($post);
$requete .= "Content-type: application/x-www-form-urlencoded\r\n";
$requete .= 'Content-Length: ' . strlen($data) . "\r\n";
}
fwrite($fp, $requete . "\r\n" . $data);
$buffer = '';
do { // Saut de l'entête HTTP
$buffer .= fgets($fp, 1024);
} while (!feof($fp) && strpos($buffer, "\r\n\r\n") === FALSE);
$buffer = '';
while (!feof($fp)) {
$buffer .= fread($fp, 1024);
}
$buffer = ltrim($buffer);
$pos = strpos($buffer, "\r\n");
$len = hexdec(substr($buffer, 0, $pos));
$buffer = substr($buffer, $pos + strlen("\r\n"), $len);
if ($nom_local) {
fwrite($out, $buffer);
fclose($out);
$buffer = TRUE;
}
fclose($fp);
return $buffer;
} else {
die("Aucune méthode permettant de récupérer la source d'une page n'est disponible");
}
} |
Partager