2012-05-25 80 views
6

有谁对如何使用微软Bing翻译API与他们的新的基于令牌的请求,系统中的PHP代码示例?我已经注册了Azure Marketplace,我有一个客户端ID和一个客户端“秘密”,但是我迄今为止尝试过的所有内容都会导致“400错误请求”错误。非常感谢!PHP和Bing翻译API

下面是代码的一个相当基本的样品我一直想(我节录客户端ID和密钥值)。我正在理解post变量可以通过URL请求传递......我希望这是正确的。

$authURL = 'http://datamarket.accesscontrol.windows.net/v2/OAuth2-13&grant_type=client_credentials&client_id={CLIENT_ID VALUE HERE}&client_secret={CLIENT_SECRET VALUE HERE}&scope=http://api.microsofttranslator.com'; 
$chpre = curl_init(); 
curl_setopt($chpre, CURLOPT_URL, $authURL); 
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
$xpre = curl_exec($chpre); 

$texttobetranslated = "الذي تقدمه"; 
$BingURL = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text=" . $texttobetranslated . "&from=ar&to=en"; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $BingURL); 
$x = curl_exec($ch); 
+0

嗨用户,相反,你为什么不*显示*我们做了什么,然后询问关于代码的具体问题,然后我们可以更好地帮助您。这个网站并不是真的为人们编写代码,而是在他们被困时帮助(你是......你只需要发布你的代码:)祝你好运! – jmort253

+0

完成...见上面。 – user1408397

+0

您是否需要将令牌传递给第二个卷曲请求? http://wangpidong.blogspot.com/2012/04/how-to-use-new-bing-translator-api-with.html – jmort253

回答

6

我已经createde小PHP类,它是易于使用和易于集成到任何PHP项目给出。你可以找到它here。这是代码:

<?php 
    class BingTranslation 
    { 
     public $clientID; 
     public $clientSecret; 

     public function __construct($cid, $secret) 
     { 
      $this->clientID = $cid; 
      $this->clientSecret = $secret; 
     } 

     public function get_access_token() 
     { 
      //if access token is not expired and is stored in COOKIE 
      if(isset($_COOKIE['bing_access_token'])) 
       return $_COOKIE['bing_access_token']; 

      // Get a 10-minute access token for Microsoft Translator API. 
      $url = 'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13'; 
      $postParams = 'grant_type=client_credentials&client_id='.urlencode($this->clientID). 
      '&client_secret='.urlencode($this->clientSecret).'&scope=http://api.microsofttranslator.com'; 

      $ch = curl_init(); 
      curl_setopt($ch, CURLOPT_URL, $url); 
      curl_setopt($ch, CURLOPT_POSTFIELDS, $postParams); 
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
      $rsp = curl_exec($ch); 
      $rsp = json_decode($rsp); 
      $access_token = $rsp->access_token; 

      setcookie('bing_access_token', $access_token, $rsp->expires_in); 

      return $access_token; 
     } 

     public function translate($word, $from, $to) 
     { 
      $access_token = $this->get_access_token(); 
      $url = 'http://api.microsofttranslator.com/V2/Http.svc/Translate?text='.$word.'&from='.$from.'&to='.$to; 

      $ch = curl_init(); 
      curl_setopt($ch, CURLOPT_URL, $url); 
      curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization:bearer '.$access_token)); 
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
      $rsp = curl_exec($ch); 

      preg_match_all('/<string (.*?)>(.*?)<\/string>/s', $rsp, $matches); 

      return $matches[2][0]; 
     } 

     public function translate2($word, $from, $tos) 
     { 
      //translates 1 word to several languages 
      //$tos is array of languages to translate to 
      //returns array of translations as $result['en']=>'Hello' 

      $access_token = $this->get_access_token(); 

      $result[$from] = $word; 

      foreach($tos as $to) 
      { 
       $url = 'http://api.microsofttranslator.com/V2/Http.svc/Translate?text=hello&from='.$from.'&to='.$to; 

       $ch = curl_init(); 
       curl_setopt($ch, CURLOPT_URL, $url); 
       curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization:bearer '.$access_token)); 
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
       $rsp = curl_exec($ch); 

       preg_match_all('/<string (.*?)>(.*?)<\/string>/s', $rsp, $matches); 

       $result[$to] = $matches[2][0]; 
      } 

      return $result; 
     } 
    } 
?> 
+0

@kleopatra查看答案,我已编辑它。 – torayeff

0

请把这段代码在你的头部分.. 这个作品非常适合Me..Enjoy它

<div id='MicrosoftTranslatorWidget' class='Light' style='color:white;background-color:#555555'></div> 
<script type='text/javascript'> 
setTimeout(function(){{ 
var s=document.createElement('script'); 
s.type='text/javascript'; 
s.charset='UTF-8'; 
s.src=((location && location.href && location.href.indexOf('https') == 0)?'https://ssl.microsofttranslator.com':'http://www.microsofttranslator.com')+'/ajax/v3/WidgetV3.ashx?siteData=ueOIGRSKkd965FeEGM5JtQ**&ctf=False&ui=true&settings=Manual&from='; 
var p=document.getElementsByTagName('head')[0]||document.documentElement; 
p.insertBefore(s,p.firstChild); 
}},0); 
</script>