微信公众平台功能大杂烩 - ip/域名查询 车牌号归属地查询 手机归属地查询 公交查询 英汉互译

我看到有些公众平台比如:海南大学 功能非常丰富,包含四六级成绩查询、自习室空位查询、小i机器人、天气查询、快递查询、笑话、火车查询、人品计算。。。

我实现了海南大学微信公众平台的部分功能,再根据自己需要另外添加了车牌查询等,这些功能觉得对我来说实用的,有下面几个:

1、ip/域名查询

2、车牌号归属地查询

3、手机归属地查询

4、公交查询

5、英汉互译

其中 ip查询是调用互联网的:http://www.ip.cn/getip.php?action=queryip&from=web&ip_url=

车牌号查询是我自己在网络上抓的数据import到mysql中,做的模糊like查询实现的

手机归属地查询也有现成的api可以调用:http://api.showji.com/Locating/www.showji.com.aspx?output=json&callback=querycallback&m=

公交查询是使用百度的私有api

英汉互译是调用百度的翻译api,需要申请一个key(code:keykey需要手动更改)

另外小i 因为经常被封杀的是使用cookies和js加密的,抱歉不能用表示我实在没有耐心和时间研究破解它了。

和上一篇blog所说的google tts也能够被调用,我并没有在截图中弄出来,因为还在测试中,暂时也没想到怎样应用它:

核心代码:

<?php

    //
    // 用經度, 緯度查詢 地址!
    // http://www.yuehn.com/tq.php?longitude=30.612270&latitude=104.067291
    // http://maps.googleapis.com/maps/api/geocode/json?latlng=30.612270,104.067291&sensor=false
    //
    // 參考資料..
    // https://developers.google.com/maps/documentation/geocoding/?hl=zh-tw
    
class GoogleMapOBj 
{
        
    function translateAddressToLatLng($address)
    {
    $url = "http://maps.googleapis.com/maps/api/geocode/json?sensor=true&address=".urlencode($address);
        $request = file_get_contents($url);
      $json = json_decode($request,true);
      $geo_status = $json['status'];
    if($geo_status=="OVER_QUERY_LIMIT"){ die("OVER_QUERY_LIMIT"); }
    if($geo_status!="OK") return "return status error !";      
    //$geo_address = $json['results'][0]['formatted_address'];
    $geo_lat = $json['results'][0]['geometry']['location'];
    return $geo_lat;
    }    
    
    //
    public function translateLatLngtoAddress($lat,$long)
    {
        $lat=mb_convert_encoding($lat, 'UTF-8',mb_detect_encoding($lat, 'UTF-8, ISO-8859-1', true));
        $long=mb_convert_encoding($long, 'UTF-8',mb_detect_encoding($long, 'UTF-8, ISO-8859-1', true));
        $url="http://maps.googleapis.com/maps/api/geocode/json?latlng=$lat,$long&sensor=false";
        $request=file_get_contents($url);
        $json=json_decode($request,true);
        return $json['results'][0]['formatted_address']; // 傳回此經緯度的地址
        //return $json['results'][0]['geometry']['location']; // 傳回經度與緯度
    }
}

