簡體   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