Subversion Repositories Applications.framework

Rev

Rev 428 | Rev 439 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 428 Rev 438
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 428 2013-06-28 13:12:36Z raphael $
12
* @version		$Id: RestClient.php 438 2013-10-11 15:56:08Z 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 78... Line 78...
78
			}
78
			}
Line 79... Line 79...
79
 
79
 
80
			$ch = curl_init($this->url);
80
			$ch = curl_init($this->url);
81
			curl_setopt($ch, CURLOPT_HEADER, TRUE);
81
			curl_setopt($ch, CURLOPT_HEADER, TRUE);
-
 
82
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
Line 82... Line 83...
82
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
83
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
83
 
84
 
84
			if($mode == 'POST') curl_setopt($ch, CURLOPT_POST, TRUE);
85
			if($mode == 'POST') curl_setopt($ch, CURLOPT_POST, TRUE);
Line 85... Line 86...
85
			elseif($mode == 'PUT') curl_setopt($ch, CURLOPT_PUT, TRUE);
86
			elseif($mode == 'PUT') curl_setopt($ch, CURLOPT_PUT, TRUE);
Line 86... Line 87...
86
			elseif($mode == 'DELETE') curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
87
			elseif($mode == 'DELETE') curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
-
 
88
 
-
 
89
			if($donnees) curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($donnees, null, self::HTTP_URL_REQUETE_SEPARATEUR));
87
 
90
 
88
			if($donnees) curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($donnees, null, self::HTTP_URL_REQUETE_SEPARATEUR));
91
			/* if(isset($_COOKIES["XDEBUG_PROFILE"])) curl_setopt($ch, CURLOPT_COOKIE, "XDEBUG_PROFILE");
Line 89... Line 92...
89
 
92
			$caller = self::getCaller(debug_backtrace());
90
			// curl_setopt($ch, CURLOPT_COOKIE, "XDEBUG_PROFILE"); // debug
93
			@curl_setopt($ch, CURLOPT_REFERER, sprintf("%s[%s:%d:%s]", $_SERVER['REQUEST_URI'], $caller['file'], $caller['line'], $caller['function'])); // $_SERVER['QUERY_STRING']; // debug */
91
			$flux = curl_exec($ch);
94
			$flux = curl_exec($ch);
92
			curl_close($ch);
95
			curl_close($ch);
93
 
96
 
-
 
97
			if (!$flux) {
94
			if (!$flux) {
98
				$this->reponse_entetes = $http_response_header;
-
 
99
				trigger_error(sprintf("fail: %s \"%s\" (%s)", strtoupper($mode), $this->url, http_build_query($donnees, null, self::HTTP_URL_REQUETE_SEPARATEUR)),
-
 
100
							  E_USER_WARNING);
-
 
101
			} else {
-
 
102
				// attention, CURLOPT_FOLLOWLOCATION amène le stream à contenir plusieurs section d'header HTTP successives
-
 
103
				$t = explode("\r\n\r\n", $flux);
95
				$this->reponse_entetes = $http_response_header;
104
				$contenu = array_splice($t, -1);
96
				$e = "L'ouverture de l'url '{$this->url}' par la méthode HTTP '$mode' a échoué!";
105
				$dernier_entete = array_splice($t, -1);
97
				trigger_error($e, E_USER_WARNING);
106
				$contenu = $contenu[0];
98
			} else {
107
				$dernier_entete = $dernier_entete[0];
99
				list($this->reponse_entetes, $contenu) = explode("\r\n\r\n", $flux, 2);
108
 
100
				// XXX: mimic stream_get_meta_data() (ce qui n'est pas très propre, le code appelant ferait mieux de se mettre à jour)
109
				// 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));
110
				$this->reponse_entetes = array('wrapper_data' => explode("\r\n", $dernier_entete));
102
			}
111
			}
Line -... Line 112...
-
 
112
			$this->traiterEntete();
-
 
113
		}
-
 
114
		$this->reinitialiser();
-
 
115
		return $contenu;
-
 
116
	}
103
			$this->traiterEntete();
117
 
104
		}
118
	static function getCaller($trace) {
Line 105... Line 119...
105
		$this->reinitialiser();
119
		foreach($trace as $v) if(strpos($v['file'], '/modules/') !== false) return $v;
106
		return $contenu;
120
		return NULL;
107
	}
121
	}
108
 
122
 
Line 131... Line 145...
131
					'header' => "Content-type: application/x-www-form-urlencoded\r\n",
145
					'header' => "Content-type: application/x-www-form-urlencoded\r\n",
132
      				'content' => http_build_query($donnees, null, self::HTTP_URL_REQUETE_SEPARATEUR))));
146
      				'content' => http_build_query($donnees, null, self::HTTP_URL_REQUETE_SEPARATEUR))));
133
			$flux = @fopen($this->url, 'r', false, $contexte);
147
			$flux = @fopen($this->url, 'r', false, $contexte);
134
			if (!$flux) {
148
			if (!$flux) {
135
				$this->reponse_entetes = $http_response_header;
149
				$this->reponse_entetes = $http_response_header;
136
				$e = "L'ouverture de l'url '{$this->url}' par la méthode HTTP '$mode' a échoué!";
150
				trigger_error(sprintf("fail: %s \"%s\" (%s)", strtoupper($mode), $this->url, http_build_query($donnees, null, self::HTTP_URL_REQUETE_SEPARATEUR)),
137
				trigger_error($e, E_USER_WARNING);
151
							  E_USER_WARNING);
-
 
152
 
138
			} else {
153
			} else {
139
				// Informations sur les en-têtes et métadonnées du flux
154
				// Informations sur les en-têtes et métadonnées du flux
140
				$this->reponse_entetes = stream_get_meta_data($flux);
155
				$this->reponse_entetes = stream_get_meta_data($flux);
Line 141... Line 156...
141
				
156