<?php
$api_key = 'PUT_YOUR_SERVER_KEY_HERE';
$text = 'How are you';
$source="en";
$target="fr";
$url = 'https://www.googleapis.com/language/translate/v2?key=' . $api_key . '&q=' . rawurlencode($text);
$url .= '&target='.$target;
$url .= '&source='.$source;
$response = file_get_contents($url);
$obj =json_decode($response,true); //true converts stdClass to associative array.
if($obj != null)
{
if(isset($obj['error']))
{
echo "Error is : ".$obj['error']['message'];
}
else
{
echo "Translsated Text: ".$obj['data']['translations'][0]['translatedText']."\n";
}
}
else
echo "UNKNOW ERROR";
?>
======================================================
Google Translate API ( PHP CURL ):
=================================
<?php
$api_key = 'PUT_YOUR_SERVER_KEY_HERE';
$text = 'How are you';
$source="en";
$target="fr";
$obj = translate($api_key,$text,$target,$source);
if($obj != null)
{
if(isset($obj['error']))
{
echo "Error is : ".$obj['error']['message'];
}
else
{
echo "Translsated Text: ".$obj['data']['translations'][0]['translatedText']."\n";
if(isset($obj['data']['translations'][0]['detectedSourceLanguage'])) //this is set if only source is not available.
echo "Detecte Source Languge : ".$obj['data']['translations'][0]['detectedSourceLanguage']."\n";
}
}
else
echo "UNKNOW ERROR";
function translate($api_key,$text,$target,$source=false)
{
$url = 'https://www.googleapis.com/language/translate/v2?key=' . $api_key . '&q=' . rawurlencode($text);
$url .= '&target='.$target;
if($source)
$url .= '&source='.$source;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$obj =json_decode($response,true); //true converts stdClass to associative array.
return $obj;
}
?>
======================================================
Google Translate API ( GET Request ):
====================================
https://www.googleapis.com/language/translate/v2/detect?key={YOUR_API_KEY}&q=Wie%20geht%20es%20Ihnen
Multiple 'q' parameters:
========================
https://www.googleapis.com/language/translate/v2?key={YOUR_API_KEY}&source=en&target=de&q=Hello%20Ravi&q=How%20are%20you&q=I%20am%20fine
No comments:
Post a Comment