2012-11-04 96 views
2

我想找到一個簡單的教程,瞭解如何讓新的Azure翻譯API與PHP和Curl一起使用。使用Azure Microsoft Translator API與PHP和cURL

有沒有人有一個簡單的函數的示例代碼可以調用來執行字符串的翻譯?

我已經創建了我的用戶帳戶並註冊了一個應用程序。

我正在處理這些示例,但我無法弄清楚如何將它們用作簡單的PHP函數。

http://wangpidong.blogspot.ca/2012/04/how-to-use-new-bing-translator-api-with.html

New Bing API PHP example doesnt work

+0

我在找同樣的東西。這裏有可怕的MSDN文檔... – Bashevis

回答

11

我知道這個問題是一個幾個月大,但因爲我正在處理這個今天我想我會分享我的工作代碼。下面是一個簡單的示例,介紹如何使用主帳戶密鑰和基本身份驗證在Microsoft Translator V2 API中使用翻譯方法。您可以獲取您的主要帳戶密鑰here

// Prepare variables 
$text = urlencode('Hello world.'); 
$from = 'en'; 
$to = 'es'; 

// Prepare cURL command 
$key = 'YOUR_PRIMARY_ACCOUNT_KEY'; 
$ch = curl_init('https://api.datamarket.azure.com/Bing/MicrosoftTranslator/v1/Translate?Text=%27'.$text.'%27&From=%27'.$from.'%27&To=%27'.$to.'%27'); 
curl_setopt($ch, CURLOPT_USERPWD, $key.':'.$key); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

// Parse the XML response 
$result = curl_exec($ch); 
$result = explode('<d:Text m:type="Edm.String">', $result); 
$result = explode('</d:Text>', $result[1]); 
$result = $result[0]; 

echo $result; 

這應返回:

Hola mundo. 

有關GET參數的詳細信息,請參閱MSDN documentation

+0

用戶名和密碼是ACC密鑰?這組代碼將使它工作?這麼短而甜美:哦? – CodeGuru

+0

@RainbowHat:是的,這並不算糟糕,儘管需要一段時間才能凝聚成這樣。 –

+0

不錯的一個:)與微軟的smaple相比真的不錯的代碼,無論如何,我使用谷歌API,微軟不會採取我的中文字符。他們似乎不聰明地閱讀它 – CodeGuru

5

微軟DataMarket翻譯API將停止工作,17年3月31日: https://datamarket.azure.com/dataset/bing/microsofttranslator

所以我做了一個新的樣本PHP /捲曲的代碼,將在今後的工作:

<?php // 4.01.17 AZURE Text Translation API 2017 - PHP Code Example - Cognitive Services with CURL http://www.aw6.de/azure/ 
// Get your key from: http://docs.microsofttranslator.com/text-translate.html 
// Put your parameters here: 
$azure_key = "KEY_1"; // !!! TODO: secret key here !!! 
$fromLanguage = "en"; // Translator Language Codes: https://msdn.microsoft.com/de-de/library/hh456380.aspx 
$toLanguage = "de"; 
$inputStr = "AZURE - The official documentation and examples for PHP are useless."; 
// and leave the rest of the code as it is ;-) 
// Get the AZURE token 
function getToken($azure_key) 
{ 
    $url = 'https://api.cognitive.microsoft.com/sts/v1.0/issueToken'; 
    $ch = curl_init(); 
    $data_string = json_encode('{body}'); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
      'Content-Type: application/json', 
      'Content-Length: ' . strlen($data_string), 
      'Ocp-Apim-Subscription-Key: ' . $azure_key 
     ) 
    ); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_HEADER, false); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
    $strResponse = curl_exec($ch); 
    curl_close($ch); 
    return $strResponse; 
} 
// Request the translation 
function curlRequest($url) 
{ 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, "Content-Type: text/xml"); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, False); 
    $curlResponse = curl_exec($ch); 
    curl_close($ch); 
    return $curlResponse; 
} 
// Get the translation 
$accessToken = getToken($azure_key); 
$params = "text=" . urlencode($inputStr) . "&to=" . $toLanguage . "&from=" . $fromLanguage . "&appId=Bearer+" . $accessToken; 
$translateUrl = "http://api.microsofttranslator.com/v2/Http.svc/Translate?$params"; 
$curlResponse = curlRequest($translateUrl); 
$translatedStr = simplexml_load_string($curlResponse); 
// Display the translated text on the web page: 
echo "<p>From " . $fromLanguage . ": " . $inputStr . "<br>"; 
echo "To " . $toLanguage . ": " . $translatedStr . "<br>"; 
echo date(r) . "<p>"; 
?> 
+0

謝謝Andreas Weygandt - 這對我很好。 –

相關問題