Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select a node from json string

Tags:

json

php

I am trying to use the google translation API as shown on this page...

https://developers.google.com/translate/v2/using_rest

When I replace my API key, it works correctly and display the translated text as shown below.

GET https://www.googleapis.com/language/translate/v2?key=INSERT-YOUR-KEY&target=de&q=Hello%20world

{
    "data": {
        "translations": [
            {
                "translatedText": "Hallo Welt",
                "detectedSourceLanguage": "en"
            }
        ]
    }
}

I will like to return only the text i.e. "Hallo Welt" using PHP.

I used json_decode function, but it returns everything.

like image 407
shantanuo Avatar asked May 11 '26 10:05

shantanuo


1 Answers

$url = "https://www.googleapis.com/language/translate/v2?key=INSERT-YOUR-KEY&target=de&q=Hello%20world";
$data = file_get_contents($url);
$json = json_decode($data);

echo $json->data->translations[0]->translatedText;
like image 179
flux Avatar answered May 13 '26 00:05

flux