每次我尝试使用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%20world URL时,我得到一个完美的响应" hello wereld"
例如使用$curl = http://www.jsontest.com/输出是正确的。
有人能看到我做错了什么吗?
Stichoza/google-translate-php是您尝试执行的一个很好的替代方案:
use Stichoza'GoogleTranslate'TranslateClient;
$tr = new TranslateClient('en', 'nl');
dd($tr->getResponse('Hello World'));