Vai*_*pta 5 javascript php ajax curl http-status-code-400
我正在尝试使用免费的 Google Translate API,它是从 Firefox 的 S3 Google Translator 插件中提取的,即。
\n\nhttps://translate.google.com/translate_a/single?client=t&sl=auto&\ntl=en&hl=en&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t\n&dt=at&ie=UTF-8&oe=UTF-8&otf=2&srcrom=1&ssel=0&tsel=0&q=Hello\nRun Code Online (Sandbox Code Playgroud)\n\n在 PHP cURL 中,即。
\n\n$isPOST=isset($_POST) && !empty($_POST);\n$q=$isPOST ? $_POST['q'] : $_GET['q'];\n\n$url='https://translate.google.com/translate_a/single';\n$data='client=t&sl=auto&tl=en&hl=en&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&dt=at&ie=UTF-8&oe=UTF-8&otf=2&srcrom=1&ssel=0&tsel=0&q='.$q;\n$ch=curl_init();\ncurl_setopt($ch, CURLOPT_HEADER, 0);\ncurl_setopt($ch, CURLOPT_NOBODY, 0);\ncurl_setopt($ch, CURLOPT_URL, !$isPOST ? $url.'?'.$data : $url);\ncurl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);\ncurl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7');\ncurl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);\ncurl_setopt($ch, CURLOPT_REFERER, $url);\ncurl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);\ncurl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);\nif($isPOST){\n curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');\n curl_setopt($ch, CURLOPT_POST, 1);\n curl_setopt($ch, CURLOPT_POSTFIELDS, $data);\n}\n$return=curl_exec($ch);\ncurl_close($ch);\nRun Code Online (Sandbox Code Playgroud)\n\n我正在使用ajax调用此页面..
\n\n$.ajax({\n type: text.length>750 ? 'post' : 'get',\n url: 'translate.php',\n data: 'q='+text,\n success: function(d){ alert(d); }\n});\nRun Code Online (Sandbox Code Playgroud)\n\n但完成这一切后,我得到了谷歌翻译的回应,即。
\n\nError: 400. That\xe2\x80\x99s an error.\nYour client has issued a malformed or illegal request. That\xe2\x80\x99s all we know.\nRun Code Online (Sandbox Code Playgroud)\n\n请帮我解决这个错误并获取翻译文本..
\n| 归档时间: |
|
| 查看次数: |
4913 次 |
| 最近记录: |