Last active
October 13, 2015 13:18
-
-
Save LogansUA/5d81019de52006cf083f to your computer and use it in GitHub Desktop.
Google translate function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Google translate | |
* | |
* @param string $from From language | |
* @param string $to To language | |
* @param string $text Word to translate | |
* | |
* @return string | |
*/ | |
public function googleTranslate($from, $to, $text) | |
{ | |
ini_set('user_agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3'); | |
ini_set('default_charset', 'utf-8'); | |
$getString = 'hl='.$from.'&tl='.$to.'&q='.urlencode($text); | |
$data = file_get_contents('http://translate.google.com/?'.$getString); | |
$dom = new DOMDocument; | |
$dom->loadHTML($data); | |
$items = $dom->getElementById('result_box'); | |
return $items->nodeValue; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment