2012-05-25 26 views
6

Czy ktoś ma próbkę kodu PHP opisującą sposób korzystania z interfejsu API tłumaczeń Microsoft Bing z nowym systemem żądania opartym na tokenie? Zarejestrowałem się w usłudze Azure Marketplace, mam identyfikator klienta i "tajny klucz" klienta, ale wszystko, co do tej pory wypróbowałem, powoduje błąd "400 nieprawidłowych żądań". Dziękuję bardzo!Interfejs API PHP i Bing Translation

Poniżej znajduje się raczej podstawowa próbka kodu, który próbowałem (zredagowałem identyfikator klienta i tajną wartość). Działam ze zrozumieniem, że zmienne pocztowe można przekazywać za pośrednictwem żądania adresu URL ... Mam nadzieję, że to prawda.

$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

Hi Użytkownika, zamiast tego, dlaczego nie * Pokaż * Zróbmy to, co zrobiłeś, a następnie zadaj konkretne pytania dotyczące kodu, a następnie pomożemy Ci. Ta strona naprawdę nie polega na pisaniu kodu dla ludzi, ale zamiast tego pomaga, kiedy utkną (bo jesteś ... wystarczy, że opublikujesz swój kod :) Powodzenia! – jmort253

+0

Zrobione ... patrz wyżej. – user1408397

+0

Czy musisz przekazać token do drugiej prośby o curl? http://wangpidong.blogspot.com/2012/04/how-to-use-new-bing-translator-api-with.html – jmort253

Odpowiedz

6

Mam createde małą klasę PHP, który jest łatwy w obsłudze i łatwe do zintegrowania do każdego projektu PHP. Możesz go znaleźć here. Jest to kod:

<?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 Zobacz odpowiedź, edytowałem ją. – torayeff

0

Proszę umieścić ten kod na swojej części nagłówka .. Działa Idealne dla Me..Enjoy go

<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>