2012-11-04 99 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 - 这对我很好。 –

相关问题