Skip to content

Instantly share code, notes, and snippets.

@salathe
Created May 31, 2011 10:14
Show Gist options
  • Save salathe/1000264 to your computer and use it in GitHub Desktop.
Save salathe/1000264 to your computer and use it in GitHub Desktop.
<?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