0

{---------------------Final Update-----------------} 我很快就会需要 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,请随时把它留给我。

谢谢你们

4

1 回答 1

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 服务,而您将使用翻译服务。

于 2011-03-31T22:19:11.433 回答