0

以前はZFの最初の古い関数が機能していましたが、どういうわけかZFが失敗していたので、ユニットテストモジュールを作成しました。

Google 翻訳 v2 を使用しようとしていますが、もう機能しません。Google は公共の使用のためにサービスを停止したのでしょうか、それとも PHP のバグでしょうか、それとも他の場所で非常に混乱しているのでしょうか。

次の両方の関数で常に 403 を返します。

何がうまくいかないのですか?

<?php
## Test: How to's
/*
$ php tst.php 
403

$ curl http://ajax.googleapis.com/ajax/services/language/translate -d "v=1.0&q=dog&langpair=en|ru" -H "Referer: http://google.com"
{"responseData": null, "responseDetails": "Please use Translate v2.  See http://code.google.com/apis/language/translate/overview.html", "responseStatus": 403}sun@sun-M14xR2:/var/www/html/vooyz.com/unittest$ 

*/

// V1 - Old not working
function googleTranslatePostV1($text, $destLang = 'nl', $srcLang = 'en') {
  $url = 'http://ajax.googleapis.com/ajax/services/language/translate';
  $http_response = '';
  $text = urlencode($text);
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_REFERER, !empty($_SERVER["HTTP_REFERER"]) ? $_SERVER["HTTP_REFERER"] : "");
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, "v=1.0&q=" . $text . "&langpair=$srcLang|$destLang");
  $http_response = curl_exec($ch);
  $json = json_decode($http_response, true);

  if ($json['responseStatus'] != '200') {
    return $json['responseStatus'];
  } else {
    return $json['responseData']['translatedText'];
  }
  curl_close($ch);
}

// V2 - Curl way not working
function googleTranslatePostV2($text, $destLang = 'nl', $srcLang = 'en') {
  $url = 'https://www.googleapis.com/language/translate/v2';
  $http_response = '';
  $text = urlencode($text);
  $postArr = array('key' => 'sdfdsfdsfds',
          'q' => $text,
          'source' => $srcLang,
          'target' => $destLang);

  $ch = curl_init();    
  //curl_setopt($ch, CURLOPT_POSTFIELDS,'hl=en&ie=UTF8&text=-->this+is+a+test<--&langpair=en%7Car');      
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);       
  curl_setopt($ch, CURLOPT_REFERER, !empty($_SERVER["HTTP_REFERER"]) ? $_SERVER["HTTP_REFERER"] : "");
  curl_setopt($ch, CURLOPT_POST,true);
  curl_setopt($ch, CURLOPT_POSTFIELDS,$postArr);        
  $http_response = curl_exec($ch);

  var_dump($http_response);

  $json = json_decode($http_response, true);

  if ($json['responseStatus'] != '200') {
    return $json['responseStatus'];
  } else {
    return $json['responseData']['translatedText'];
  }
  curl_close($ch);
}


// V2 - Google way
function googleTranslateV2Method1($text, $destLang = 'nl', $srcLang = 'en') {
  require_once 'google/src/Google_Client.php';
  require_once 'google/src/contrib/Google_TranslateService.php';
  $client = new Google_Client();
  $client->setApplicationName('Google Translate PHP Starter Application');
  $client->setDeveloperKey('dsfdsfdsf');
  $service = new Google_TranslateService($client);

  //$langs = $service->languages->listLanguages();
  //print "<h1>Languages</h1><pre>" . print_r($langs, true) . "</pre>";
  $translations = $service->translations->listTranslations($text, 'hi');

  return $translations;
}

echo googleTranslatePostV1("V1: " . "How are you?") . "\n";
echo googleTranslatePostV2("V2: " . "How are you?") . "\n";
echo googleTranslateV2Method1("V2: " . "How are you?") . "\n";


?>
4

1 に答える 1

0
$ curl http://ajax.googleapis.com/ajax/services/language/translate -d "v=1.0&q=dog&langpair=en|ru" -H "Referer: http://google.com"

{"responseData": null, "responseDetails": "Please use Translate v2.  See http://code.google.com/apis/language/translate/overview.html", "responseStatus": 403}

http://code.google.com/apis/language/translate/overview.html :
Google Translate API は有料サービスとして利用できます。詳細については、価格とよくある質問のページをご覧ください。

于 2012-12-15T13:22:32.180 に答える