剛剛玩了一下「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,但回傳給我的字串為何顯示「澳大利亚米尔顿」呵~ 不過有些還蠻準確的~