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
and also with the layout in your weblog. Is this a paid topic or did you customize it yourself?
Either way stay up the nice high quality writing, it's uncommon to see a
nice blog like this one today..
the structure to your weblog. Is this a paid topic or did you customize
it your self? Either way stay up the excellent high quality writing,
it's uncommon to look a nice blog like this one these days..
Your writing style has been amazed me. Thanks, quite nice article.
Do you know how to make your site mobile friendly? My web site
looks weird when viewing from my apple iphone.
I'm trying to find a template or plugin that
might be able to correct this problem. If you have any
recommendations, please share. Appreciate it!
strategies to your won weblog.
for your post. They're really convincing and can certainly
work. Still, the posts are very brief for starters.
May you please extend them a little from subsequent time?
Thanks for the post.
up posting these articles.
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



one and i was just wondering if you get a lot of spam remarks?
If so how do you protect against it, any plugin or anything you can recommend?
I get so much lately it's driving me insane so any support
is very much appreciated.