帮助无法理解用于PHP的Google Translate API v2?

时间:2011-03-31 22:07:36

标签: php oop google-api translate

{---------------------最终更新-----------------} 我有一天会很快需要api进行生产,但是现在我只是想要习惯它。而我一次用它来节省时间。 (但我结束了整整两天......)无论如何,我发现了gtranslate。因此,我会在将来的某个时候看看实际的api。 :d

我找到了解决方法。

{-------------------------------- ------------- UPDATE ------------------------------}

自从我的原始帖子以来,我已经能够在prodigitals和David Gillen的帮助下得到更远的距离,但我仍然处于迷失状态。

我收到了一个神秘的SLL错误,所以我在网上看了一下,它告诉我要做证书。无论如何,结果就是我早上大部分时间都在编程地狱,因为我试图从Windows上的源代码openll进行编译,但这些代码无效。所以我放弃了这个想法并发现了一篇建议使用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随时随地留下它。

谢谢大家

1 个答案:

答案 0 :(得分: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服务,而您将使用翻译服务。