class BaiduTranslate
{
    function translateString($string)
    {
        $url = "http://openapi.baidu.com/public/2.0/bmt/translate";
        $post_data = array
        (
            'client_id' => 'keykey',
            'q' => $string
        );
        $context = array();
        if (is_array($post_data))
        {
            ksort($post_data);
            $context['http'] = array
            (
                'method' => 'POST',
                'content' => http_build_query($post_data, '', '&'),
            );
        }
        $request = file_get_contents($url, false, stream_context_create($context));
        $json = json_decode($request,true);
        $trans_result = $json['trans_result'][0]['dst'];
        return $trans_result;
    }
}
class MobAddress
{
    function getMobAddressByNumber($number)
    {
        $url = "http://api.showji.com/Locating/www.showji.com.aspx?output=json&callback=querycallback&m=".$number;
        $request = file_get_contents($url);
        $request = mb_substr($request, 14);
        $request = substr($request, 0, strlen($request)-2);
        $json1 = json_decode($request,true);
        if($json1['QueryResult'] == "True")
        $aaa=<<<STR
您查询的号码: {$json1['Mobile']}
卡号归属地: {$json1['Province']} {$json1['City']}
区号: {$json1['AreaCode']}
邮编: {$json1['PostCode']}
卡类型: {$json1['Corp']}{$json1['Card']}
STR;
        else
            return "没有找到您要查询的手机号归属地";
        return $aaa;
    }
}
class IPAddress
{
    function getAddressByIP($ip)
    {
        $url = "http://www.ip.cn/getip.php?action=queryip&from=web&ip_url=".$ip;
        $request = file_get_contents($url);
        $request = iconv("gbk","UTF-8",$request);
        return strip_tags($request);
    }
}
class Traff
{
    function getTraffLine($src,$dst)
    {
        $url="http://map.baidu.com/?newmap=1&reqflag=pcmap&biz=1&qt=bt&c=1&sn=2$$$$$$".$src."$$0$$$$&en=2$$$$$$".$dst."$$0$$$$&sc=1&ec=1&rn=5&tn=B_NORMAL_MAP&nn=0&ie=utf-8&l=6&b=%289980316.235,2887022.065;14141852.235,3775854.065%29&t=1365849817463";
        $request = file_get_contents($url);//echo $request;
        $json = json_decode($request,true);
        //$trans_result = $json['content'][0]['lines'][0][0]['name'];
        $ReqStr = "";
        $planLines = array();
        $contents = $json['content'];
        foreach ($contents as $contentIndex => $contentValue)
        {
            $lines = $contentValue['lines'];
            $stops = $contentValue['stops'];    //get stops
            $lineArr = array();
            $stopArr = array();
            for($num=0;$num<count($lines[0])-7;$num++)
            {
                $tempArr = array();
                array_push($lineArr,$tempArr);
            }
            if(count($lines)<=0) return "请输入更精确的地址,如: 成都市武侯区武侯祠。";
            foreach ($lines as $lineIndex => $lineValue)
            {
                $tools = $lineValue;
                //foreach ($tools as $toolIndex => $toolValue)
                for($toolIndex=0;$toolIndex<count($tools)-7;$toolIndex++)
                {
                    array_push($lineArr[$toolIndex],$this->getChunBus($tools[$toolIndex]['name']));
                    $lineArr[$toolIndex] = array_unique($lineArr[$toolIndex]);
                }
            }
            foreach ($stops as $stopIndex => $stopValue)
            {
                $stations = $stopValue;
                //foreach ($tools as $toolIndex => $toolValue)
                for($stationIndex=0;$stationIndex<count($stations);$stationIndex++)
                {
                    array_push($stopArr,$stations[$stationIndex]['getOff']['name']);
                    array_push($stopArr,$stations[$stationIndex]['getOn']['name']);
                    $stopArr = array_unique($stopArr);
                }
            }
            //$ReqStr .= print_r($lineArr).print_r($stopArr);
            array_push($planLines,Array(
            'lines'=>$lineArr,'stops'=>$stopArr)
            );
        }
        //echo print_r($planLines);
        foreach ($planLines as $planLineIndex => $planLineValue)
        {
            $ReqStr .= (string)($planLineIndex+1).": ";
            $lines = $planLineValue['lines'];
            $stops = $planLineValue['stops'];
            foreach($lines as $lineIndex => $lineValue)
            {
                $toolstr = "";
                if(count($lineValue)>=2)
                    $toolstr = "(";
                foreach($lineValue as $tmpIndex => $tmpValue)    //[0] => 84路
                {
                    $toolstr .= $tmpValue."/";
                }
                $toolstr = mb_substr($toolstr,0,mb_strlen($toolstr,"UTF-8")-1,"UTF-8");
                if(count($lineValue)>=2)
                    $toolstr .= ")";
                $ReqStr .= $toolstr."→";
            }
            $ReqStr = mb_substr($ReqStr,0,mb_strlen($ReqStr,"UTF-8")-1,"UTF-8");
            $ReqStr .= "\n途经: ";
            $stops = array_slice($stops,1,count($stops)-2);
            foreach($stops as $stopIndex => $stopValue)
            {
                $ReqStr .= $stopValue . ",";
            }
            $ReqStr = mb_substr($ReqStr,0,mb_strlen($ReqStr,"UTF-8")-1,"UTF-8");
            $ReqStr .= "\n";
        }
        return $ReqStr;
    }
    function getChunBus($Bus)
    {
        return preg_replace('/\(.+?\)/','',$Bus);
    }
}

