252 |
jpm |
1 |
<?php
|
|
|
2 |
// declare(encoding='UTF-8');
|
|
|
3 |
/**
|
|
|
4 |
* Classe client permettant d'interroger des services web REST.
|
|
|
5 |
*
|
|
|
6 |
* @category php 5.2
|
|
|
7 |
* @package Framework
|
|
|
8 |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
|
|
|
9 |
* @copyright Copyright (c) 2010, Tela Botanica (accueil@tela-botanica.org)
|
|
|
10 |
* @license http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
|
|
|
11 |
* @license http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
|
|
|
12 |
* @version $Id: RestClient.php 348 2011-06-16 15:35:06Z jpm $
|
|
|
13 |
*/
|
287 |
jpm |
14 |
class RestClient {
|
252 |
jpm |
15 |
const HTTP_URL_REQUETE_SEPARATEUR = '&';
|
256 |
jpm |
16 |
private $http_methodes = array('GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'OPTIONS', 'CONNECT', 'TRACE');
|
252 |
jpm |
17 |
protected $parametres = null;
|
256 |
jpm |
18 |
private $url = null;
|
|
|
19 |
private $reponse_entetes = null;
|
252 |
jpm |
20 |
|
|
|
21 |
//+----------------------------------------------------------------------------------------------------------------+
|
|
|
22 |
// ACCESSEURS
|
|
|
23 |
|
256 |
jpm |
24 |
public function getReponseEntetes($cle) {
|
|
|
25 |
return $this->reponse_entetes;
|
|
|
26 |
}
|
|
|
27 |
|
252 |
jpm |
28 |
public function getParametre($cle) {
|
|
|
29 |
$valeur = (isset($this->parametres[$cle])) ? $this->parametres[$cle] : null;
|
|
|
30 |
return $valeur;
|
|
|
31 |
}
|
|
|
32 |
|
|
|
33 |
public function ajouterParametre($cle, $valeur) {
|
|
|
34 |
$this->parametres[$cle] = $valeur;
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
public function supprimerParametre($cle) {
|
|
|
38 |
unset($this->parametres[$cle]);
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
public function nettoyerParametres() {
|
|
|
42 |
$this->parametres = null;
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
//+----------------------------------------------------------------------------------------------------------------+
|
|
|
46 |
// MÉTHODES
|
|
|
47 |
|
|
|
48 |
public function consulter($url) {
|
|
|
49 |
$retour = $this->envoyerRequete($url, 'GET');
|
|
|
50 |
return $retour;
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
public function ajouter($url, Array $donnees) {
|
|
|
54 |
$retour = $this->envoyerRequete($url, 'PUT', $donnees);
|
|
|
55 |
return $retour;
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
public function modifier($url, Array $donnees) {
|
|
|
59 |
$retour = $this->envoyerRequete($url, 'POST', $donnees);
|
|
|
60 |
return $retour;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
public function supprimer($url) {
|
|
|
64 |
$retour = $this->envoyerRequete($url, 'DELETE');
|
|
|
65 |
return $retour;
|
|
|
66 |
}
|
|
|
67 |
|
256 |
jpm |
68 |
public function envoyerRequete($url, $mode, Array $donnees = array()) {
|
|
|
69 |
$this->url = $url;
|
252 |
jpm |
70 |
$contenu = false;
|
256 |
jpm |
71 |
if (! in_array($mode, $this->http_methodes)) {
|
252 |
jpm |
72 |
$e = "Le mode de requête '$mode' n'est pas accepté!";
|
|
|
73 |
trigger_error($e, E_USER_WARNING);
|
|
|
74 |
} else {
|
257 |
jpm |
75 |
if ($mode == 'GET') {
|
|
|
76 |
$this->traiterUrlParametres();
|
|
|
77 |
}
|
252 |
jpm |
78 |
$contexte = stream_context_create(array(
|
|
|
79 |
'http' => array(
|
|
|
80 |
'method' => $mode,
|
|
|
81 |
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
|
|
|
82 |
'content' => http_build_query($donnees, null, self::HTTP_URL_REQUETE_SEPARATEUR))));
|
|
|
83 |
$flux = @fopen($url, 'r', false, $contexte);
|
|
|
84 |
if (!$flux) {
|
256 |
jpm |
85 |
$this->reponse_entetes = $http_response_header;
|
252 |
jpm |
86 |
$e = "L'ouverture de l'url '$url' par la méthode HTTP '$mode' a échoué!";
|
|
|
87 |
trigger_error($e, E_USER_WARNING);
|
|
|
88 |
} else {
|
|
|
89 |
// Informations sur les en-têtes et métadonnées du flux
|
256 |
jpm |
90 |
$this->reponse_entetes = stream_get_meta_data($flux);
|
252 |
jpm |
91 |
|
|
|
92 |
// Contenu actuel de $url
|
|
|
93 |
$contenu = stream_get_contents($flux);
|
|
|
94 |
|
|
|
95 |
fclose($flux);
|
|
|
96 |
}
|
256 |
jpm |
97 |
$this->traiterEntete();
|
252 |
jpm |
98 |
}
|
|
|
99 |
$this->reinitialiser();
|
|
|
100 |
return $contenu;
|
|
|
101 |
}
|
|
|
102 |
|
257 |
jpm |
103 |
private function traiterUrlParametres() {
|
252 |
jpm |
104 |
$parametres = array();
|
|
|
105 |
if (count($this->parametres) > 0) {
|
|
|
106 |
foreach ($this->parametres as $cle => $valeur) {
|
348 |
jpm |
107 |
$parametres[] = $cle.self::HTTP_URL_REQUETE_SEPARATEUR.$valeur;
|
252 |
jpm |
108 |
}
|
|
|
109 |
$url_parametres = implode(self::HTTP_URL_REQUETE_SEPARATEUR, $parametres);
|
257 |
jpm |
110 |
$this->url = $this->url.'?'.$url_parametres;
|
252 |
jpm |
111 |
}
|
|
|
112 |
}
|
|
|
113 |
|
256 |
jpm |
114 |
private function traiterEntete() {
|
|
|
115 |
$infos = $this->analyserEntete();
|
252 |
jpm |
116 |
$this->traiterEnteteDebogage($infos);
|
|
|
117 |
}
|
|
|
118 |
|
256 |
jpm |
119 |
private function analyserEntete() {
|
|
|
120 |
$entetes = $this->reponse_entetes;
|
|
|
121 |
$infos = array('date' => null, 'uri' => $this->url, 'debogages' => null);
|
252 |
jpm |
122 |
|
|
|
123 |
if (isset($entetes['wrapper_data'])) {
|
|
|
124 |
$entetes = $entetes['wrapper_data'];
|
|
|
125 |
}
|
|
|
126 |
foreach ($entetes as $entete) {
|
|
|
127 |
if (preg_match('/^X_REST_DEBOGAGE_MESSAGES: (.+)$/', $entete, $match)) {
|
|
|
128 |
$infos['debogages'] = json_decode($match[1]);
|
|
|
129 |
}
|
|
|
130 |
if (preg_match('/^Date: .+ ([012][0-9]:[012345][0-9]:[012345][0-9]) .*$/', $entete, $match)) {
|
|
|
131 |
$infos['date'] = $match[1];
|
|
|
132 |
}
|
|
|
133 |
}
|
|
|
134 |
return $infos;
|
|
|
135 |
}
|
|
|
136 |
|
256 |
jpm |
137 |
private function traiterEnteteDebogage($entetes_analyses) {
|
252 |
jpm |
138 |
if (isset($entetes['debogages'])) {
|
|
|
139 |
$date = $entetes['date'];
|
|
|
140 |
$uri = $entetes['uri'];
|
|
|
141 |
$debogages = $entetes['debogages'];
|
|
|
142 |
foreach ($debogages as $debogage) {
|
|
|
143 |
$e = "DEBOGAGE : $date - $uri :\n$debogage";
|
|
|
144 |
trigger_error($e, E_USER_NOTICE);
|
|
|
145 |
}
|
|
|
146 |
}
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
private function reinitialiser() {
|
|
|
150 |
$this->nettoyerParametres();
|
|
|
151 |
}
|
|
|
152 |
}
|