PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
Copy Clear
Copy Format Clear
<?php class Translation { const DETECT_YA_URL = 'https://translate.yandex.net/api/v1.5/tr.json/detect'; const TRANSLATE_YA_URL = 'https://translate.yandex.net/api/v1.5/tr.json/translate'; private static $key ="AIza1yCf2zgkmk-nRxdbB4gg49M9GZhmFei55uo"; function __construct() { if (empty(self::$key)) { throw new ErrorException("Field key is required"); } else { echo self::$key; } } public static function translate_text($format="text") { $values = array( 'key' => self::$key, 'text' => htmlspecialchars($_GET["text"]), 'lang' => htmlspecialchars($_GET["lang"]), 'format' => $format == "text" ? 'plain': $format ); $formData = http_build_query($values); echo $formData; $ch = curl_init(self::TRANSLATE_YA_URL); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $formData); $json = curl_exec($ch); curl_close($ch); $data = json_decode($json, true); if($data['code'] == 200){ return $data['text']; } else { throw new ErrorException("Error"); } } } //$transl = new Translation(); //Translation::translate_text(); //echo $transl->translate_text(); //Translation::translate_text();
Show:  
Copy Clear