soma56 Posted April 18, 2012 Share Posted April 18, 2012 I've signed up for Google's translating service and I'm able to get results, however, it's wrapped around some information. Here's an example: //Content to translate - Spanish for 'Hello World' $content = urlencode("Hola Mundo"); $translate = file_get_contents("https://www.googleapis.com/language/translate/v2?key=MyKey&q=$content&source=es&target=en"); echo $translate; The code returns { "data": { "translations": [ { "translatedText": "Hello World" } ] } } All I want is the result Hello World I tried using str_replace but it does not work through file_get_contents. The Google Translate does not provide any information on how to do this. Does anyone have any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/261196-anyone-familiar-with-google-translate-api/ Share on other sites More sharing options...
plznty Posted April 18, 2012 Share Posted April 18, 2012 It is using JSON - http://stackoverflow.com/questions/1493588/php-json-management-get-a-value-from-a-decoded-json-object Quote Link to comment https://forums.phpfreaks.com/topic/261196-anyone-familiar-with-google-translate-api/#findComment-1338551 Share on other sites More sharing options...
soma56 Posted April 18, 2012 Author Share Posted April 18, 2012 Thanks, this is where I'm at right now: $translate = file_get_contents("https://www.googleapis.com/language/translate/v2?key=MyKey&q=$content&source=es&target=en"); $obj = json_decode($translate); print "TEST 1".$obj->{'translatedText'}; echo "<br />"; $response = json_decode($translate); echo "TEST 2".$response->data->translations->translatedText; echo "<br />"; echo "TEST 3".$response->translatedText; echo "<br />"; $response2 = json_decode($translate, true); echo "TEST 4".$response2['translatedText']; echo "<br />"; No results returned but 'json' is working based on an example from php.net. Got a feeling I'm in the right direction? Quote Link to comment https://forums.phpfreaks.com/topic/261196-anyone-familiar-with-google-translate-api/#findComment-1338561 Share on other sites More sharing options...
samshel Posted April 18, 2012 Share Posted April 18, 2012 worth a try. $translate = file_get_contents("https://www.googleapis.com/language/translate/v2?key=MyKey&q=$content&source=es&target=en"); $arr = json_decode($translate, true); var_dump($arr); echo $arr['data']['translations']['translatedText']; Quote Link to comment https://forums.phpfreaks.com/topic/261196-anyone-familiar-with-google-translate-api/#findComment-1338568 Share on other sites More sharing options...
soma56 Posted April 18, 2012 Author Share Posted April 18, 2012 Thanks, getting somewhere: array(1) { ["data"]=> array(1) { ["translations"]=> array(1) { [0]=> array(1) { ["translatedText"]=> string(11) "Hello World" } } } } I appreciate it and I'll see if I can learn how to figure out the rest. Quote Link to comment https://forums.phpfreaks.com/topic/261196-anyone-familiar-with-google-translate-api/#findComment-1338572 Share on other sites More sharing options...
samshel Posted April 18, 2012 Share Posted April 18, 2012 Just a hint... you will have to use translations[0] and not translations HTH Quote Link to comment https://forums.phpfreaks.com/topic/261196-anyone-familiar-with-google-translate-api/#findComment-1338575 Share on other sites More sharing options...
soma56 Posted April 18, 2012 Author Share Posted April 18, 2012 Ha. Got it, but something tells me there's probably over a hundred better ways to get this value: foreach ($arr as $var){ foreach ($var as $vv){ foreach ($vv as $v){ foreach ($v as $i){ echo $i; } } } } Quote Link to comment https://forums.phpfreaks.com/topic/261196-anyone-familiar-with-google-translate-api/#findComment-1338594 Share on other sites More sharing options...
samshel Posted April 18, 2012 Share Posted April 18, 2012 $translate = file_get_contents("https://www.googleapis.com/language/translate/v2?key=MyKey&q=$content&source=es&target=en"); $arr = json_decode($translate, true); var_dump($arr); echo $arr['data']['translations'][0]['translatedText']; Quote Link to comment https://forums.phpfreaks.com/topic/261196-anyone-familiar-with-google-translate-api/#findComment-1338598 Share on other sites More sharing options...
soma56 Posted April 19, 2012 Author Share Posted April 19, 2012 Thank you. I like my way better though (just kidding). Quote Link to comment https://forums.phpfreaks.com/topic/261196-anyone-familiar-with-google-translate-api/#findComment-1338631 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.