Sep 26

function translate($s,$d,$q){
 // Basic request parameters:
 // s = source language
 // d = destination language
 // q = Text to be translated
 
 $lang_pair = urlencode($s.’|’.$d);
 $q = urlencode($q);
 
 // Google’s API translator URL
 $url = “http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=”.$q.”&langpair=”.$lang_pair;
 
 // Make sure to set CURLOPT_REFERER because Google doesn’t like if you leave the referrer out
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_REFERER, “http://www.yoursite.com/translate.php“);
 $body = curl_exec($ch);
 curl_close($ch);
 
 $json = json_decode($body, true);
 return $json['responseData']['translatedText'];
}

ไม่แน่ใจว่า ลอก script นี้มาจากเว็บไหน แต่มันก็โอเค เรียกใช้ Google API แปลได้หลายภาษาเลย แต่บางจังหวะก็ไม่ work สงสัย google ทำงานไม่ทันมั้ง

  • Share/Bookmark