Azure Translator Text APIを使ってphpで翻訳する

file_get_contentsしたjsonをdecodeします。

$key = 'hogehoge';

$host = "https://api.cognitive.microsofttranslator.com";
$path = "/translate?api-version=3.0";
$params = "&to=en";
$text = "1文をバラバラにして単語レベルで翻訳をするのではなく、1文をひとまとめにして翻訳";

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);
echo $json[0]->translations[0]->text;

これは。。。

eminem lose yourself
——-
Look, if you had one shot, one opportunity
To seize everything you ever wanted
One moment
Would you capture it or just let it slip?
Yo
His palms are sweaty, knees weak, arms are heavy
There’s vomit on his sweater already, mom’s spaghetti
He’s nervous, but on the surface he looks calm and ready
To drop bombs, but he keeps on forgettin’
What he wrote down, the whole crowd goes so loud
He opens his mouth, but the words won’t come out
He’s chokin’, how, everybody’s jokin’ now
The clocks run out, times up, over, blaow!
Snap back to reality, oh there goes gravity
Oh, there goes Rabbit, he choked
He’s so mad, but he won’t give up that easy? No
He won’t have it, he knows his whole back city’s ropes
It don’t matter,
He’s dope,…

ラップの翻訳って難しいと思うが、
結果

ほお