• 26-11-2016, 12:13:19
    #1
    $url="https://translation.googleapis.com/language/translate/v2?key=API_KODU_BURAYA&source=$source&target=$target&q=$text";
    
    $json = json_decode(file_get_contents("$url"));
    $translated_text = $json->translatedText;
    echo $translated_text;
    urlyi tarayıcıda açtığım zaman çevirme işlemi yapıyor ancak bir türlü php değişkenine tanımlayamadım sorun nerededir acaba?

    tarayıcıda bu şekilde yazdırıyor.

    {
      "data": {
        "translations": [
          {
            "translatedText": "Nasılsın"
          }
        ]
      }
    }
    yardımlarınız için teşekkür ederim.
  • 27-11-2016, 14:53:38
    #2
    sorun hala devam ediyor
  • 27-11-2016, 14:58:52
    #3
    Üyeliği durduruldu
    Bu şekilde kullanırsanız çalışacaktır.

    $url="https://translation.googleapis.com/language/translate/v2?key=API_KODU_BURAYA&source=$source&target=$target&q=$text"; 
    
    $json = json_decode(file_get_contents("$url"), true); 
    
    $sonuc = $json['data']['translations'][0]['translatedText'];
    
    echo $sonuc;
  • 27-11-2016, 20:30:12
    #4
    Rise adlı üyeden alıntı: mesajı görüntüle
    Bu şekilde kullanırsanız çalışacaktır.

    $url="https://translation.googleapis.com/language/translate/v2?key=API_KODU_BURAYA&source=$source&target=$target&q=$text"; 
    
    $json = json_decode(file_get_contents("$url"), true); 
    
    $sonuc = $json['data']['translations'][0]['translatedText'];
    
    echo $sonuc;
    Allah razı olsun.
    Çalıştı
  • 14-06-2017, 14:46:52
    #5
    Üyeliği durduruldu
    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'];
        }
    ?>