0

我之前有 ZF 的第一个旧功能,但不知何故 ZF 它失败了,然后我制作了 unittest 模块。

我正在尝试使用谷歌翻译 v2,但它不再工作了,谷歌是否停止了服务供公众使用,或者它是 PHP Bug 还是其他非常令人困惑的地方。

始终使用以下两个函数返回 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 回答