前幾天寫了一個小程式,利用Zend_Http來做Google翻譯:
require_once 'Zend/Http/Client.php'; $sentence = urlencode("Hello! Welcome to visit my blog."); $langpair = urlencode("en|zh-TW"); $url = "http://www.google.com/translate_t?langpair=$langpair&hl=en&ie=UTF8&text=$sentence"; $client = new Zend_Http_Client(); $client->setUri($url); $client->setConfig(array( 'maxredirects' => 0, 'timeout' => 30) ); $client->setMethod(Zend_Http_Client::GET); $client->setHeaders('User-Agent', 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'); $response = $client->request(); if ($response->isError()) { echo "Error transmitting data.\n"; echo "Server reply was: " . $response->getStatus() . " " . $response->getMessage() . "\n"; }else{ $body = $response->getBody(); $pattern = "/<div id=result_box dir=\"ltr\">(.*)<\/div><\/td>/Us"; preg_match($pattern, $body, $matches); echo $matches[1]; }
參考資源:
看完學長的文章後,
試著改寫成google搜尋的功能,
真的可行耶!真是太酷了^^
2007-12-08 03:09:27
呵~ 不錯呀!~ 繼續加油!^^
2007-12-08 13:45:49