[ Toutes les pages - php ]

Lire une page web à partir de son URL Préférences

Pargager sur facebook

Afficher le menu

Lecture simple d'une page

<?php
$url = 'http://www.monsite.com/mapage.php';                 ## https:// convient aussi
$http_page = file_get_contents($url);

$http_page_ = htmlspecialchars($http_page);                 ## Conversion des caractères spéciaux pour l'affichage
echo "<pre>$http_page_</pre>";
?>

Lecture simple d'une page et de ses header http

<?php
$url = 'http://www.ilu.be/';
$http_page = file_get_contents($url);

$http_result = 0;
$http_header = '';

if (($http_page != FALSE) && (isset($http_response_header))) {
	$nLines = count( $http_response_header );
	for ( $i = $nLines-1; $i >= 0; $i-- ) {
		$line = $http_response_header[$i];
		$http_header .= $line . "\n";
		if ( strncasecmp( "HTTP", $line, 4 ) == 0 ) {
			$response = explode( ' ', $line );
			$http_result = $response[1];
		}
	}
}
$http_page_ = htmlspecialchars($http_page);
if ($http_result != 0) echo "<pre>$http_page_\n\n$http_header\n\n$http_result</pre>";
?>

Poster des données

<?php
$url = 'http://www.monsite.com/mapage.php';

$post = array(
	'var1' => 'données 1' ,
	'var2 => 'données 2'
);

$postdata = http_build_query($post);

$opts = array( 'http' =>
	 array (
		'method' => 'POST',
		'header' => 'Content-type: application/x-www-form-urlencoded',
		'content' => $postdata
	 )
);

$context = stream_context_create($opts);

$http_page = file_get_contents($url,false,$context);
$http_result = 0;
$http_header = '';

if (($http_page != FALSE) && (isset($http_response_header))) {
	$nLines = count( $http_response_header );
	for ( $i = $nLines-1; $i >= 0; $i-- ) {
		$line = $http_response_header[$i];
		$http_header .= $line . "\n";
		if ( strncasecmp( "HTTP", $line, 4 ) == 0 ) {
			$response = explode( ' ', $line );
			$http_result = $response[1];
		}
	}
}

if ($http_result != 0) echo "<pre>$http_page\n\n$http_header\n\n$http_result</pre>";
?>

http_build_query: Génère une chaîne URL-Encodée à partir d'un tableau associatif. Par exemple, array('key1′ => `value1′, `key2′ => `value2′) devient "key1=value1&key2=value2″ .
Les données sont ainsi encodées pour le POST.

stream_context_create: Crée et retourne un flux contextuel à partir d'un tableau associatif, dans le format $arr['wrapper']['option'] = $value.
Le flux linéaire ainsi créé est transmis lors de la requête.

Page protégée par authentification

<?php
$url = 'http://www.monsite.com/mapage.php';
$nom = 'nom';
$pass = 'password';

$auth = base64_encode("$nom:$pass") . "\r\n";

$header = "Authorization: Basic $auth";

$opts = array( 'http' =>
	 array (
	 	'method' => 'GET',
		'header' => $header
	 )
);

$context = stream_context_create($opts);

$http_page = file_get_contents($url,false,$context);
$http_result = 0;
$http_header = '';

if (($http_page != FALSE) && (isset($http_response_header))) {
	$nLines = count( $http_response_header );
	for ( $i = $nLines-1; $i >= 0; $i-- ) {
		$line = $http_response_header[$i];
		$http_header .= $line . "\n";
		if ( strncasecmp( "HTTP", $line, 4 ) == 0 ) {
			$response = explode( ' ', $line );
			$http_result = $response[1];
		}
	}
}

if ($http_result != 0) echo "<pre>$http_page\n\n$http_header\n\n$http_result</pre>";
?>

Via un proxy

<?php
$url = 'http://www.monsite.com/mapage.php';

$opts = array( 'http' =>
	 array (
		'method' => 'GET',
		'proxy' => 'tcp://localhost:8080',
		'request_fulluri' => true
	 )
);

$context = stream_context_create($opts);

$http_page = file_get_contents($url,false,$context);
$http_result = 0;
$http_header = '';

if (($http_page != FALSE) && (isset($http_response_header))) {
	$nLines = count( $http_response_header );
	for ( $i = $nLines-1; $i >= 0; $i-- ) {
		$line = $http_response_header[$i];
		$http_header .= $line . "\n";
		if ( strncasecmp( "HTTP", $line, 4 ) == 0 ) {
			$response = explode( ' ', $line );
			$http_result = $response[1];
		}
	}
}

if ($http_result != 0) echo "<pre>$http_page\n\n$http_header\n\n$http_result</pre>";
?>

Le $header de 'header' => $header

Les différentes valeurs sont séparées par "\r\n".

Exemples:

"Accept-Encoding: gzip\r\n"
"Accept-Language: fr\r\n"
"Accept-Charset: iso-8859-1,*,utf-8\r\n"
"Authorization: Basic $auth\r\n"
"Content-type: application/x-www-form-urlencoded\r\n"
"Cookie: info=azerty\r\n"
"Content-length: strlen($post)\r\n"
"User-Agent: PHP Script\r\n"
"Content-Type: text/xml\r\n"
"Connection: close\r\n"
"Host: $host\r\n"
"Referer: http://$host\r\n"

Valid XHTML 1.0Strict Valid CSS

AccueilPage précédenteHaut de pagePage suivante