Google翻译API使用CURL post方法 [英] Google translator API using CURL post method
问题描述
有没有人在Google翻译API v2上使用PHP CURL在POST方法上翻译HTML?
Did anyone have any experience in Google translator API v2 for translating HTML using PHP CURL on POST method?
我已经尝试过几个来自github的代码和库,他们对我说话。我找到的是GET方法。
I have tried several codes and libraries from github, but none of them worded for me. What I have found is GET methods.
由于通过GET或查询字符串解析数据的限制,我无法发送大的HTML数据进行翻译。
Due to limitation for parsing data over GET or query string, I am unable to send large HTML data to translate.
我正在寻找一个可以使用CURL翻译缓冲数据的解决方案/ snippets
I am looking for a solution/snippets which can translate buffered data using using CURL
推荐答案
这是我的解决方案
$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);
如果要在单个请求中发送更多数据,还可以使用POST调用API。 POST主体中的q参数必须小于5K个字符。要使用POST,必须使用X-HTTP-Method-Override标头来告诉Translate API将请求视为GET(使用X-HTTP-Method-Override:GET)。
You can also use POST to invoke the API if you want to send more data in a single request. The q parameter in the POST body must be less than 5K characters. 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).
这篇关于Google翻译API使用CURL post方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!