Skip to content

Commit

Permalink
added functionality to download content without reading files
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorCrazySnail committed Nov 18, 2013
1 parent 5a1c7df commit a9be1f1
Show file tree
Hide file tree
Showing 3 changed files with 198 additions and 27 deletions.
10 changes: 9 additions & 1 deletion example.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
$fileType = 'xml';
$newFileUri = 'newfile.xml';
$fileName = 'translated.xml';

$content = file_get_contents(realpath('./test.xml'));
$fileContentUri = "testing_content.xml";

$translationState = 'PUBLISHED';
$key = "";
$projectId = "";
Expand All @@ -22,7 +26,11 @@
var_dump($result);
echo "<br />This is a upload file<br />";


//try to upload content
$result = $api->uploadContent($content, $fileType, $fileContentUri);
var_dump($result);
echo "<br />This is a upload content<br />";

//try to download file
$result = $api->downloadFile($fileUri, $locale);
var_dump($result);
Expand Down
176 changes: 155 additions & 21 deletions lib/HttpClient.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,147 @@ class HttpClient {
const REQUEST_TYPE_PUT = 'PUT';
const REQUEST_TYPE_DELETE = 'DELETE';

/**
* defines boundary in case of different content types
*
* @var null | string
*/
protected static $_boundary = null;

protected $_host;
/**
*
* @var string
*/
protected $_host;

/**
*
* @var int
*/
protected $_port;

/**
*
* @var string
*/
protected $_path;

/**
*
* @var string
*/
protected $_scheme;

/**
* http method
*
* @var string
*/
protected $_method;

/**
* stores data for POST query
*
* @var string
*/
protected $_postdata = '';

/**
*
* @var string
*/
protected $_httpVersion = 'HTTP/1.0';

/**
*
* @var string
*/
protected $_accept = 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,image/jpeg,image/gif,*/*';
protected $_acceptEncoding = 'gzip';

/**
*
* @var string
*/
protected $_acceptEncoding = 'gzip';

/**
* collects custom request header for http query
*
* @var array
*/
protected $_requestHeaders = array();

/**
* stores request variables (data) for http request
*
* @var string
*/
protected $_requestData;

/**
*
* @var int
*/
protected $_timeout = 30;
protected $_useGzip = false;
protected $_maxRedirects = 5;

/**
*
* @var bool
*/
protected $_useGzip = false;

/**
*
* @var bool
*/
protected $_headersOnly = false;

/**
* flag for upload file
*
* @var bool
*/
protected $_needUploadFile = false;
protected $_fileKey = 'file';

/**
* flag for update content
*
* @var bool
*/
protected $_needUploadContent = false;

/**
* holds key in parameters for defining which param stores uploading data
*
* @var string
*/
protected $_fileKey = 'file';

/**
*
* @var string
*/
protected $_status;

/**
*
* @var array
*/
protected $_headers = array();

/**
* stores response content
*
* @var string
*/
protected $_content = '';
protected $_errormsg;

// * Tracker variables:

protected $_redirect_count = 0;

/**
*
* @var array
*/
protected $_errormsg;

/**
*
* @param string $uri
Expand Down Expand Up @@ -135,6 +247,7 @@ public function getRequestData(){


/**
* prepare data for http request body depending request method
*
* @param string | array | object $data
*/
Expand Down Expand Up @@ -162,17 +275,22 @@ protected function _buildQuery($data){
. "Content-Length:" . strlen($value) . "\r\n\r\n"
. $value . "\r\n";
}
if ($this->_needUploadFile){
if (file_exists(realpath($data[$this->_fileKey]))){
if ($this->_needUploadFile || $this->_needUploadContent){
if ($this->_needUploadFile && file_exists(realpath($data[$this->_fileKey]))){
$file_contents = file_get_contents(realpath($data[$this->_fileKey]));

$this->_postdata .= "--" . $boundary . "\r\n"
. "Content-Disposition: form-data; name=\"" . $this->_fileKey
. "\"; filename = \"" . basename($data[$this->_fileKey]) . "\"\r\n"
. "Content-Length: " . strlen($file_contents) . "\r\n"
. "Content-Type: application/octet-stream\r\n\r\n"
. $file_contents . "\r\n";
}

if ($this->_needUploadContent && ($data[$this->_fileKey] !== '')){
$file_contents = $data[$this->_fileKey];
}

$this->_postdata .= "--" . $boundary . "\r\n"
. "Content-Disposition: form-data; name=\"" . $this->_fileKey
. "\"; filename = \"" . basename($data[$this->_fileKey]) . "\"\r\n"
. "Content-Length: " . strlen($file_contents) . "\r\n"
. "Content-Type: application/octet-stream\r\n\r\n"
. $file_contents . "\r\n";

}
$this->_postdata .= "--" . $boundary . "--";
}
Expand Down Expand Up @@ -233,6 +351,7 @@ protected function _buildRequest(){
}

/**
* establish connection with server
*
* @return resource
*/
Expand All @@ -253,6 +372,7 @@ protected function _connect(){
}

/**
* parse response and unset headers
*
* @param resource $fp
* @return boolean
Expand Down Expand Up @@ -317,16 +437,29 @@ public function getContent(){
}

/**
* set file
* set flag for uploading file content
*
* @param bool $flag Description
* @return HttpClient
*/
public function setNeedUploadFile($flag){
$this->_needUploadFile = $flag;
return $this;
}
}

/**
* set flag for uploading content
*
* @param bool $flag
* @return \HttpClient
*/
public function setNeedUploadContent($flag){
$this->_needUploadContent = $flag;
return $this;
}

/**
* set headers
*
* @param bool $flag
* @return \HttpClient
Expand All @@ -337,6 +470,7 @@ public function setHeadersOnly($flag){
}

/**
*
*
* @param bool $flag
* @return \HttpClient
Expand Down
Loading

0 comments on commit a9be1f1

Please sign in to comment.