Voulant une fois pour toutes pouvoir gérer correctement les appels à des pages distantes via HTTP, j'ai décidé de me pencher sur la librairie cURL. Une des premières étapes que je souhaitais accomplir était d'extraire les en-têtes HTTP afin d'obtenir une indication sur le déroulement de la requête HTTP demandée. Une première approche permet de réaliser ceci très simplement grâce à l'option de transmission CURLOPT_HEADER :
<?php $url = curl_init(); curl_setopt($url, CURLOPT_URL, 'http://www.google.com'); curl_setopt($url, CURLOPT_RETURNTRANSFER, true); curl_setopt($url, CURLOPT_HEADER, true); $page = curl_exec($url); curl_close($url); ?>
L'inconvénient majeur de cette méthode est que les en-têtes HTTP et le contenu de la page demandée se retrouvent concaténés dans le résultat de curl_exec(). Effectuer une décomposition manuelle du résultat obtenu pourrait fournir le résultat attendu, mais c'est alors omettre une solution plus propre offerte par la librairie. Elle propose en effet un mécanisme de fonctions "callbacks" appelées lorsqu'un évènement particulier se produit. Dans le cas qui nous intéresse ici, l'option CURLOPT_HEADERFUNCTION permet d'appeler une fonction à chaque en-tête HTTP rencontré. Attention que cette fonction doit absolument retourner le nombre d'octets de l'en-tête reçu en paramètre.
<?php
function read_header($url, $str) {
echo 'Header : '.$str."\n";
return strlen($str);
}
$url = curl_init();
curl_setopt($url, CURLOPT_URL, 'http://www.google.com');
curl_setopt($url, CURLOPT_RETURNTRANSFER, true);
curl_setopt($url, CURLOPT_HEADER, true);
curl_setopt($url, CURLOPT_HEADERFUNCTION, 'read_header');
$page = curl_exec($url);
curl_close($url);
?>
Sur base de ce principe, on peut alors se construire rapidement une petit classe, histoire d'encapsuler ces traitements dans un objet :
<?php
/**
* A sample class to read HTTP headers
* @author Geoffray Warnants - http://www.geoffray.be
*/
class HTTPReader {
protected $_url = null;
protected $_headers = array();
protected $_body = '';
public function __construct($url) {
$this->_url = curl_init($url);
curl_setopt($this->_url, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->_url, CURLOPT_HEADER, true);
curl_setopt($this->_url, CURLOPT_HEADERFUNCTION,
array($this, 'readHeaders')
);
}
public function __destruct() {
curl_close($this->_url);
}
public function getHeaders() {
$this->_body = curl_exec($this->_url);
return $this->_headers;
}
public function getBody() {
return $this->_body;
}
protected function readHeaders($url, $str) {
if (strlen($str) > 0) {
$this->_headers[] = $str;
}
return strlen($str);
}
}
?>
Vos commentaires
Is there any way you can remove people from that service?
Many thanks!
if so after that you will absolutely obtain fastidious experience.
The overall look of your website is great, let alone the content!
Between your wit and your videos, I was almost moved to
start my own blog (well, almost...HaHa!) Wonderful job.
I really loved what you had to say, and more than that,
how you presented it. Too cool!
your views are pleasant for new people.
board and I find It really useful & it helped me out much.
I hope to give something back and help others like
you helped me.
keep up posting such content.
Continue the good work!
A bookmarquer.
PS : merci Seebz et Charles pour les compléments.
NICKEL
file_get_contents("http://www.mapage.fr");
foreach ($http_response_header as $i)
{
echo '<br>'.$i ;
}
Exemple :
<?php file_get_contents("http://example.com");
var_dump($http_response_header);
Réagir à cet article



up posting these articles.