最新バージョンだけなら、難なくいけますね。
$text = $titles[0]."<br><br>"; foreach(glob("file/".$y."/{*.txt}",GLOB_BRACE) as $file){ if(is_file($file)){ $files[] = htmlspecialchars($file); } } // var_dump($files); $file = fopen($files[0], "r"); if($file){ while ($line = fgets($file)){ $text .= $line; } } fclose($file); $key = ''; $host = "https://api.cognitive.microsofttranslator.com"; $path = "/translate?api-version=3.0"; $params = "&to=zh"; if(!function_exists('com_create_guid')){ function com_create_guid(){ return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0x0fff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000, mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)); } } function Translate ($host, $path, $key, $params, $content) { $headers = "Content-type: application/json\r\n" . "Content-length: " . strlen($content) . "\r\n" . "Ocp-Apim-Subscription-Key: $key\r\n" . "X-ClientTraceId: " . com_create_guid() . "\r\n"; $options = array ( 'http' => array ( 'header' => $headers, 'method' => 'POST', 'content' => $content ) ); $context = stream_context_create($options); $result = file_get_contents ($host . $path . $params, false, $context); return $result; } $requestBody = array ( array ( 'Text' => $text, ), ); $content = json_encode($requestBody); $result = Translate($host, $path, $key, $params, $content); $json = json_decode($result); $newtext = $json[0]->translations[0]->text; echo $newtext;
ガンガンいきたいところだが、アプリ(android・iOS)のタスクがかなり重いので、translatorはひとまずこれをazureに載せるところまでとしたい。