繁体   English   中英

Google翻译api cUrl在Laravel 5项目中不起作用

[英]Google translate api cUrl not working in a Laravel 5 project

每次尝试在Laravel项目中使用标准cUrl功能获取响应时,服务器响应都为0。 我认为与这种组合无关,因为当我访问其他URL时,它可以正常工作...所以我现在有点迷路了...

在我的控制器中,我具有如下功能:

$curl = "https://www.googleapis.com/language/translate/v2?key=MY-KEY&source=EN&target=NL&q=Hello%20world";
echo $curl;
$handle = curl_init($curl);
curl_setopt($handle, CURLOPT_URL, $curl);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($handle);
$responseDecoded = json_decode($response, true);

$responseCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
if($responseCode != 200) {
    echo 'Fetching translation failed! Server response code:' . $responseCode;
}
else {
    echo 'Source: ' . $text . '<br>';
    echo $responseDecoded['data'];
}
curl_close($handle);

输出为零,错误代码为0(零)

当我访问https://www.googleapis.com/language/translate/v2?key=MY-KEY&source=EN&target=NL&q=Hello%20worldURL时,我得到一个完美的答复“ Hallo Areld”

例如,使用$ curl = http://www.jsontest.com/ ,输出是正确的。

有人可以在这里看到我做错了吗?

Stichoza / google-translate-php是您尝试执行的一种不错的选择:

use Stichoza\GoogleTranslate\TranslateClient;

$tr = new TranslateClient('en', 'nl');
dd($tr->getResponse('Hello World'));

暂无
暂无

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

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