GoogleTranslate.php
[Pman.Core] / GoogleTranslate.php
index 605bb5b..a2d7a91 100644 (file)
@@ -31,7 +31,9 @@ class Pman_Core_GoogleTranslate extends Pman
         if (empty($pc['googlekey'])) {
             $this->jerr("Google API Key not configured");
         }
-        
+        if (!strlen(trim($_REQUEST['text']))) {
+            $this->jok(array("translatedText" =>""));
+        }
         $param = array(
             'key' => $pc['googlekey'],
             'q' => rawurlencode($_REQUEST['text']),
@@ -40,25 +42,29 @@ class Pman_Core_GoogleTranslate extends Pman
         );
         
         $url = 'https://www.googleapis.com/language/translate/v2';
-//
-//        $handle = curl_init();
-//        curl_setopt($handle, CURLOPT_URL, $url);
-//        curl_setopt($handle, CURLOPT_POST, count($param));
-//        curl_setopt($handle, CURLOPT_POSTFIELDS, http_build_query($param));
-//        curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
-//        curl_setopt($handle, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: GET'));
-//
-//        $response = curl_exec($handle);
-//        utf8_decode('{"success":true,"total":1,"data":{"data":{"translations":[{"translatedText":"Alessi% E6% 96% B0% E5% 93% 81% E7% 99% BC% E5% B8% 83% E3% 80% 80% E6% 99% 82% E5% B0% 9A% E5% AF% A6% E7% 94% A8% E5% 85% BC% E5% 82% 99"}]}}}');
+
+        $handle = curl_init();
+        curl_setopt($handle, CURLOPT_URL, $url);
+        curl_setopt($handle, CURLOPT_POST, count($param));
+        curl_setopt($handle, CURLOPT_POSTFIELDS, http_build_query($param));
+        curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
+        curl_setopt($handle, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: GET'));
+
+        $response = curl_exec($handle);
+
+        $responseDecoded = json_decode($response);
+        curl_close($handle);
         
-        $testStr = '{"success":true,"total":1,"data":{"data":{"translations":[{"translatedText":"Alessi %E6 %96 %B0 %E5 %93 %81 %E7% 99% BC% E5% B8% 83% E3% 80% 80% E6% 99% 82% E5% B0% 9A% E5% AF% A6% E7% 94% A8% E5% 85% BC% E5% 82% 99"}]}}}';
-//        mb_convert_encoding($testStr, 'HTML-ENTITIES', "UTF-8");
-        print_R(utf8_decode(urldecode($testStr)));
-        $responseDecoded = json_decode(utf8_decode(urldecode($testStr)));
-//        curl_close($handle);
-//        print_r($responseDecoded->data->data->translations[0]->translatedText);
-        $responseDecoded->data->data->translations[0]->translatedText = utf8_decode(urldecode($responseDecoded->data->data->translations[0]->translatedText));
-        $this->jok($responseDecoded->data->data->translations[0]);
+        if(!empty($responseDecoded->error)){
+            $this->jerr($responseDecoded->error->message);
+        }
+//        print_r($responseDecoded);
+        if(empty($responseDecoded->data->translations[0]->translatedText)){
+            $this->jerr('does not have translated text.', print_r($responseDecoded, true));
+        }
+        var_dump($responseDecoded->data->translations[0]->translatedText);
+        $responseDecoded->data->translations[0]->translatedText = rawurldecode(str_replace(' ', '', $responseDecoded->data->translations[0]->translatedText));
+        $this->jok($responseDecoded->data->translations[0]);
         
     }