class XiaoI
{
    public function get_wap_xiaoi($key){
    $post_data =
       array(
               'requestContent='.$key,
       );
       $post_data = implode('&',$post_data);
        
       /***old method
       $url='http://nlp.xiaoi.com/robot/demo/wap/wap-demo.action';
        
       $ch = curl_init();
       curl_setopt($ch, CURLOPT_POST, 1);
       curl_setopt($ch, CURLOPT_URL,$url);
               curl_setopt($ch,CURLOPT_USERAGENT,"Opera/9.60");
       curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
       ob_start();
       curl_exec($ch);
       $result = ob_get_contents() ;
       ob_end_clean();
        
       */
       $url="http://nlp.xiaoi.com/robot/demo/wap/";
       $ch = curl_init();  
       curl_setopt($ch, CURLOPT_URL, $url);  
       curl_setopt($ch, CURLOPT_HEADER, 1);  
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
       $content = curl_exec($ch);  
       curl_close($ch); 
       //echo $content;
       list($header, $body) = explode("\r\n\r\n", $content);  
       preg_match("/set\-cookie:([^\r\n]*)/i", $header, $matches);  
           
       $cookie = $matches[1]; 
       $ch = curl_init( ); 
       curl_setopt( $ch, CURLOPT_REFERER, "http://nlp.xiaoi.com/robot/demo/wap/" ); 
       curl_setopt( $ch, CURLOPT_HEADER, true ); 
       curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); 
       curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6; Windows NT 5.0)" );  
       curl_setopt( $ch, CURLOPT_URL, "http://nlp.xiaoi.com/robot/demo/wap/wap-demo.action" ); 
       curl_setopt( $ch, CURLOPT_POST, true ); 
       curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_data );
       curl_setopt($ch, CURLOPT_COOKIE, $cookie);
       $content = curl_exec($ch);  
       curl_close($ch); 

       $preg = '/<\/span>(.*)<\/p>/iUs';
       preg_match_all($preg,$content,$match);
       $response_msg=$match[0][0];
       $preg = "/<\/?[^>]+>/i";
       $response_msg=preg_replace($preg,'',$response_msg);
       if("hello,how are you"==$response_msg||"how do you do"==$response_msg)
       {
           $response_msg="小i机器人欢迎您,欢迎各种调戏…/:,@-D";//欢迎语
       }
       $response_msg=trim($response_msg);
       return $response_msg;
   }
}

class GoogleTTS
{
    public function getGoogleTTS($key,$tl='zh-cn'){
    $post_data =
       array(
            'idx=0',
            'ie=UTF-8',
            'q='.$key,
            'tl='.$tl,
            'total=1',
            'textlen='.(string)mb_strlen($key,"UTF-8")
       );
       $post_data = implode('&',$post_data);
        
       
       $url="http://translate.google.com/translate_tts";

       $ch = curl_init();
       curl_setopt($ch, CURLOPT_URL, $url);
       curl_setopt( $ch, CURLOPT_REFERER, "http://translate.google.com" ); 
       curl_setopt( $ch, CURLOPT_HEADER, true ); 
       curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); 
       curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 5.1; rv:20.0) Gecko/20100101 Firefox/20.0" );  
       curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: translate.google.com'));        //host
       curl_setopt( $ch, CURLOPT_POST, true ); 
       curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_data );
       $content = curl_exec($ch);
       curl_close($ch);
       $response_msg=trim($content);
       return $content;
   }
}

?>

 

posted @ 2013-05-08 15:31 重庆Debug 阅读(...) 评论(...) 编辑 收藏