/[openfoncier]/trunk/services/outgoing/MessageSenderRest.class.php
ViewVC logotype

Contents of /trunk/services/outgoing/MessageSenderRest.class.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2024 - (show annotations)
Wed Jun 19 14:10:38 2013 UTC (11 years, 7 months ago) by vpihour
File size: 7404 byte(s)
Suppression des accesseurs et mutateurs inutiles.
Utilisation des accesseurs et mutateurs dans les méthodes de la classe

1 <?php
2 /**
3 * Classe générique d'envoi de requête REST avec cURL
4 *
5 * @package openads
6 */
7
8 class MessageSenderRest {
9
10 // Variables de la classe
11 private $url;
12 private $username;
13 private $password;
14 private $curl;
15 private $contentType;
16 private $data;
17 private $headers = array();
18 private $response = "";
19 private $acceptedContentType = array(
20 'application/xml' => 'xml',
21 'text/xml' => 'xml',
22 'application/json' => 'json',
23 'text/json' => 'json',
24 'application/html' => 'html',
25 'text/html' => 'html',
26 'application/pdf' => 'pdf',
27 'text/pdf' => 'pdf',
28 'text/plain' => 'plain',
29 'image/gif' => 'gif',
30 'image/jpeg' => 'jpeg',
31 'image/png' => 'png',
32 'application/base64' => 'base64',
33 );
34
35
36 /**
37 * Constructeur
38 */
39 public function __construct($url, $username = "", $password = ""){
40
41 //Initialisation des variables de la classe
42 $this->url = $url;
43 $this->username = $username;
44 $this->password = $password;
45
46 //Initialisation de la session curl
47 $this->curl = curl_init();
48 curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
49
50 //Tente une authentification si les options ont été fournies
51 if ( $this->username !== "" && $this->password !== "" ){
52 curl_setopt($this->curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
53 curl_setopt($this->curl, CURLOPT_USERPWD, $this->username . ':' . $this->password);
54 }
55 }
56
57 /**
58 * Lance l'appel au webservice
59 * @param string $method
60 * @param string $contentType
61 * @param string $file
62 *
63 * @return Retourne la réponse du webservice ou -1 en cas d'erreur
64 */
65 public function execute($method, $contentType, $data){
66
67 //L'URL d'envoi doit être non vide
68 if (strcmp($this->getUrl(), "")==0) {
69 return -1;
70 }
71
72 //Formatage des données passées en paramètre
73 $data = is_array($data)?http_build_query($data):$data;
74
75 //Ajoute les certaines options à la session curl selon la méthode d'envoi
76 //souhaitée
77 switch ($method) {
78 case 'GET':
79 curl_setopt($this->getCurl(),CURLOPT_HTTPGET,true);
80 break;
81 case 'PUT':
82 curl_setopt($this->getCurl(),CURLOPT_PUT,true);
83 break;
84 case 'POST':
85 curl_setopt($this->getCurl(),CURLOPT_POST,true);
86 curl_setopt($this->getCurl(),CURLOPT_POSTFIELDS,$data);
87 break;
88 case 'DELETE':
89 curl_setopt($this->getCurl(),CURLOPT_CUSTOMREQUEST,'DELETE');
90 break;
91 //Si aucune méthode n'a été fournie
92 default:
93 return -1;
94 }
95 // une reponse est attendue
96 curl_setopt($this->getCurl(), CURLOPT_RETURNTRANSFER, true);
97 //Ajoute l'url
98 curl_setopt($this->getCurl(),CURLOPT_URL,$this->getUrl()."?".$data);
99 $this->setResponse(curl_exec($this->getCurl()));
100
101 $this->treatResponse();
102
103 return $this->getResponse();
104 }
105
106 /**
107 * Va traiter le retour de l'exécution d'une requête afin d'en extraire l'header
108 * et la réponse.
109 */
110 private function treatResponse(){
111
112 $headers = array();
113 //Si la réponse n'est pas vide
114 if ( $this->getResponse() !== NULL ){
115
116 //Récupération des informations du transfert curl
117 $info = curl_getinfo($this->getCurl());
118
119 //Si la requête ne retourne aucune information
120 if ( !is_array($info) ){
121 return;
122 }
123
124 //Récupère le type de contenu et le code HTTP de la requête réponse
125 $headers['content_type'] = $info['content_type'];
126 $headers['http_code'] = $info['http_code'];
127
128 $this->setHeaders($headers);
129
130 //Décomposition des données selon le type de retour
131 $acceptedContentType = $this->getAcceptedContentType();
132 $contentType = isset($acceptedContentType[$info['content_type']])?$acceptedContentType[$info['content_type']]: '';
133 switch ($contentType) {
134 case 'xml':
135 $this->setResponse((array) simplexml_load_string($this->getResponse(), 'SimpleXMLElement', LIBXML_NOCDATA));
136 break;
137 case 'json':
138 $this->setResponse((array) json_decode(trim($this->getResponse())));
139 break;
140 //Si la réponse n'est pas un des formats supportés
141 default:
142
143 // Le header et le contenu du retour sont séparés par une ligne vide
144 $parts = explode("\n\r",$this->getResponse());
145
146 //Recompose la réponse dans le cas où elle contiendrait des
147 //sauts de lignes
148 $this->setResponse("");
149 for ( $i = 0 ; $i < count($parts) ; $i++ ) {
150
151 if( $i > 0 ) {
152 $this->setResponse($this->getResponse()."\n\r") ;
153 }
154 $this->setResponse($this->getResponse().$parts[$i]) ;
155 }
156 break;
157 }
158 }
159
160
161 }
162
163 /**
164 * Destructeur
165 */
166 public function __destruct(){
167 curl_close($this->curl);
168 }
169
170 /**
171 * Ferme la connexion curl
172 */
173 public function close(){
174 curl_close($this->curl);
175 }
176
177 // {{{ Accesseur
178 /**
179 * @return string
180 */
181 public function getUrl() {
182 return $this->url;
183 }
184
185 /**
186 * @return object
187 */
188 public function getCurl() {
189 return $this->curl;
190 }
191
192 /**
193 * @return array
194 */
195 public function getHeaders() {
196 return $this->headers;
197 }
198
199 /**
200 * @return string
201 */
202 public function getResponse() {
203 return $this->response;
204 }
205
206 /**
207 * @return numeric
208 */
209 public function getResponseCode() {
210 return $this->headers['http_code'];
211 }
212
213 /**
214 * @return string
215 */
216 public function getResponseContentType() {
217 return $this->headers['content_type'];
218 }
219
220 /**
221 * @return array
222 */
223 public function getAcceptedContentType() {
224 return $this->acceptedContentType;
225 }
226 // }}}
227
228 // {{{ Mutateur
229 /**
230 * Met à jour le header.
231 * @param array $headers
232 */
233 public function setHeaders($headers) {
234 $this->headers=$headers;
235 }
236
237 /**
238 * Met à jour la réponse.
239 * @param string $response
240 */
241 public function setResponse($response) {
242 $this->response=$response;
243 }
244
245 /**
246 * Met à jour les types de contenu acceptés.
247 * @param array $acceptedContentType
248 */
249 public function setAcceptedContentType($acceptedContentType) {
250 $this->acceptedContentType=$acceptedContentType;
251 }
252 // }}}
253 }
254
255 ?>

Properties

Name Value
svn:keywords Id

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26