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

发布于 2024-10-28 06:54:34 字数 3928 浏览 4 评论 0原文

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

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

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

自从我发表最初的文章以来,在 prodigitalson 和 David 的帮助下,我已经取得了一些进展吉伦,但我还是迷失了方向。

我遇到了一个神秘的 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,请随时将其留给我。

谢谢大家

{---------------------Final Update-----------------}
I will need the api for production soon someday, but for now I just wanted to get used to it. And I was using it for a one time deal to save time. (But I ended up using two full days...) Anyways I found gtranslate for this. So I will look at the actual api sometime in the future when it is further along. :D

Yay I found a work around.

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

Since my original post I've been able to get a little farther with the help of prodigitalson, and David Gillen but I'm still at a lost.

I was getting a mysterious SLL error so I read a bit online and it told me to make a cert. Anyways the result of that is I spent most of the morning in programming hell as I tried to compile from source opensll on windows which just didn't work. And so I gave up on that Idea and found a post that suggested using curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); line and that dealt with the issue, Now the error I'm getting is:

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

I'm now using this code to which the commented lines to not affect the error message

<?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');

{----------------------------------ORIGINAL POST BELOW--------------------------------}

This is my first time trying to use an API of any sort and it's more frustrating than anything.

I'm trying to get the Google Translate API working on PHP. Unfortunately the documentation isn't dummy proof so I'm lost.

I've read this document up and down and side to side : http://code.google.com/apis/language/translate/v2/getting_started.html

I've downloaded the PHP API Client shown here http://code.google.com/apis/language/translate/v2/libraries.html

And I'm trying to use this script in particular:
http://code.google.com/p/google-api-php-client/source/browse/trunk/src/contrib/apiTranslateService.php

I don't really understand how to implement OOP* so this is my downfall. The documentation states that I need to use my api key to use it but I haven't found where I need to put that in the php api client.

So far this is what I've attempted:

<?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');
?>

And this is my result

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  

*If you know of a good website link to teach me oop feel free to leave it with me.

Thank you all

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蒗幽 2024-11-04 06:54:34

您需要首先实例化一个 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 服务,而您将正在使用翻译服务。

You need to instantiate an api client first (the first file youre requiring)... for example:

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

$translateService = new apiTranslateService($client);

As far as authenticating check out: http://code.google.com/p/google-api-php-client/wiki/UsingTheLibrary note the example uses the Buzz service whereas you will be using the translate service.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文