Skip to content

Commit a9be1f1

Browse files
added functionality to download content without reading files
1 parent 5a1c7df commit a9be1f1

File tree

3 files changed

+198
-27
lines changed

3 files changed

+198
-27
lines changed

example.php

100755100644
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
$fileType = 'xml';
66
$newFileUri = 'newfile.xml';
77
$fileName = 'translated.xml';
8+
9+
$content = file_get_contents(realpath('./test.xml'));
10+
$fileContentUri = "testing_content.xml";
11+
812
$translationState = 'PUBLISHED';
913
$key = "";
1014
$projectId = "";
@@ -22,7 +26,11 @@
2226
var_dump($result);
2327
echo "<br />This is a upload file<br />";
2428

25-
29+
//try to upload content
30+
$result = $api->uploadContent($content, $fileType, $fileContentUri);
31+
var_dump($result);
32+
echo "<br />This is a upload content<br />";
33+
2634
//try to download file
2735
$result = $api->downloadFile($fileUri, $locale);
2836
var_dump($result);

lib/HttpClient.php

100755100644
Lines changed: 155 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)