252 |
jpm |
1 |
<?php
|
|
|
2 |
// declare(encoding='UTF-8');
|
|
|
3 |
/**
|
|
|
4 |
* Classe client permettant d'interroger des services web REST.
|
|
|
5 |
*
|
473 |
jpm |
6 |
* @category php 5.2
|
|
|
7 |
* @package Framework
|
252 |
jpm |
8 |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
|
|
|
9 |
* @copyright Copyright (c) 2010, Tela Botanica (accueil@tela-botanica.org)
|
473 |
jpm |
10 |
* @license CeCILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt>
|
|
|
11 |
* @license GNU-GPL <http://www.gnu.org/licenses/gpl.html>
|
|
|
12 |
* @since 0.2
|
252 |
jpm |
13 |
*/
|
287 |
jpm |
14 |
class RestClient {
|
252 |
jpm |
15 |
const HTTP_URL_REQUETE_SEPARATEUR = '&';
|
349 |
jpm |
16 |
const HTTP_URL_REQUETE_CLE_VALEUR_SEPARATEUR = '=';
|
256 |
jpm |
17 |
private $http_methodes = array('GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'OPTIONS', 'CONNECT', 'TRACE');
|
252 |
jpm |
18 |
protected $parametres = null;
|
256 |
jpm |
19 |
private $url = null;
|
|
|
20 |
private $reponse_entetes = null;
|
473 |
jpm |
21 |
|
|
|
22 |
//+------------------------------------------------------------------------------------------------------+
|
252 |
jpm |
23 |
// ACCESSEURS
|
473 |
jpm |
24 |
|
353 |
jpm |
25 |
public function getReponseEntetes() {
|
256 |
jpm |
26 |
return $this->reponse_entetes;
|
|
|
27 |
}
|
473 |
jpm |
28 |
|
252 |
jpm |
29 |
public function getParametre($cle) {
|
|
|
30 |
$valeur = (isset($this->parametres[$cle])) ? $this->parametres[$cle] : null;
|
|
|
31 |
return $valeur;
|
|
|
32 |
}
|
473 |
jpm |
33 |
|
252 |
jpm |
34 |
public function ajouterParametre($cle, $valeur) {
|
|
|
35 |
$this->parametres[$cle] = $valeur;
|
|
|
36 |
}
|
473 |
jpm |
37 |
|
252 |
jpm |
38 |
public function supprimerParametre($cle) {
|
|
|
39 |
unset($this->parametres[$cle]);
|
|
|
40 |
}
|
473 |
jpm |
41 |
|
252 |
jpm |
42 |
public function nettoyerParametres() {
|
|
|
43 |
$this->parametres = null;
|
|
|
44 |
}
|
473 |
jpm |
45 |
|
|
|
46 |
//+------------------------------------------------------------------------------------------------------+
|
252 |
jpm |
47 |
// MÉTHODES
|
473 |
jpm |
48 |
|
252 |
jpm |
49 |
public function consulter($url) {
|
|
|
50 |
$retour = $this->envoyerRequete($url, 'GET');
|
|
|
51 |
return $retour;
|
|
|
52 |
}
|
473 |
jpm |
53 |
|
252 |
jpm |
54 |
public function ajouter($url, Array $donnees) {
|
|
|
55 |
$retour = $this->envoyerRequete($url, 'PUT', $donnees);
|
|
|
56 |
return $retour;
|
|
|
57 |
}
|
473 |
jpm |
58 |
|
252 |
jpm |
59 |
public function modifier($url, Array $donnees) {
|
|
|
60 |
$retour = $this->envoyerRequete($url, 'POST', $donnees);
|
|
|
61 |
return $retour;
|
|
|
62 |
}
|
473 |
jpm |
63 |
|
252 |
jpm |
64 |
public function supprimer($url) {
|
|
|
65 |
$retour = $this->envoyerRequete($url, 'DELETE');
|
|
|
66 |
return $retour;
|
|
|
67 |
}
|
473 |
jpm |
68 |
|
|
|
69 |
public function envoyerRequete($url, $mode, Array $donnees = array()) {
|
|
|
70 |
$contenu = false;
|
|
|
71 |
if (FALSE && function_exists('curl_init') && $mode == 'GET') {
|
|
|
72 |
// nous n'activons le wrapper que pour GET pour l'instant
|
|
|
73 |
// car l'utilisation de curl pour les autres modes pourrait
|
|
|
74 |
// vraisemblablement induire des comportements différents. (test-suite needed)
|
|
|
75 |
$contenu = $this->envoyerCurlRequete($url, $mode, $donnees);
|
|
|
76 |
} else {
|
|
|
77 |
$contenu = $this->envoyerStreamRequete($url, $mode, $donnees);
|
|
|
78 |
}
|
|
|
79 |
return $contenu;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
private function envoyerStreamRequete($url, $mode, Array $donnees = array()) {
|
428 |
raphael |
83 |
$this->url = $url;
|
|
|
84 |
$contenu = false;
|
|
|
85 |
if (! in_array($mode, $this->http_methodes)) {
|
|
|
86 |
$e = "Le mode de requête '$mode' n'est pas accepté!";
|
|
|
87 |
trigger_error($e, E_USER_WARNING);
|
|
|
88 |
} else {
|
|
|
89 |
if ($mode == 'GET') {
|
|
|
90 |
$this->traiterUrlParametres();
|
|
|
91 |
}
|
473 |
jpm |
92 |
$content = http_build_query($donnees, null, self::HTTP_URL_REQUETE_SEPARATEUR);
|
|
|
93 |
$contexte = stream_context_create(array(
|
|
|
94 |
'http' => array(
|
|
|
95 |
'method' => $mode,
|
|
|
96 |
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
|
|
|
97 |
'content' => $content)));
|
|
|
98 |
$flux = @fopen($this->url, 'r', false, $contexte);
|
|
|
99 |
if (!$flux) {
|
|
|
100 |
$entetesFmt = print_r($this->analyserEntete(), true);
|
|
|
101 |
$e = "Echec requête '$mode' : {$this->url}\n".
|
|
|
102 |
"Paramètres requête : $content \n".
|
|
|
103 |
"Entêtes réponse : $entetesFmt \n";
|
|
|
104 |
trigger_error($e, E_USER_WARNING);
|
|
|
105 |
} else {
|
|
|
106 |
// Informations sur les en-têtes et métadonnées du flux
|
|
|
107 |
$this->reponse_entetes = stream_get_meta_data($flux);
|
428 |
raphael |
108 |
|
473 |
jpm |
109 |
// Contenu actuel de $url
|
|
|
110 |
$contenu = stream_get_contents($flux);
|
|
|
111 |
|
|
|
112 |
fclose($flux);
|
|
|
113 |
}
|
|
|
114 |
$this->traiterEntete();
|
|
|
115 |
}
|
|
|
116 |
$this->reinitialiser();
|
|
|
117 |
return $contenu;
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
private function envoyerCurlRequete($url, $mode, Array $donnees = array()) {
|
|
|
121 |
$this->url = $url;
|
|
|
122 |
$contenu = false;
|
|
|
123 |
if (! in_array($mode, $this->http_methodes)) {
|
|
|
124 |
$e = "Le mode de requête '$mode' n'est pas accepté!";
|
|
|
125 |
trigger_error($e, E_USER_WARNING);
|
|
|
126 |
} else {
|
|
|
127 |
if ($mode == 'GET') {
|
|
|
128 |
$this->traiterUrlParametres();
|
|
|
129 |
}
|
|
|
130 |
|
428 |
raphael |
131 |
$ch = curl_init($this->url);
|
|
|
132 |
curl_setopt($ch, CURLOPT_HEADER, TRUE);
|
|
|
133 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
|
438 |
raphael |
134 |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
|
473 |
jpm |
135 |
if ($mode == 'POST') {
|
|
|
136 |
curl_setopt($ch, CURLOPT_POST, TRUE);
|
|
|
137 |
} elseif ($mode == 'PUT') {
|
|
|
138 |
curl_setopt($ch, CURLOPT_PUT, TRUE);
|
|
|
139 |
} elseif($mode == 'DELETE') {
|
|
|
140 |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
|
|
|
141 |
}
|
428 |
raphael |
142 |
|
473 |
jpm |
143 |
$content = '';
|
|
|
144 |
if ($donnees) {
|
|
|
145 |
$content = http_build_query($donnees, null, self::HTTP_URL_REQUETE_SEPARATEUR);
|
|
|
146 |
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
|
|
|
147 |
}
|
428 |
raphael |
148 |
|
|
|
149 |
$flux = curl_exec($ch);
|
|
|
150 |
curl_close($ch);
|
|
|
151 |
|
|
|
152 |
if (!$flux) {
|
|
|
153 |
$this->reponse_entetes = $http_response_header;
|
473 |
jpm |
154 |
$entetesFmt = print_r($this->analyserEntete(), true);
|
|
|
155 |
$msgTpl = "Echec requête (CURL) '%s' : %s\nParamètres requête : %s \nEntêtes réponse : %s \n";
|
|
|
156 |
$msg = sprintf($msgTpl, strtoupper($mode), $this->url, $content, $entetesFmt);
|
|
|
157 |
trigger_error($msg, E_USER_WARNING);
|
428 |
raphael |
158 |
} else {
|
438 |
raphael |
159 |
// attention, CURLOPT_FOLLOWLOCATION amène le stream à contenir plusieurs section d'header HTTP successives
|
|
|
160 |
$t = explode("\r\n\r\n", $flux);
|
|
|
161 |
$contenu = array_splice($t, -1);
|
|
|
162 |
$dernier_entete = array_splice($t, -1);
|
|
|
163 |
$contenu = $contenu[0];
|
|
|
164 |
$dernier_entete = $dernier_entete[0];
|
|
|
165 |
|
428 |
raphael |
166 |
// XXX: mimic stream_get_meta_data() (ce qui n'est pas très propre, le code appelant ferait mieux de se mettre à jour)
|
438 |
raphael |
167 |
$this->reponse_entetes = array('wrapper_data' => explode("\r\n", $dernier_entete));
|
428 |
raphael |
168 |
}
|
|
|
169 |
$this->traiterEntete();
|
|
|
170 |
}
|
|
|
171 |
$this->reinitialiser();
|
|
|
172 |
return $contenu;
|
|
|
173 |
}
|
|
|
174 |
|
257 |
jpm |
175 |
private function traiterUrlParametres() {
|
252 |
jpm |
176 |
$parametres = array();
|
|
|
177 |
if (count($this->parametres) > 0) {
|
|
|
178 |
foreach ($this->parametres as $cle => $valeur) {
|
350 |
jpm |
179 |
$cle = rawurlencode($cle);
|
|
|
180 |
$valeur = rawurlencode($valeur);
|
349 |
jpm |
181 |
$parametres[] = $cle.self::HTTP_URL_REQUETE_CLE_VALEUR_SEPARATEUR.$valeur;
|
252 |
jpm |
182 |
}
|
|
|
183 |
$url_parametres = implode(self::HTTP_URL_REQUETE_SEPARATEUR, $parametres);
|
257 |
jpm |
184 |
$this->url = $this->url.'?'.$url_parametres;
|
252 |
jpm |
185 |
}
|
|
|
186 |
}
|
473 |
jpm |
187 |
|
256 |
jpm |
188 |
private function traiterEntete() {
|
|
|
189 |
$infos = $this->analyserEntete();
|
252 |
jpm |
190 |
$this->traiterEnteteDebogage($infos);
|
|
|
191 |
}
|
473 |
jpm |
192 |
|
256 |
jpm |
193 |
private function analyserEntete() {
|
473 |
jpm |
194 |
$entetes = $this->reponse_entetes;
|
256 |
jpm |
195 |
$infos = array('date' => null, 'uri' => $this->url, 'debogages' => null);
|
473 |
jpm |
196 |
|
252 |
jpm |
197 |
if (isset($entetes['wrapper_data'])) {
|
|
|
198 |
$entetes = $entetes['wrapper_data'];
|
|
|
199 |
}
|
|
|
200 |
foreach ($entetes as $entete) {
|
|
|
201 |
if (preg_match('/^X_REST_DEBOGAGE_MESSAGES: (.+)$/', $entete, $match)) {
|
|
|
202 |
$infos['debogages'] = json_decode($match[1]);
|
|
|
203 |
}
|
|
|
204 |
if (preg_match('/^Date: .+ ([012][0-9]:[012345][0-9]:[012345][0-9]) .*$/', $entete, $match)) {
|
|
|
205 |
$infos['date'] = $match[1];
|
|
|
206 |
}
|
|
|
207 |
}
|
|
|
208 |
return $infos;
|
|
|
209 |
}
|
473 |
jpm |
210 |
|
|
|
211 |
private function traiterEnteteDebogage($entetes) {
|
252 |
jpm |
212 |
if (isset($entetes['debogages'])) {
|
|
|
213 |
$date = $entetes['date'];
|
|
|
214 |
$uri = $entetes['uri'];
|
|
|
215 |
$debogages = $entetes['debogages'];
|
|
|
216 |
foreach ($debogages as $debogage) {
|
|
|
217 |
$e = "DEBOGAGE : $date - $uri :\n$debogage";
|
|
|
218 |
trigger_error($e, E_USER_NOTICE);
|
|
|
219 |
}
|
|
|
220 |
}
|
|
|
221 |
}
|
473 |
jpm |
222 |
|
252 |
jpm |
223 |
private function reinitialiser() {
|
|
|
224 |
$this->nettoyerParametres();
|
|
|
225 |
}
|
|
|
226 |
}
|