@@ -12,35 +12,147 @@ class HttpClient {
1212 const REQUEST_TYPE_PUT = 'PUT ' ;
1313 const REQUEST_TYPE_DELETE = 'DELETE ' ;
1414
15+ /**
16+ * defines boundary in case of different content types
17+ *
18+ * @var null | string
19+ */
1520 protected static $ _boundary = null ;
1621
17- protected $ _host ;
22+ /**
23+ *
24+ * @var string
25+ */
26+ protected $ _host ;
27+
28+ /**
29+ *
30+ * @var int
31+ */
1832 protected $ _port ;
33+
34+ /**
35+ *
36+ * @var string
37+ */
1938 protected $ _path ;
39+
40+ /**
41+ *
42+ * @var string
43+ */
2044 protected $ _scheme ;
45+
46+ /**
47+ * http method
48+ *
49+ * @var string
50+ */
2151 protected $ _method ;
52+
53+ /**
54+ * stores data for POST query
55+ *
56+ * @var string
57+ */
2258 protected $ _postdata = '' ;
59+
60+ /**
61+ *
62+ * @var string
63+ */
2364 protected $ _httpVersion = 'HTTP/1.0 ' ;
65+
66+ /**
67+ *
68+ * @var string
69+ */
2470 protected $ _accept = 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,image/jpeg,image/gif,*/* ' ;
25- protected $ _acceptEncoding = 'gzip ' ;
71+
72+ /**
73+ *
74+ * @var string
75+ */
76+ protected $ _acceptEncoding = 'gzip ' ;
77+
78+ /**
79+ * collects custom request header for http query
80+ *
81+ * @var array
82+ */
2683 protected $ _requestHeaders = array ();
84+
85+ /**
86+ * stores request variables (data) for http request
87+ *
88+ * @var string
89+ */
2790 protected $ _requestData ;
91+
92+ /**
93+ *
94+ * @var int
95+ */
2896 protected $ _timeout = 30 ;
29- protected $ _useGzip = false ;
30- protected $ _maxRedirects = 5 ;
97+
98+ /**
99+ *
100+ * @var bool
101+ */
102+ protected $ _useGzip = false ;
103+
104+ /**
105+ *
106+ * @var bool
107+ */
31108 protected $ _headersOnly = false ;
109+
110+ /**
111+ * flag for upload file
112+ *
113+ * @var bool
114+ */
32115 protected $ _needUploadFile = false ;
33- protected $ _fileKey = 'file ' ;
34116
117+ /**
118+ * flag for update content
119+ *
120+ * @var bool
121+ */
122+ protected $ _needUploadContent = false ;
123+
124+ /**
125+ * holds key in parameters for defining which param stores uploading data
126+ *
127+ * @var string
128+ */
129+ protected $ _fileKey = 'file ' ;
130+
131+ /**
132+ *
133+ * @var string
134+ */
35135 protected $ _status ;
136+
137+ /**
138+ *
139+ * @var array
140+ */
36141 protected $ _headers = array ();
142+
143+ /**
144+ * stores response content
145+ *
146+ * @var string
147+ */
37148 protected $ _content = '' ;
38- protected $ _errormsg ;
39-
40- // * Tracker variables:
41-
42- protected $ _redirect_count = 0 ;
43149
150+ /**
151+ *
152+ * @var array
153+ */
154+ protected $ _errormsg ;
155+
44156 /**
45157 *
46158 * @param string $uri
@@ -135,6 +247,7 @@ public function getRequestData(){
135247
136248
137249 /**
250+ * prepare data for http request body depending request method
138251 *
139252 * @param string | array | object $data
140253 */
@@ -162,17 +275,22 @@ protected function _buildQuery($data){
162275 . "Content-Length: " . strlen ($ value ) . "\r\n\r\n"
163276 . $ value . "\r\n" ;
164277 }
165- if ($ this ->_needUploadFile ){
166- if (file_exists (realpath ($ data [$ this ->_fileKey ]))){
278+ if ($ this ->_needUploadFile || $ this -> _needUploadContent ){
279+ if ($ this -> _needUploadFile && file_exists (realpath ($ data [$ this ->_fileKey ]))){
167280 $ file_contents = file_get_contents (realpath ($ data [$ this ->_fileKey ]));
168-
169- $ this ->_postdata .= "-- " . $ boundary . "\r\n"
170- . "Content-Disposition: form-data; name= \"" . $ this ->_fileKey
171- . "\"; filename = \"" . basename ($ data [$ this ->_fileKey ]) . "\"\r\n"
172- . "Content-Length: " . strlen ($ file_contents ) . "\r\n"
173- . "Content-Type: application/octet-stream \r\n\r\n"
174- . $ file_contents . "\r\n" ;
175281 }
282+
283+ if ($ this ->_needUploadContent && ($ data [$ this ->_fileKey ] !== '' )){
284+ $ file_contents = $ data [$ this ->_fileKey ];
285+ }
286+
287+ $ this ->_postdata .= "-- " . $ boundary . "\r\n"
288+ . "Content-Disposition: form-data; name= \"" . $ this ->_fileKey
289+ . "\"; filename = \"" . basename ($ data [$ this ->_fileKey ]) . "\"\r\n"
290+ . "Content-Length: " . strlen ($ file_contents ) . "\r\n"
291+ . "Content-Type: application/octet-stream \r\n\r\n"
292+ . $ file_contents . "\r\n" ;
293+
176294 }
177295 $ this ->_postdata .= "-- " . $ boundary . "-- " ;
178296 }
@@ -233,6 +351,7 @@ protected function _buildRequest(){
233351 }
234352
235353 /**
354+ * establish connection with server
236355 *
237356 * @return resource
238357 */
@@ -253,6 +372,7 @@ protected function _connect(){
253372 }
254373
255374 /**
375+ * parse response and unset headers
256376 *
257377 * @param resource $fp
258378 * @return boolean
@@ -317,16 +437,29 @@ public function getContent(){
317437 }
318438
319439 /**
320- * set file
440+ * set flag for uploading file content
441+ *
321442 * @param bool $flag Description
322443 * @return HttpClient
323444 */
324445 public function setNeedUploadFile ($ flag ){
325446 $ this ->_needUploadFile = $ flag ;
326447 return $ this ;
327- }
448+ }
328449
329450 /**
451+ * set flag for uploading content
452+ *
453+ * @param bool $flag
454+ * @return \HttpClient
455+ */
456+ public function setNeedUploadContent ($ flag ){
457+ $ this ->_needUploadContent = $ flag ;
458+ return $ this ;
459+ }
460+
461+ /**
462+ * set headers
330463 *
331464 * @param bool $flag
332465 * @return \HttpClient
@@ -337,6 +470,7 @@ public function setHeadersOnly($flag){
337470 }
338471
339472 /**
473+ *
340474 *
341475 * @param bool $flag
342476 * @return \HttpClient
0 commit comments