微信公众平台体验之四(IP查询)

接上文,增加一个IP查询功能
调用新浪接口查IP,

function city_ip($ip) {
$url = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=' . $ip;
$f = new SaeFetchurl();
$str = $f->fetch($url);
preg_match('|var remote_ip_info =(.*)\;|',$str,$ar);
$obj = json_decode($ar[1]);
$city =$obj->city;
return $city;
}

在responseMsg()函数中增加处理

if(!empty( $keyword ))
{
$msgType = "text";
$wel = "百度查正在为你服务,暂时只支持单词翻译及IP查询,手机号查询,支持日语,韩语译成中文。(以后会增加身份证等查询。)";
$format = "查ip 请用英文 ,+ip ;"."\n"."翻译单词请直接发送单词。"."\n"."查手机号归属地请用英文 .+手机号"."\n"."(例如: ,58.243.79.221 방귀 .15850781443 )";
$contentStr = $wel."\n\n".$format;
if($keyword == "h" || $keyword == "Hello2BizUser")
{
$contentStr = $format;
}
else
{
$key1 = substr($keyword,0,1);
$key2 = substr($keyword,1);
if($key1 == "," || $key1 == ",")
{
$city_name = city_ip($key2);//用户发来的IP
if($city_name !== "")
$contentStr = $city_name;
else
$contentStr = "未查到有关信息或输入有误!";
}
else if($key1 == "." || $key1 == "。")

$contentStr = taobao_m($key2);

else if($key1 != "," && $key1 != ".")

$contentStr = youdao($keyword);//用户发来的词汇
else
$contentStr = $wel."\n\n".$format;
}
$contentStr .= "\n发送h,查看使用帮助";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else{

未经允许不得转载:ganlei的个人博客 » 微信公众平台体验之四(IP查询)

赞 (1)
分享到:更多 ()