Line 7... |
Line 7... |
7 |
* @package Framework
|
7 |
* @package Framework
|
8 |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
|
8 |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
|
9 |
* @copyright Copyright (c) 2010, Tela Botanica (accueil@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
|
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
|
11 |
* @license http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
|
12 |
* @version $Id: RestClient.php 353 2011-08-01 16:02:32Z jpm $
|
12 |
* @version $Id: RestClient.php 428 2013-06-28 13:12:36Z raphael $
|
13 |
*/
|
13 |
*/
|
14 |
class RestClient {
|
14 |
class RestClient {
|
15 |
const HTTP_URL_REQUETE_SEPARATEUR = '&';
|
15 |
const HTTP_URL_REQUETE_SEPARATEUR = '&';
|
16 |
const HTTP_URL_REQUETE_CLE_VALEUR_SEPARATEUR = '=';
|
16 |
const HTTP_URL_REQUETE_CLE_VALEUR_SEPARATEUR = '=';
|
17 |
private $http_methodes = array('GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'OPTIONS', 'CONNECT', 'TRACE');
|
17 |
private $http_methodes = array('GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'OPTIONS', 'CONNECT', 'TRACE');
|
Line 64... |
Line 64... |
64 |
public function supprimer($url) {
|
64 |
public function supprimer($url) {
|
65 |
$retour = $this->envoyerRequete($url, 'DELETE');
|
65 |
$retour = $this->envoyerRequete($url, 'DELETE');
|
66 |
return $retour;
|
66 |
return $retour;
|
67 |
}
|
67 |
}
|
Line -... |
Line 68... |
- |
|
68 |
|
- |
|
69 |
public function envoyerCurlRequete($url, $mode, Array $donnees = array()) {
|
- |
|
70 |
$this->url = $url;
|
- |
|
71 |
$contenu = false;
|
- |
|
72 |
if (! in_array($mode, $this->http_methodes)) {
|
- |
|
73 |
$e = "Le mode de requête '$mode' n'est pas accepté!";
|
- |
|
74 |
trigger_error($e, E_USER_WARNING);
|
- |
|
75 |
} else {
|
- |
|
76 |
if ($mode == 'GET') {
|
- |
|
77 |
$this->traiterUrlParametres();
|
- |
|
78 |
}
|
- |
|
79 |
|
- |
|
80 |
$ch = curl_init($this->url);
|
- |
|
81 |
curl_setopt($ch, CURLOPT_HEADER, TRUE);
|
- |
|
82 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
|
- |
|
83 |
|
- |
|
84 |
if($mode == 'POST') curl_setopt($ch, CURLOPT_POST, TRUE);
|
- |
|
85 |
elseif($mode == 'PUT') curl_setopt($ch, CURLOPT_PUT, TRUE);
|
- |
|
86 |
elseif($mode == 'DELETE') curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
|
- |
|
87 |
|
- |
|
88 |
if($donnees) curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($donnees, null, self::HTTP_URL_REQUETE_SEPARATEUR));
|
- |
|
89 |
|
- |
|
90 |
// curl_setopt($ch, CURLOPT_COOKIE, "XDEBUG_PROFILE"); // debug
|
- |
|
91 |
$flux = curl_exec($ch);
|
- |
|
92 |
curl_close($ch);
|
- |
|
93 |
|
- |
|
94 |
if (!$flux) {
|
- |
|
95 |
$this->reponse_entetes = $http_response_header;
|
- |
|
96 |
$e = "L'ouverture de l'url '{$this->url}' par la méthode HTTP '$mode' a échoué!";
|
- |
|
97 |
trigger_error($e, E_USER_WARNING);
|
- |
|
98 |
} else {
|
- |
|
99 |
list($this->reponse_entetes, $contenu) = explode("\r\n\r\n", $flux, 2);
|
- |
|
100 |
// XXX: mimic stream_get_meta_data() (ce qui n'est pas très propre, le code appelant ferait mieux de se mettre à jour)
|
- |
|
101 |
$this->reponse_entetes = array('wrapper_data' => explode("\r\n", $this->reponse_entetes));
|
- |
|
102 |
}
|
- |
|
103 |
$this->traiterEntete();
|
- |
|
104 |
}
|
- |
|
105 |
$this->reinitialiser();
|
- |
|
106 |
return $contenu;
|
- |
|
107 |
}
|
68 |
|
108 |
|
- |
|
109 |
public function envoyerRequete($url, $mode, Array $donnees = array()) {
|
- |
|
110 |
// debug: error_log("$mode {$this->url}");
|
- |
|
111 |
|
- |
|
112 |
// nous n'activons le wrapper que pour GET pour l'instant
|
- |
|
113 |
// car l'utilisation de curl pour les autres modes pourrait
|
- |
|
114 |
// vraisemblablement induire des comportements différents. (test-suite needed)
|
- |
|
115 |
if(function_exists('curl_init') && $mode == 'GET') {
|
- |
|
116 |
return $this->envoyerCurlRequete($url, $mode, $donnees);
|
- |
|
117 |
}
|
69 |
public function envoyerRequete($url, $mode, Array $donnees = array()) {
|
118 |
|
70 |
$this->url = $url;
|
119 |
$this->url = $url;
|
71 |
$contenu = false;
|
120 |
$contenu = false;
|
72 |
if (! in_array($mode, $this->http_methodes)) {
|
121 |
if (! in_array($mode, $this->http_methodes)) {
|
73 |
$e = "Le mode de requête '$mode' n'est pas accepté!";
|
122 |
$e = "Le mode de requête '$mode' n'est pas accepté!";
|