[英]Google translate api cUrl not working in a Laravel 5 project
I am getting a server response of 0 each time I try and fetch the response using standard cUrl functionality within a Laravel project. 每次尝试在Laravel项目中使用标准cUrl功能获取响应时,服务器响应都为0。 It has nothing to do with this combination I think as when I access a way other URL it works fine... So I am now kinda lost... 我认为与这种组合无关,因为当我访问其他URL时,它可以正常工作...所以我现在有点迷路了...
In my controller I have a function like below: 在我的控制器中,我具有如下功能:
$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);
The output is zero and the error code is 0 (zero) 输出为零,错误代码为0(零)
While when I access the https://www.googleapis.com/language/translate/v2?key=MY-KEY&source=EN&target=NL&q=Hello%20world URL I get a perfect response "Hallo wereld" 当我访问https://www.googleapis.com/language/translate/v2?key=MY-KEY&source=EN&target=NL&q=Hello%20worldURL时,我得到一个完美的答复“ Hallo Areld”
Using for example $curl = http://www.jsontest.com/ the output is correct. 例如,使用$ curl = http://www.jsontest.com/ ,输出是正确的。
Can someone see what I am doing wrong here? 有人可以在这里看到我做错了吗?
Stichoza/google-translate-php is a nice alternative for what you're trying to perform: 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.