【问题标题】:Help unable to understand Google Translate API v2 for PHP?帮助无法理解 Google Translate API v2 for PHP?
【发布时间】:2011-04-02 02:01:39
【问题描述】:

{---------------------最终更新-----------------} 有一天我很快就会需要这个 api 来生产,但现在我只是想习惯它。我将它用于一次性交易以节省时间。 (但我最终用了整整两天......)无论如何我为此找到了gtranslate。所以我会在未来某个时候查看实际的 api,因为它会更进一步。 :D

是的,我找到了解决办法。

{--------------------------------更新------------- ------------------------------}

自从我发表最初的帖子以来,在 prodigitalson 和 David Gillen 的帮助下,我已经能够走得更远,但我仍然迷茫。

我遇到了一个神秘的 SLL 错误,所以我在网上阅读了一下,它告诉我要制作证书。无论如何,结果是我早上的大部分时间都在编程地狱,因为我试图从 Windows 上的源代码 opensll 编译,但它不起作用。所以我放弃了这个想法,找到了一篇建议使用 curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); 的帖子。行并解决了这个问题,现在我得到的错误是:

Fatal error: Uncaught exception 'apiAuthException' with message
'Couldn't fetch request token, http code: 400, response body: Invalid scope:
https://www.googleapis.com/auth/translate ' in 
C:\xampp\htdocs\translate\google-api-php-client2\src\auth\apiOAuth.php:191 
Stack trace: 
#0 
C:\xampp\htdocs\translate\google-api-php-client2\src\auth\apiOAuth.php(169):
apiOAuth->requestRequestToken('http://localhos...') 
#1 C:\xampp\htdocs\translate\google-api-php-client2\src\auth\apiOAuth.php(87): 
apiOAuth->obtainRequestToken('http://localhos...', '4d9609c2bc4ce') 
#2 C:\xampp\htdocs\translate\google-api-php-client2\src\apiClient.php(131): 
apiOAuth->authenticate(Object(apiFileCache), Object(apiCurlIO), Array) 
#3 C:\xampp\htdocs\translate\lang.php(20): apiClient->authenticate() 
#4 {main} thrown in C:\xampp\htdocs\translate\google-api-php-client2\src\auth\apiOAuth.php on line 191

我现在正在使用此代码,注释行不会影响错误消息

<?php
//session_start();
//$_session['auth_token'] = 'AIzaSyAdPfnEsdsQQ6AxSn6K78LznZXHfHZIB3M';

require_once('/google-api-php-client2/src/apiClient.php');
require_once('/google-api-php-client2/src/contrib/apiTranslateService.php');

$apiClient = new apiClient();
$translate = new apiTranslateService($apiClient);

//If a oauth token was stored in the session, use that- and otherwise go through the oauth dance
if (isset($_SESSION['auth_token'])) {
$apiClient->setAccessToken($_SESSION['auth_token']);
}
else {
//In a real application this would be stored in a database, not in the session
$_SESSION['auth_token'] = $apiClient->authenticate();
}

//$translate->listTranslations('Hello to the world of space', 'fr', 'text', 'en');

{-------------------------下面是原帖--------- -----------------------}

这是我第一次尝试使用任何类型的 API,这比任何事情都更令人沮丧。

我正在尝试让 Google Translate API 在 PHP 上运行。不幸的是,文档不是虚拟证明,所以我迷路了。

我已经上下左右阅读了这份文件:http://code.google.com/apis/language/translate/v2/getting_started.html

我已经下载了此处显示的 PHP API 客户端http://code.google.com/apis/language/translate/v2/libraries.html

我特别想使用这个脚本: http://code.google.com/p/google-api-php-client/source/browse/trunk/src/contrib/apiTranslateService.php

我不太了解如何实现 OOP*,所以这是我的失败。文档说明我需要使用我的 api 密钥来使用它,但我还没有找到需要将它放在 php api 客户端中的位置。

到目前为止,这是我尝试过的:

<?php
include('../google-api-php-client/src/apiClient.php');
include('../google-api-php-client/src/contrib/apiTranslateService.php');
$a = new apiTranslateService;
$a->listTranslateions('Hello to the world of space', 'fr', 'text', 'en');
?>

这是我的结果

Catchable fatal error: 
Argument 1 passed to apiTranslateService::__construct() 
must be an instance of apiClient, none given, called in     
C:\xampp\htdocs\translate\lang.php on line 7 and defined in 
C:\xampp\htdocs\google-api-php-client\src\contrib\apiTranslateService.php on line 38  

*如果你知道一个很好的网站链接来教我oop,请随时把它留给我。

谢谢大家

【问题讨论】:

  • 行“$a = new apiTranslateService;”应该看起来像“$a = new apiTranslateService($apiClient);”根据您遇到的错误,在前一行实例化了 $apiClient。
  • $_session['auth_token'] 应该是 $_SESSION['auth_token'],因为它是区分大小写的变量。不确定这是否与您的问题有关 - 我从未使用过此 API :-)

标签: php oop google-api translate


【解决方案1】:

您需要先实例化一个 api 客户端(您需要的第一个文件)...例如:

$client = new apiClient();
// do your auth with the client here

$translateService = new apiTranslateService($client);

至于验证结帐:http://code.google.com/p/google-api-php-client/wiki/UsingTheLibrary 请注意,该示例使用 Buzz 服务,而您将使用翻译服务。

【讨论】:

  • 感谢这确实帮助我解决了这个问题,但我仍然遇到了一些问题,我更新了我的问题。
猜你喜欢
  • 2018-12-21
  • 1970-01-01
  • 1970-01-01
  • 2013-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-15
相关资源
最近更新 更多