blog.Ring.idv.tw

用Zend_Http來做Google翻譯

用Zend_Http來做Google翻譯


前幾天寫了一個小程式,利用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];
}

參考資源:

檢查Alexa PageRank、Google/MSN/Yahoo Backlinks的PHP程式範例

preg_match

Zend_Http

2007-12-07 14:56:12

2 comments on "用Zend_Http來做Google翻譯"

  1. 1. 呈現 說:

    看完學長的文章後,
    試著改寫成google搜尋的功能,
    真的可行耶!真是太酷了^^

    2007-12-08 03:09:27

  2. 2. Shen 說:

    呵~ 不錯呀!~ 繼續加油!^^

    2007-12-08 13:45:49

Leave a Comment

Copyright (C) Ching-Shen Chen. All rights reserved.

::: 搜尋 :::

::: 分類 :::

::: Ads :::

::: 最新文章 :::

::: 最新回應 :::

::: 訂閱 :::

Atom feed
Atom Comment

::: 人氣指數 :::

今日人氣:458

累積人氣:3006740


::: 線上人數 :::

counter