Bazı sunucu ve hostinglerde file_get_contents fonksiyonu güvenlik için kapatılır ve kod bloğunuz çalışmaz. Hata alan arkadaşlar Curl seçeneği hazırlanmış kodu kullanırlarsa sorunsuz çalışacaktır.
<?php
$apiKey = 'Api anahtarınız';
$text = 'Merhaba PHP';
$url = 'https://www.googleapis.com/language/translate/v2?key=' . $apiKey . '&q=' . rawurlencode($text) . '&source=tr&target=en';
$handle = curl_init($url);
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 . '<br>';
echo 'Error description: ' . $responseDecoded['error']['errors'][0]['message'];
}
else {
echo 'Source: ' . $text . '<br>';
echo 'Translation: ' . $responseDecoded['data']['translations'][0]['translatedText'];
}
?>