Ejemplo n.º 1
0
        curl_setopt($ch, CURLOPT_URL, "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/");
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $result = curl_exec($ch);
        $result = json_decode($result);
        if (!is_object($result) || empty($result->access_token)) {
            die('Problems');
        }
        $this->_token = $result->access_token;
        curl_close($ch);
    }
    public function translate($content, $languageFrom = 'ru', $languageTo = 'de')
    {
        $postData = array('text' => $content, 'from' => $languageFrom, 'to' => $languageTo, 'contentType' => 'text/plain');
        $ch = curl_init('http://api.microsofttranslator.com/V2/Http.svc/Translate?' . http_build_query($postData));
        curl_setopt($ch, CURLOPT_POST, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $this->_token));
        $translation = curl_exec($ch);
        return trim($translation);
    }
}
$text = 'Виталий - это работает!!!';
$bing = new BingTranslator();
$bing->getToken();
$translation = $bing->translate($text);
file_put_contents('trans.txt', [$text => $translation]);
$echo = file_get_contents('trans.txt', $translation);
echo $echo;