0

i have this:-

$str = 'hello';

$url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=fr&dt=t&q=" . $str;

$R=file_get_contents($url);

print($R);

and i get:-

 [[["Bonjour","hello",null,null,1] ] ,null,"en",null,null,null,1.0,[] ,[["en"] ,null,[1.0] ,["en"] ] ]

could you help me to get access to 'Bonjour' and 'en'?

its a string, im trying to decode, uncode, etc, but nothing work to get something normal. thank you in advance

Markus AO
  • 4,771
  • 2
  • 18
  • 29
ivass
  • 17
  • 3
  • 1
    Please show us what you've tried to decode this JSON string. – Markus AO Jul 22 '20 at 15:46
  • 2
    Does this answer your question? [How do I extract data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – Markus AO Jul 22 '20 at 15:48

1 Answers1

0

The data you receive is in json so you should decode it.

$str = 'hello';
$url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=fr&dt=t&q=" . $str;

$result = file_get_contents($url);
$data = json_decode($result);

//print_r($data);

$bonjour = $data[0][0][0];
$en = $data[2];

echo $bonjour . ' ' . $en;