blog.Ring.idv.tw

Zend Framework

用Zend_Http做一個IP地理位址查詢

剛剛玩了一下「IP地址查询-IP地理位置查询系统-中文e讯」想說乾脆也用Zend_Http來做一個應用~

require_once 'Zend/Http/Client.php';

$ip = '163.17.131.33';

$url = "http://ip.cemsg.com/index.php";

$client = new Zend_Http_Client();
$client->setUri($url);
$client->setConfig(array(
    'maxredirects' => 0,
    'timeout'      => 30)
);
$client->setMethod(Zend_Http_Client::POST);
$client->setParameterPost('s', $ip);
$client->setParameterPost('doit', 1);
$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=\"ip_pos\" style=\"color:#FF0000\">(.*)<\/div>/Us";
	preg_match($pattern, $body, $matches);
	echo $matches[1];
}

不過出來的結果好像似乎有點不準,上述的IP是學校的Web Server,但回傳給我的字串為何顯示「澳大利亚米尔顿」呵~ 不過有些還蠻準確的~

2007-12-08 21:41:21 | Comments (2)

用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 | Comments (2)

Zend_Http發送簡訊 - 使用TWSMS

這裡提供一個透過TWSMS發送簡訊的小範例,利用Zend Framework所提供的Zend_Http蠻方便的~

$message = urlencode("測試中文");

$client = new Zend_Http_Client();
$client->setUri("http://api.twsms.com/send_sms.php?username=xxxx&password=xxxx&type=now&encoding=big5&mobile=09xxxxxxxx&message=$message&vldtme=3600");
$client->setConfig(array(
    'maxredirects' => 0,
    'timeout'      => 30)
);

$client->setMethod(Zend_Http_Client::GET);
$response = $client->request();
	
if ($response->isError())
{
	echo "Server reply was: " . $response->getStatus() . " " . $response->getMessage() . "\n";
}else{
	$body = $response->getBody();
	echo $body;
}

2007-11-27 16:13:08 | Add Comment

使用Zend_Mail透過Google GMail來寄信

剛試出來的一個小範例,使用Zend Framework所提供的Zend_Mail來寄信~

而所指定的SMTP就是要透過Google的Gmail來寄信~

在使用這個範例之前~ 由於Gmail必須透過加密傳輸~ 所以請確認你已經為PHP安置好OpenSSL

程式範例如下:

require_once 'Zend/Mail.php';
require_once 'Zend/Mail/Transport/Smtp.php';

$config = array(
'auth' => 'login',
'username' => '你的帳號',
'password' => '******',
'ssl' => 'tls',
'port' => '587'
);

$transport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config);

$mail = new Zend_Mail('UTF-8');
$mail->setBodyText('This is the text of the mail.');
$mail->setFrom('[email protected]', 'Some Sender');
$mail->addTo('[email protected]', 'Shen');
$mail->setSubject('Zend Mail!');
$mail->send($transport);

參考資源:

Zend_Mail - how to use Zend_Mail_Transport_Smtp for googlemail.com

2007-10-12 12:21:26 | Add Comment

Zend Framework in Action

Zend Framework in Action

作者:Rob Allen & Nick Lo

這本書問世的時間應該要到明年4月了~

從七月份釋出Zend Framework 1.0 版後~ 到這本書要拿到手中可能要九或十個月之久~

所以還是趕緊K官方文件較實在了~

有空的話可以去Rob Allen的Blog走走~ 裡面有蠻豐富的資源~

2007-10-12 11:00:52 | Comments (1)

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

::: 搜尋 :::

::: 分類 :::

::: Ads :::

::: 最新文章 :::

::: 最新回應 :::

::: 訂閱 :::

Atom feed
Atom Comment