@@ -102,7 +102,23 @@ protected function checkStatusCode($status_code)
102
102
}
103
103
104
104
/**
105
- * Handles a CURL request to PrestaShop Webservice. Can throw exception.
105
+ * Throws exception if prestashop version is not supported
106
+ * @param int $version The prestashop version
107
+ * @throws PrestaShopWebserviceException
108
+ */
109
+ public function isPrestashopVersionSupported ($ version )
110
+ {
111
+ if (version_compare ($ version , self ::PS_COMPATIBLE_VERSION_MIN , '>= ' ) === false ||
112
+ version_compare ($ version , self ::PS_COMPATIBLE_VERSION_MAX , '<= ' ) === false
113
+ ) {
114
+ $ exception = 'This library is not compatible with this version of PrestaShop. ' ;
115
+ $ exception .= 'Please upgrade/downgrade this library ' ;
116
+ throw new PrestaShopWebserviceException ($ exception );
117
+ }
118
+ }
119
+
120
+ /**
121
+ * Prepares and validate a CURL request to PrestaShop Webservice. Can throw exception.
106
122
* @param string $url Resource name
107
123
* @param mixed $curl_params CURL parameters (sent to curl_set_opt)
108
124
* @return array status_code, response
@@ -119,8 +135,6 @@ protected function executeRequest($url, $curl_params = array())
119
135
CURLOPT_HTTPHEADER => array ( 'Expect: ' )
120
136
);
121
137
122
- $ session = curl_init ($ url );
123
-
124
138
$ curl_options = array ();
125
139
foreach ($ defaultParams as $ defkey => $ defval ) {
126
140
if (isset ($ curl_params [$ defkey ])) {
@@ -135,55 +149,79 @@ protected function executeRequest($url, $curl_params = array())
135
149
}
136
150
}
137
151
138
- curl_setopt_array ($ session , $ curl_options );
139
- $ response = curl_exec ($ session );
152
+ list ($ response , $ info , $ error ) = $ this ->executeCurl ($ url , $ curl_options );
153
+
154
+ $ status_code = $ info ['http_code ' ];
155
+ if ($ status_code === 0 || $ error ) {
156
+ throw new PrestaShopWebserviceException ('CURL Error: ' .$ error );
157
+ }
140
158
141
- $ index = strpos ( $ response , "\r\n\r\n" ) ;
142
- if ($ index === false && $ curl_params [CURLOPT_CUSTOMREQUEST ] != 'HEAD ' ) {
159
+ $ index = $ info [ ' header_size ' ] ;
160
+ if ($ index === false && $ curl_params [CURLOPT_CUSTOMREQUEST ] !== 'HEAD ' ) {
143
161
throw new PrestaShopWebserviceException ('Bad HTTP response ' );
144
162
}
145
163
146
164
$ header = substr ($ response , 0 , $ index );
147
- $ body = substr ($ response , $ index + 4 );
148
-
149
- $ headerArrayTmp = explode ("\n" , $ header );
165
+ $ body = substr ($ response , $ index );
150
166
151
167
$ headerArray = array ();
152
- foreach ($ headerArrayTmp as & $ headerItem ) {
153
- $ tmp = explode (': ' , $ headerItem );
154
- $ tmp = array_map ( ' trim ' , $ tmp );
155
- if ( count ( $ tmp) == 2 ) {
168
+ foreach (explode ( "\n" , $ header ) as $ headerItem ) {
169
+ $ tmp = explode (': ' , $ headerItem, 2 );
170
+ if ( count ( $ tmp) === 2 ) {
171
+ $ tmp = array_map ( ' trim ' , $ tmp );
156
172
$ headerArray [$ tmp [0 ]] = $ tmp [1 ];
157
173
}
158
174
}
159
175
160
176
if (array_key_exists ('PSWS-Version ' , $ headerArray )) {
177
+ $ this ->isPrestashopVersionSupported ($ headerArray ['PSWS-Version ' ]);
161
178
$ this ->version = $ headerArray ['PSWS-Version ' ];
162
- if (version_compare (PrestaShopWebservice::PS_COMPATIBLE_VERSION_MIN , $ headerArray ['PSWS-Version ' ]) == 1 ||
163
- version_compare (PrestaShopWebservice::PS_COMPATIBLE_VERSION_MAX , $ headerArray ['PSWS-Version ' ]) == -1
164
- ) {
165
- $ exception = 'This library is not compatible with this version of PrestaShop. ' ;
166
- $ exception .= 'Please upgrade/downgrade this library ' ;
167
- throw new PrestaShopWebserviceException ($ exception );
168
- }
169
179
}
170
180
171
- $ this ->printDebug ('HTTP REQUEST HEADER ' , curl_getinfo ( $ session , CURLINFO_HEADER_OUT ) );
181
+ $ this ->printDebug ('HTTP REQUEST HEADER ' , $ info [ ' request_header ' ] );
172
182
$ this ->printDebug ('HTTP RESPONSE HEADER ' , $ header );
173
183
174
- $ status_code = curl_getinfo ($ session , CURLINFO_HTTP_CODE );
175
- if ($ status_code === 0 ) {
176
- throw new PrestaShopWebserviceException ('CURL Error: ' .curl_error ($ session ));
177
- }
178
- curl_close ($ session );
179
-
180
184
if ($ curl_params [CURLOPT_CUSTOMREQUEST ] == 'PUT ' || $ curl_params [CURLOPT_CUSTOMREQUEST ] == 'POST ' ) {
181
185
$ this ->printDebug ('XML SENT ' , urldecode ($ curl_params [CURLOPT_POSTFIELDS ]));
182
186
}
183
187
if ($ curl_params [CURLOPT_CUSTOMREQUEST ] != 'DELETE ' && $ curl_params [CURLOPT_CUSTOMREQUEST ] != 'HEAD ' ) {
184
188
$ this ->printDebug ('RETURN HTTP BODY ' , $ body );
185
189
}
186
- return array ('status_code ' => $ status_code , 'response ' => $ body , 'header ' => $ header );
190
+
191
+ return array (
192
+ 'status_code ' => $ status_code ,
193
+ 'response ' => $ body ,
194
+ 'header ' => $ header ,
195
+ 'headers ' => $ headerArray
196
+ );
197
+ }
198
+
199
+ /**
200
+ * Executes the CURL request to PrestaShop Webservice.
201
+ * @param string $url Resource name
202
+ * @param mixed $options CURL parameters (sent to curl_setopt_array)
203
+ * @return array response, info
204
+ */
205
+ protected function executeCurl ($ url , array $ options = array ())
206
+ {
207
+ $ session = curl_init ($ url );
208
+
209
+ if (count ($ options )) {
210
+ curl_setopt_array ($ session , $ options );
211
+ }
212
+
213
+ $ response = curl_exec ($ session );
214
+
215
+ $ error = false ;
216
+ if ($ response === false ) {
217
+ $ error = curl_error ($ session );
218
+ } else {
219
+ $ info = curl_getinfo ($ session );
220
+ }
221
+
222
+ curl_close ($ session );
223
+
224
+ return array ($ response , $ info , $ error );
187
225
}
188
226
189
227
public function printDebug ($ title , $ content )
0 commit comments