在前面利用 QQWry.Dat 实现 IP 地址高效检索(PHP) 和 利用 QQWry.Dat 实现 IP 地址高效检索(PHP 共享内存版)的基础上,我们只需再写一点儿代码,就可以实现一个 PHP XML-RPC 的查询接口了。
下面是有用共享内存版实现的程序:
<?php include('xmlrpc.inc.php'); include('xmlrpcs.inc.php'); include('iplocation.inc.php'); function iplocation_xmlrpc($params) { $ipval = $params->getParam(0); $ip = $ipval->scalarval(); $iplocation = & IpLocation::getInstance(); $location = $iplocation->getlocation($ip); if (function_exists('iconv')) { $location['country'] = iconv("GBK", "UTF-8", $location['country']); $location['area'] = iconv("GBK", "UTF-8", $location['area']); } else if (function_exists('mb_convert_encoding')) { $location['country'] = mb_convert_encoding($location['country'], "UTF-8", "GBK"); $location['area'] = mb_convert_encoding($location['area'], "UTF-8", "GBK"); } else { return new xmlrpcresp(0, $xmlrpcerruser, "Can't convert the result to the correct encode."); } return new xmlrpcresp(new xmlrpcval(array( "ip" => new xmlrpcval($location['ip'], "string"), "beginip" => new xmlrpcval($location['beginip'], "string"), "endip" => new xmlrpcval($location['endip'], "string"), "country" => new xmlrpcval($location['country'], "string"), "area" => new xmlrpcval($location['area'], 'string')), "struct")); } new xmlrpc_server(array('iplocation' => array('function' => 'iplocation_xmlrpc', 'signature' => array(array('struct', 'string')), 'docstring' => 'Return the location of the ip or domain name.')) ); ?> |
有了这个 XML-RPC 服务器,我们就可以在任何程序中去调用它了
|
|
|
|