Mymemory翻译API - CURL不起作用

时间:2016-02-03 10:04:47

标签: php curl

我现在正在使用带有API的Mymemory翻译API。我不断收到很多错误,但它不起作用。我使用file_get_contents得到HTTP失败错误。

我使用此代码:

$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);

如果我var_dump $curl变量,我会得到:resource(2) of type (curl)

如果我var_dump $output变量,我明白了:

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

如果我var_dump curl_error函数,我会得到:string(0) ""

如果我var_dump curl_errno函数,我会得到:int(0)

我打开了我的错误,并且它也没有给出任何错误,也包含error_reporting。我不知道自己在做什么。我先通过我的xampp服务器localhost进行测试。

我尝试使用除https://之外的http://,但没有改变任何内容。如果我直接将网址用作$.get,它确实有效。我不知道自己做错了什么。

1 个答案:

答案 0 :(得分:0)

谢谢@Passerby:

不得不使用urlencode而不是参数:

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