@@ -154,11 +154,11 @@ public function __construct($user_key, $service_url = 'https://api.rosette.com/
154154 $ this ->subUrl = null ;
155155 $ this ->timeout = 300 ;
156156
157- $ this ->headers = array (' X-RosetteAPI-Key ' => $ user_key ,
158- ' Content-Type ' => ' application/json ' ,
159- ' Accept ' => ' application/json ' ,
160- ' Accept-Encoding ' => ' gzip ' ,
161- ' User-Agent ' => ' RosetteAPIPHP/ ' . self ::$ binding_version , );
157+ $ this ->headers = array (" X-RosetteAPI-Key: $ user_key" ,
158+ " Content-Type: application/json " ,
159+ " Accept: application/json " ,
160+ " Accept-Encoding: gzip " ,
161+ " User-Agent: RosetteAPIPHP/ " . self ::$ binding_version , );
162162 }
163163
164164 /**
@@ -275,36 +275,45 @@ private function finishResult($resultObject, $action)
275275 */
276276 private function callEndpoint ($ parameters , $ subUrl )
277277 {
278- // $this->checkVersion($this->service_url);
278+ $ this ->checkVersion ($ this ->service_url );
279279 $ this ->subUrl = $ subUrl ;
280280 $ this ->useMultiPart = $ parameters ->useMultiPart ;
281+
281282 if ($ this ->useMultiPart ){
283+ $ content = $ parameters ->content ;
284+ $ filename = $ parameters ->fileName ;
285+
286+ $ parameters = (array ) $ parameters ;
287+ json_encode ($ parameters );
288+ json_encode ($ content , JSON_FORCE_OBJECT );
289+
290+ // create multipart
282291 $ clrf = "\r\n" ;
283292 $ multi = '' ;
284293 $ boundary = md5 (time ());
285294 $ multi .= '-- ' . $ boundary . $ clrf ;
286- $ multi .= 'Content-Type: "application/json" ' . "\r\n" ;
287- $ multi .= 'Content-Disposition: form-data; name="request" ' . "\r\n" . "\r\n" ;
288- //$multi .= $parameters . $clrf .$clrf;
295+ $ multi .= 'Content-Type: application/json ' . "\r\n" ;
296+ $ multi .= 'Content-Disposition: mixed; name="request" ' . "\r\n" . "\r\n" ;
297+ $ multi .= "{ \"language \": \"eng \"} " . "\r\n" ;
298+ $ multi .= $ parametersTemp . $ clrf .$ clrf ;
289299 $ multi .= '-- ' . $ boundary . "\r\n" ;
290300 $ multi .= 'Content-Type: text/plain ' . "\r\n" ;
291- $ multi .= 'Content-Disposition: "multipart/form-data" ; name="content"; filename=" ' . $ parameters -> fileName . '" ' . "\r\n" . "\r\n" ;
292- $ multi .= $ parameters -> content . "\r\n" . "\r\n" ;
301+ $ multi .= 'Content-Disposition: mixed ; name="content"; filename=" ' . $ filename . '" ' . "\r\n" . "\r\n" ;
302+ $ multi .= $ content . "\r\n" ;
293303 $ multi .= '-- ' . $ boundary . '-- ' ;
294304
295- $ this ->headers = array (' X-RosetteAPI-Key ' => $ this ->user_key ,
296- ' Content-Type ' => ' multipart/form-data ' ,
297- ' Accept ' => ' */* ' ,
298- ' Accept-Encoding ' => ' gzip ' ,
299- ' User-Agent ' => ' RosetteAPIPHP/ ' . self ::$ binding_version , );
305+ $ this ->headers = array (" X-RosetteAPI-Key: $ this ->user_key " ,
306+ " Content-Type: multipart/mixed " ,
307+ " Accept: */* " ,
308+ " Accept-Encoding: gzip " ,
309+ " User-Agent: RosetteAPIPHP/ " . self ::$ binding_version , );
300310
301311 $ url = $ this ->service_url . $ this ->subUrl ;
302312 if ($ this ->debug ) {
303313 $ url .= '?debug=true ' ;
304314 }
305- $ parameters ->content = $ multi ;
306315
307- $ resultObject = $ this ->postHttp ($ url , $ this ->headers , $ parameters );
316+ $ resultObject = $ this ->postHttp ($ url , $ this ->headers , $ multi );
308317 return $ this ->finishResult ($ resultObject , 'callEndpoint ' );
309318
310319 } else {
@@ -334,12 +343,10 @@ public function checkVersion($url, $versionToCheck = null)
334343 $ versionToCheck = self ::$ binding_version ;
335344 }
336345 $ resultObject = $ this ->postHttp ($ url . "info?clientVersion= $ versionToCheck " , $ this ->headers , null );
337- var_dump (json_encode ($ resultObject [1 ]));
338- @$ tempJSON = json_encode ($ resultObject [1 ]);
339- $ finalJSON = json_decode ($ tempJSON , true );
340- var_dump ($ finalJSON );
346+ $ resultObject = array_pop ((array_slice ($ resultObject , -1 )));
347+ $ resultObject = (array ) json_decode ($ resultObject );
341348
342- if ($ finalJSON ['versionChecked ' ] === true ) {
349+ if ($ resultObject ['versionChecked ' ] === true ) {
343350 $ this ->version_checked = true ;
344351 } else {
345352 throw new RosetteException (
@@ -348,7 +355,7 @@ public function checkVersion($url, $versionToCheck = null)
348355 );
349356 }
350357 }
351- $ this -> versionChecked = true ;
358+
352359 return $ this ->version_checked ;
353360 }
354361
@@ -358,7 +365,7 @@ public function checkVersion($url, $versionToCheck = null)
358365 * Encapsulates the GET/POST
359366 *
360367 * @param $url
361- * @param $context
368+ * @param $data
362369 *
363370 * @return string
364371 *
@@ -367,62 +374,69 @@ public function checkVersion($url, $versionToCheck = null)
367374 * @internal param $op : operation
368375 * @internal param $url : target URL
369376 * @internal param $headers : header data
370- * @internal param data $optional : submission data
377+ * @internal param $data : submission data
378+ * @internal param $method : http method
371379 */
372- private function makeRequest ($ url , $ headers , $ context , $ method )
380+ private function makeRequest ($ url , $ headers , $ data , $ method )
373381 {
374382 $ response = null ;
375383 $ message = null ;
376- $ context = array ('content ' => $ context ->content );
377- //$context = json_encode($context, JSON_FORCE_OBJECT);
378384
379- $ code = 'unknownError ' ;
380- $ http_response_header = null ;
381- //$response = file_get_contents($url, false, $context);
382- //$url = 'http://10.1.7.116:8181/rest/v1/language';
383- $ ch = curl_init ();
384- curl_setopt ($ ch , CURLOPT_URL , $ url );
385- curl_setopt ($ ch , CURLOPT_HTTPHEADER , $ headers );
386-
387- if ($ method === 'POST ' ){
388- curl_setopt ($ ch , CURLOPT_POST , true );
389- curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ context );
390- } else if ($ method === 'GET ' ){
391- curl_setopt ($ ch , CURLOPT_HTTPGET , TRUE );
385+ // check for multipart and set data accordingly
386+ if ($ this ->useMultiPart === NULL ){
387+ $ data = (array ) $ data ;
388+
389+ if ($ data ['content ' ] === "" ){
390+ unset($ data ['content ' ]);
391+ }
392+
393+ if ($ data ['contentUri ' ] === "" ){
394+ unset($ data ['contentUri ' ]);
392395 }
393396
394- curl_setopt ($ ch , CURLOPT_HEADER , 1 );
395- curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
396- $ response = curl_exec ($ ch );
397- $ resCode = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
398- if ($ response === false ){
399- echo curl_errno ($ ch );
400- echo curl_error ($ ch );
397+ foreach ($ data as $ v ){
398+ $ data = array_filter ($ data , function ($ v ){ if ($ v !== NULL || $ v !== "" ){return $ v ;}});
401399 }
402- curl_close ($ ch );
403- $ response = explode (PHP_EOL , $ response );
404- //var_dump($response);
405- $ this ->setResponseCode ($ resCode );
406- return $ response ;
407-
408- /*$response_status = $this->getResponseStatusCode($http_response_header);
409- $this->setResponseCode($response_status);
410- if (strlen($response) > 3 && mb_strpos($response, "\x1f" . "\x8b" . "\x08", 0) === 0) {
400+ $ data = json_encode ($ data , JSON_UNESCAPED_UNICODE );
401+ }
402+
403+ $ code = 'unknownError ' ;
404+ $ http_response_header = null ;
405+
406+ // create cURL request
407+ $ ch = curl_init ();
408+ curl_setopt ($ ch , CURLOPT_URL , $ url );
409+ curl_setopt ($ ch , CURLOPT_HTTPHEADER , $ headers );
410+
411+ if ($ method === 'POST ' ){
412+ curl_setopt ($ ch , CURLOPT_POST , true );
413+ curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ data );
414+ } else if ($ method === 'GET ' ){
415+ curl_setopt ($ ch , CURLOPT_HTTPGET , TRUE );
416+ }
417+
418+ curl_setopt ($ ch , CURLOPT_HEADER , 1 );
419+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
420+ $ response = curl_exec ($ ch );
421+ $ resCode = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
422+ if ($ response === false ){
423+ echo curl_errno ($ ch );
424+ echo curl_error ($ ch );
425+ }
426+ curl_close ($ ch );
427+ $ response = explode (PHP_EOL , $ response );
428+ $ this ->setResponseCode ($ resCode );
429+
430+ if (strlen ($ response [9 ]) > 3 && mb_strpos ($ response [9 ], "\x1f" . "\x8b" . "\x08" , 0 ) === 0 ) {
411431 // a gzipped string starts with ID1(\x1f) ID2(\x8b) CM(\x08)
412432 // http://www.gzip.org/zlib/rfc-gzip.html#member-format
413433 $ response = gzinflate (substr ($ response , 10 , -8 ));
414434 }
415435 if ($ this ->getResponseCode () < 500 ) {
416- if($http_response_header != null){
417- $response = array($http_response_header, json_decode($response, true));
418- } else {
419- $response = json_decode($response, true);
420- }
421436 return $ response ;
422- }*/
423- /* if ($response !== null) {
437+ }
438+ if ($ response !== null ) {
424439 try {
425- $json = json_decode($response, true);
426440 if (array_key_exists ('message ' , $ json )) {
427441 $ message = $ json ['message ' ];
428442 }
@@ -432,12 +446,12 @@ private function makeRequest($url, $headers, $context, $method)
432446 } catch (\Exception $ e ) {
433447 // pass
434448 }
435- }*/
449+ }
436450
437- /* if ($code === 'unknownError') {
451+ if ($ code === 'unknownError ' ) {
438452 $ message = sprintf ('A retryable network operation has not succeeded after %d attempts ' , $ this ->numRetries );
439453 }
440- throw new RosetteException($message . ' [' . $url . ']', $code);*/
454+ throw new RosetteException ($ message . ' [ ' . $ url . '] ' , $ code );
441455 }
442456
443457 /**
0 commit comments