sync
[Pman.Core] / GoogleTranslate.php
1 <?php
2
3 /**
4  * Description of GoogleTranslate
5  *
6  * @author chris
7  */
8
9 require_once 'Pman.php';
10 class Pman_Core_GoogleTranslate extends Pman
11 {
12     //put your code here
13     function getAuth()
14     {
15         
16         $au = $this->getAuthUser();
17         if (!$au) {
18             $this->jerrAuth("only authenticated users");
19         }
20         
21         $this->authUser = $au;
22     }
23     function get() {
24         // for testing..
25         return $this->post();
26     }
27     
28     function post()
29     {
30         $pc = HTML_FlexyFramework::get()->Pman_Core;
31         if (empty($pc['googlekey'])) {
32             $this->jerr("Google API Key not configured");
33         }
34         if (!strlen(trim($_REQUEST['text']))) {
35             $this->jok(array("translatedText" =>""));
36         }
37         $param = array(
38             'key' => $pc['googlekey'],
39             'q' =>  $_REQUEST['text'],
40             'source' => $_REQUEST['src'],
41             'target' => $_REQUEST['dest'],
42             'ie' => 'UTF-8',
43             'oe' => 'UTF-8',
44         );
45         
46         
47         $url = 'https://www.googleapis.com/language/translate/v2';
48
49         $handle = curl_init();
50         curl_setopt($handle, CURLOPT_URL, $url);
51         curl_setopt($handle, CURLOPT_POST, count($param));
52         curl_setopt($handle, CURLOPT_POSTFIELDS, $param);
53         curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
54         curl_setopt($handle, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: GET'));
55
56         $response = curl_exec($handle);
57
58 //        $responseDecoded = json_decode($response);
59         curl_close($handle);
60         
61         header("content-type: text/json");
62         echo $response;
63         exit;
64         if(!empty($responseDecoded->error)){
65             $this->jerr($responseDecoded->error->message);
66         }
67 //        print_r($responseDecoded);
68         if(empty($responseDecoded->data->translations[0]->translatedText)){
69             $this->jerr('does not have translated text.', print_r($responseDecoded, true));
70         }
71 //        var_dump($responseDecoded->data->translations[0]->translatedText);
72         $responseDecoded->data->translations[0]->translatedText = rawurldecode(str_replace(' ', '', $responseDecoded->data->translations[0]->translatedText));
73         $this->jok($responseDecoded->data->translations[0]);
74         
75     }
76     
77     
78 }