简体   繁体   English

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

[英]Help unable to understand Google Translate API v2 for PHP?

{---------------------Final Update-----------------} I will need the api for production soon someday, but for now I just wanted to get used to it. {---------------------最终更新-----------------}我很快将需要用于生产的api有一天,但现在我只想习惯它。 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. (但是我最终用了整整两天的时间...)无论如何,我发现为此进行了gtranslate。 So I will look at the actual api sometime in the future when it is further along. 因此,我将在将来的某个时候查看实际的api。 :D :D

Yay I found a work around. 是的,我找到了解决方法。

{--------------------------------UPDATE-------------------------------------------} {-------------------------------- 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. 自从我的原始文章以来,在prodigitalson和David Gillen的帮助下,我已经可以走得更远,但我仍然迷失了方向。

I was getting a mysterious SLL error so I read a bit online and it told me to make a cert. 我遇到了一个神秘的SLL错误,因此我在网上阅读了一些内容,并告诉我制作证书。 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. 无论如何,这样做的结果是我大部分时间都在编程地狱中,因为我试图在无法正常工作的Windows上从源opensll进行编译。 And so I gave up on that Idea and found a post that suggested using curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); 因此,我放弃了这个想法,找到了一条建议使用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. 这是我第一次尝试使用任何种类的API,这比任何事情都令人沮丧。

I'm trying to get the Google Translate API working on PHP. 我正在尝试让Google Translate API在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 我已上下左右阅读本文档: 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 我已经在此处http://code.google.com/apis/language/translate/v2/libraries.html下载了PHP API客户端

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 而且我正在尝试特别使用此脚本: 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. 我不太了解如何实现OOP *,所以这是我的失败。 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. 该文档指出,我需要使用我的api密钥才能使用它,但是我没有找到需要将其放在php api客户端中的位置。

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 谢谢你们

You need to instantiate an api client first (the first file youre requiring)... for example: 您需要首先实例化api客户端(您需要的第一个文件)...例如:

$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. 至于身份验证,请检出: http : //code.google.com/p/google-api-php-client/wiki/UsingTheLibrary注意,该示例使用Buzz服务,而您将使用翻译服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM