简体   繁体   English

Mymemory翻译API-CURL不起作用

[英]Mymemory translate API - CURL not working

I'm right now using the Mymemory translation API with API. 我现在正在将Mymemory翻译API与API结合使用。 I keep on getting a lot of errors it doesn't work. 我不断收到很多错误,但仍然无法正常工作。 I do get a HTTP failed error with file_get_contents. 我确实收到了file_get_contents的HTTP失败错误。

I use this code: 我使用以下代码:

$text = filter_input(INPUT_GET, 'text');
$from = filter_input(INPUT_GET, 'from');
$to = filter_input(INPUT_GET, 'to');

$url = 'http://mymemory.translated.net/api/get?q=' . $text . '&langpair=' . $from . '|' . $to . '&de=SOMEEMAIL';
$curl = curl_init ($url);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

$output = curl_exec ($curl);

If I var_dump the $curl variable, I get this: resource(2) of type (curl) . 如果我var_dump $curl变量, var_dump得到以下信息: resource(2) of type (curl)

If I var_dump the $output variable, I get this: 如果我var_dump $output变量,我得到以下信息:

string(90) "<html><body><h1>400 Bad request</h1>
Your browser sent an invalid request.
</body></html>
"

If I var_dump the curl_error function, I get this: string(0) "" 如果我var_dump curl_error函数, curl_error得到以下信息: string(0) ""

If I var_dump the curl_errno function, I get this: int(0) 如果我var_dump curl_errno函数, curl_errno得到以下信息: int(0)

I got my errors turned on and it doesn't give me any errors with error_reporting either. 我打开了我的错误,并且error_reporting也没有给我任何错误。 I don't know what I'm doing. 我不知道我在做什么 I'm testing by the way through my xampp server localhost first. 我首先通过xampp服务器本地主机进行测试。

I tried using https:// except of http:// but didn't change anything. 我尝试使用https://http:// https://除外),但未进行任何更改。 If I use the url directly as $.get , it does work. 如果我直接将URL用作$.get ,它将起作用。 I don't know what I'm doing wrong. 我不知道我在做什么错。

Solved thanks to @Passerby: 通过@Passerby解决:

Had to use urlencode over the params: 必须在参数上使用urlencode

$text = urlencode(filter_input(INPUT_GET, 'text'));
$from = urlencode(filter_input(INPUT_GET, 'from'));
$to = urlencode(filter_input(INPUT_GET, 'to'));

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

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