简体   繁体   English

谷歌翻译API使用CURL post方法

[英]Google translator API using CURL post method

Did anyone have any experience in Google translator API v2 for translating HTML using PHP CURL on POST method? 有没有人有过使用PHP CURL在POST方法上翻译HTML的Google翻译API v2的经验?

I have tried several codes and libraries from github, but none of them worded for me. 我从github尝试了几个代码和库,但没有一个代表我。 What I have found is GET methods. 我发现的是GET方法。

Due to limitation for parsing data over GET or query string, I am unable to send large HTML data to translate. 由于通过GET或查询字符串解析数据的限制,我无法发送大型HTML数据进行翻译。

I am looking for a solution/snippets which can translate buffered data using using CURL 我正在寻找一个解决方案/片段,可以使用CURL转换缓冲数据

Here is my solution 这是我的解决方案

$handle = curl_init();

if (FALSE === $handle)
   throw new Exception('failed to initialize');

curl_setopt($handle, CURLOPT_URL,'https://www.googleapis.com/language/translate/v2');
curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($handle, CURLOPT_POSTFIELDS, array('key'=> 'apikey', 'q' => 'Testing message', 'source' => 'en', 'target' => 'cs'));
curl_setopt($handle,CURLOPT_HTTPHEADER,array('X-HTTP-Method-Override: GET'));
$response = curl_exec($handle);

You can also use POST to invoke the API if you want to send more data in a single request. 如果要在单个请求中发送更多数据,也可以使用POST来调用API。 The q parameter in the POST body must be less than 5K characters. POST正文中的q参数必须小于5K字符。 To use POST, you must use the X-HTTP-Method-Override header to tell the Translate API to treat the request as a GET (use X-HTTP-Method-Override: GET). 要使用POST,必须使用X-HTTP-Method-Override标头告诉Translate API将请求视为GET(使用X-HTTP-Method-Override:GET)。

Google Translate API Documentation Google Translate API文档

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

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