Hello!

Here I found a translator script , uses version v2 (just what's in the API specification)

It seems everything in the script corresponds to the API bing at the moment, but this script can translate only one word.

If I do not translate more, I can not understand why - can anyone be able to explain?

This script has a minimum of code if you compare it with the fact that I posted above

Please help me figure it out!

<?php $your_client_id = '0750facb-bb29-43b4-a629-42603c2efbea'; $your_secret = 'I3GDuMftmn4pTeUTiJgJp0VGNbEkfDbBgwFDC7eb6E0'; 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]; } } //usage example $bt = new BingTranslation($your_client_id, $your_secret); echo $bt->translate('home', 'en', 'ru'); ?> 
  • one
    Do not write comments with the answer - Naumov

1 answer 1

As long as you send one word in Latin, your HTTP request is correct. Only this is nothing more than an accident.

Send a request with Cyrillic content - this will be a violation of the RFC, but not fatal. In the harsh outside world, servers have to forgive this. But the space - completely breaks the structure of the request and it turns out the request:

 GET /V2/Http.svc/Translate?text=привет мир&from=ru&to=en HTTP/1.1 

Of course, nothing else but HTTP 400 can answer you. It is very sad that you do not check curl_errno (although it is not curl_errno for this problem, but it will help with other network problems) or curl_getinfo .

And the correct URL must be URL-encoded.

 $url = 'http://api.microsofttranslator.com/V2/Http.svc/Translate?text='.urlencode($word).'&from='.urlencode($from).'&to='.urlencode($to); 

Or do not pervert, but form the correct query string using a native function:

 $url = 'http://api.microsofttranslator.com/V2/Http.svc/Translate?'.http_build_query([ 'text' => $word, 'from' => $from, 'to' => $to, ]); 

    Protected by a community spirit 5 Oct '16 at 10:20 .

    Thank you for your interest in this issue. Since he collected a large number of low-quality and spam responses, which had to be deleted, now it’s necessary to have 10 reputation points on the site (the bonus for account association is not counted ).

    Maybe you want to answer one of the unanswered questions ?