diff --git a/example.php b/example.php
old mode 100755
new mode 100644
index a1a9896..c6f7caa
--- a/example.php
+++ b/example.php
@@ -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 = "";
@@ -22,7 +26,11 @@
var_dump($result);
echo "
This is a upload file
";
-
+ //try to upload content
+ $result = $api->uploadContent($content, $fileType, $fileContentUri);
+ var_dump($result);
+ echo "
This is a upload content
";
+
//try to download file
$result = $api->downloadFile($fileUri, $locale);
var_dump($result);
diff --git a/lib/HttpClient.php b/lib/HttpClient.php
old mode 100755
new mode 100644
index 27214ef..6bfbac0
--- a/lib/HttpClient.php
+++ b/lib/HttpClient.php
@@ -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
@@ -135,6 +247,7 @@ public function getRequestData(){
/**
+ * prepare data for http request body depending request method
*
* @param string | array | object $data
*/
@@ -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 . "--";
}
@@ -233,6 +351,7 @@ protected function _buildRequest(){
}
/**
+ * establish connection with server
*
* @return resource
*/
@@ -253,6 +372,7 @@ protected function _connect(){
}
/**
+ * parse response and unset headers
*
* @param resource $fp
* @return boolean
@@ -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
@@ -337,6 +470,7 @@ public function setHeadersOnly($flag){
}
/**
+ *
*
* @param bool $flag
* @return \HttpClient
diff --git a/lib/SmartlingAPI.php b/lib/SmartlingAPI.php
old mode 100755
new mode 100644
index 0cc51d8..1fc61a0
--- a/lib/SmartlingAPI.php
+++ b/lib/SmartlingAPI.php
@@ -7,6 +7,9 @@ class SmartlingAPI
const SANDBOX_MODE = 'SANDBOX';
const PRODUCTION_MODE = 'PRODUCTION';
+ const SANDBOX_URL = 'https://sandbox-api.smartling.com/v1';
+ const PRODUCTION_URL = 'https://api.smartling.com/v1';
+
/**
*api base url
*
@@ -36,18 +39,19 @@ public function __construct($apiKey, $projectId, $mode = self::SANDBOX_MODE) {
$this->_apiKey = $apiKey;
$this->_projectId = $projectId;
if ($mode == self::PRODUCTION_MODE){
- $this->_baseUrl = "https://api.smartling.com/v1";
+ $this->_baseUrl = self::PRODUCTION_URL;
} else {
- $this->_baseUrl = "https://sandbox-api.smartling.com/v1";
+ $this->_baseUrl = self::SANDBOX_URL;
}
}
/**
+ * upload file to Smartling service
*
* @param string $path
* @param string $fileType
* @param string $fileUri
- * @param string $charset
+ * @param array $params
* @return string
*/
public function uploadFile($path, $fileType, $fileUri, $params = array()) {
@@ -60,6 +64,24 @@ public function uploadFile($path, $fileType, $fileUri, $params = array()) {
}
/**
+ * upload content to Smartling service
+ *
+ * @param string $content
+ * @param string $fileType
+ * @param string $fileUri
+ * @param array $params
+ * @return string
+ */
+ public function uploadContent($content, $fileType, $fileUri, $params = array()){
+ return $this->sendRequest('file/upload', array_merge_recursive(array(
+ 'file' => $content,
+ 'fileType' => $fileType,
+ 'fileUri' => $fileUri
+ ), $params), HttpClient::REQUEST_TYPE_POST, false, true);
+ }
+
+ /**
+ * download translated content from Smartling Service
*
* @param string $fileUri
* @param string $locale
@@ -73,6 +95,7 @@ public function downloadFile($fileUri, $locale, $params = array()) {
}
/**
+ * retrieve status about file translation progress
*
* @param string $fileUri
* @param string $locale
@@ -86,6 +109,7 @@ public function getStatus($fileUri, $locale, $params = array()) {
}
/**
+ * get uploaded files list
*
* @param string $locale
* @param array $params
@@ -98,6 +122,7 @@ public function getList($locale, $params = array()) {
}
/**
+ * rename uploaded before files
*
* @param string $fileUri
* @param string $newFileUri
@@ -111,6 +136,7 @@ public function renameFile($fileUri, $newFileUri){
}
/**
+ * remove uploaded files from Smartling Service
*
* @param string $fileUri
* @return string
@@ -122,6 +148,7 @@ public function deleteFile($fileUri){
}
/**
+ * import files form Service
*
* @param string $fileUri
* @param string $fileType
@@ -144,13 +171,14 @@ public function import($fileUri, $fileType, $locale, $file, $overwrite = false,
}
/**
+ * send request to Smartling Service
*
* @param string $uri
* @param array $requestData
* @param string $method
* @return string
*/
- protected function sendRequest($uri, $requestData, $method, $needUploadFile = false) {
+ protected function sendRequest($uri, $requestData, $method, $needUploadFile = false, $needUploadContent = false) {
$connection = new HttpClient($this->_baseUrl. "/" . $uri, 443);
$data['apiKey'] = $this->_apiKey;
@@ -160,7 +188,8 @@ protected function sendRequest($uri, $requestData, $method, $needUploadFile = fa
$connection->setMethod($method)
->setRequestData($request)
- ->setNeedUploadFile($needUploadFile);
+ ->setNeedUploadFile($needUploadFile)
+ ->setNeedUploadContent($needUploadContent);
if($connection->request())