Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
<?php
$apiBase = 'http://api.microsofttranslator.com/V2/Http.svc';
$appId = 'yourappid';
$text = 'Een hoge boom vangt veel wind';
$streamContext = stream_context_create(array('http' => array(
'method' => 'POST',
'content' => '',
'header' => implode("\r\n", array('Content-Type: text/xml', 'Content-Length: 0')),
)));
libxml_set_streams_context($streamContext);
$bingResponse = simplexml_load_file(
"{$apiBase}/GetTranslations?appId={$appId}&from=&to=en&maxTranslations=1&text=".urlencode($text)
);
$language = (string) $bingResponse->From;
$inEnglish = (string) $bingResponse->Translations->TranslationMatch->TranslatedText;
var_dump( $language, $inEnglish );
<?php
$apiBase = 'http://api.microsofttranslator.com/V2/Http.svc';
$appId = 'yourappid';
$text = 'Een hoge boom vangt veel wind';
$streamContext = stream_context_create(array('http' => array(
'method' => 'POST',
'content' => '',
'header' => implode("\r\n", array('Content-Type: text/xml', 'Content-Length: 0')),
)));
$bingResponse = simplexml_load_string(
file_get_contents(
"{$apiBase}/GetTranslations?appId={$appId}&from=&to=en&maxTranslations=1&text=".urlencode($text),
false,
$streamContext
)
);
$language = (string) $bingResponse->From;
$inEnglish = (string) $bingResponse->Translations->TranslationMatch->TranslatedText;
var_dump( $language, $inEnglish );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment